DMARC-Filter: OpenDMARC Filter v1.4.2 delorie.com 60M4kx1j1909882
Authentication-Results: delorie.com; dmarc=pass (p=none dis=none) header.from=cygwin.com
Authentication-Results: delorie.com; spf=pass smtp.mailfrom=cygwin.com
DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 60M4kx1j1909882
Authentication-Results: delorie.com;
	dkim=pass (1024-bit key, unprotected) header.d=cygwin.com header.i=@cygwin.com header.a=rsa-sha256 header.s=default header.b=Vf405gUB
X-Recipient: archive-cygwin@delorie.com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E20714BA9039
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
	s=default; t=1769057217;
	bh=5e0rOTGkVCQBexcGyy6YudRiVI20Mh0V+Ypksf+lygc=;
	h=Date:To:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:
	 List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:
	 From;
	b=Vf405gUBX1HeB7JKkcXz3xNsj0lxST7xD+uh1U0Qwovx/97wO46+eLPQBQbFthO34
	 VCQsZnRhaV5+NvSioE6HoMDLVaOCQL+4WmjUR92hgPw2rXvsiwKfwB33jcZ2Pelnrd
	 dVfb2vsIWrOvaFu4QrsOUSYKaEhZVPGyHWhi/26I=
X-Original-To: cygwin@cygwin.com
Delivered-To: cygwin@cygwin.com
DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3D8834BA540B
ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3D8834BA540B
ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1769057169; cv=none;
 b=KNASFL7XqRRWGW3x1zio67x3q8KRzF9hr07eG/ESlNqAzLId0hZjKfNRN5IjrAFrXBuqpCkLtLAkmXqYGAldxq8xehpJFJG6SYISj5innWu+mEfaXTUuwqfGiMaHX37+vsAI4Kgp8+BZ/w/JGsnmMS0Dy3+IWvCITHQNNVbrnUw=
ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key;
 t=1769057169; c=relaxed/simple;
 bh=RBVqHlU1j6ipLfOzYFQrZ8/JsYSOP/xdkBT8G3OLI3k=;
 h=DKIM-Signature:Date:From:To:Message-ID:Subject:MIME-Version;
 b=HT3twXMTSs/9hRzOavJ46MmDmhLI4z6HnRuzqkTnL/VhjjTvKBp4vkRyMUAdxQfjuwgQ2fHUf0xJtDV9krdcCfZHn8tAZXiMGJx3UvUsnjVxfP+ecQb0t99u7ha0skYgtr8jEZwA4/uVDXXx8+LAQlsqWTbAGNIBPbHYf43XpmM=
ARC-Authentication-Results: i=1; server2.sourceware.org
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3D8834BA540B
Date: Wed, 21 Jan 2026 20:46:06 -0800 (PST)
To: KENNON J CONRAD via Cygwin <cygwin@cygwin.com>,
        Jakob Bohm <jb-cygwin@wisemo.com>
Message-ID: <1610598307.678632.1769057166805@connect.xfinity.com>
In-Reply-To: <1534416783.678540.1769056874631@connect.xfinity.com>
References: <226987210.591128.1768766440723@connect.xfinity.com>
 <cc290097-2b7c-a067-981d-f0c3b6f5ee85@wisemo.com>
 <1534416783.678540.1769056874631@connect.xfinity.com>
Subject: Re: Log2 seems inefficient
MIME-Version: 1.0
X-Priority: 3
Importance: Normal
X-Mailer: Open-Xchange Mailer v7.10.6-Rev83
X-Originating-IP: ::ffff:50.47.202.14
X-Originating-Port: 28328
X-Originating-Client: open-xchange-appsuite
X-CMAE-Envelope: MS4xfCV4HN8TF/m240XoZfczfaS74hNo6wcz+xvNxf6J6I7sEIcLb3HAjVHRdAWyvGaMpQfL+2SQ/w+wgg6tc6SlnjvsFfP69q7jf+3V7EJsVNLfSAOF0KEW
 /fK6dg14CR+sH3gYhvfSm9gdZVrVE3UZ5MGlihPAbzktQHRD/bQWd4RPX3J4kii9SqDUffS9VHXhg3TAkDs3t5vCfw3mlQ6fQ1YtdQngrF8staNS93hM9Jth
 +hkdOgXFsVrJANjZv6rwuA==
X-BeenThere: cygwin@cygwin.com
X-Mailman-Version: 2.1.30
Precedence: list
List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com>
List-Unsubscribe: <https://cygwin.com/mailman/options/cygwin>,
 <mailto:cygwin-request@cygwin.com?subject=unsubscribe>
List-Archive: <https://cygwin.com/pipermail/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-request@cygwin.com?subject=help>
List-Subscribe: <https://cygwin.com/mailman/listinfo/cygwin>,
 <mailto:cygwin-request@cygwin.com?subject=subscribe>
From: KENNON J CONRAD via Cygwin <cygwin@cygwin.com>
Reply-To: KENNON J CONRAD <kennonconrad@comcast.net>
Content-Type: text/plain; charset="utf-8"
Errors-To: cygwin-bounces~archive-cygwin=delorie.com@cygwin.com
Sender: "Cygwin" <cygwin-bounces~archive-cygwin=delorie.com@cygwin.com>
Content-Transfer-Encoding: 8bit
X-MIME-Autoconverted: from base64 to 8bit by delorie.com id 60M4kx1j1909882

First paragraph should say:

   Thank you, that seems logical since log2() should be more natural (faster?) to compute than log().  But what you describe is almost exactly opposite of what I see with "GCC: (GNU) 13.4.0".

> On 01/21/2026 8:41 PM PST KENNON J CONRAD via Cygwin <cygwin@cygwin.com> wrote:
> 
>  
> Hello Jakob,
> 
>    Thank you, that seems logical since log2 should be more natural (faster?) to compute log2() than log().  But what you describe is almost exactly of what I see with "GCC: (GNU) 13.4.0".
> 
> For this code snipet:
> 
>   uint8_t i;
>   double j = 0.0;
>   for (i = 1 ; i <= 10 ; i++)
>     j += log2((double)i);
> 
> The assembler shows this for the loop (gcc -O3 -S test.c):
> 
> .L2:
> 	movzbl	%bl, %eax
> 	pxor	%xmm0, %xmm0
> 	addl	$1, %ebx
> 	cvtsi2sdl	%eax, %xmm0
> 	call	log
> 	divsd	%xmm7, %xmm0
> 	addsd	%xmm0, %xmm6
> 	cmpb	$11, %bl
> 	jne	.L2
> 
> So it uses the log function in place of the log2 function with the argument in xmm0 and the result in xmm0, and then divides the result by log2(e), which is in xmm7.  (The sum (j) is accumulated in xmm6.)
> 
> From an efficiency standpoint, I find this to be...disappointing.  Log2 should be easier to compute, the divide should not be needed, and xmm7 gets unnecessarily consumed to store the conversion constant.  I even question whether the pxor is necessary.
> 
> Interestingly, if I compile with g++ -O3 -S test.c, I get something more reasonable:
> .L2:
> 	movzbl	%bl, %eax
> 	pxor	%xmm0, %xmm0
> 	addl	$1, %ebx
> 	cvtsi2sdl	%eax, %xmm0
> 	call	log2
> 	addsd	%xmm0, %xmm6
> 	cmpb	$11, %bl
> 	jne	.L2
> 
> So part of the solution may be for this old guy to figure out what it takes to make the rest of his code c++ compliant.
> 
> Best Regards,
> 
> Kennon
> 
>  
> > On 01/21/2026 6:33 PM PST Jakob Bohm via Cygwin <cygwin@cygwin.com> wrote:
> > 
> >  
> > On 18/01/2026 21:00, KENNON J CONRAD via Cygwin wrote:
> > > Hello,
> > >   
> > >     I am not an experienced cygwin user but have been using it to compile code on Windows (gcc), so I may be leaving out some key details.  My issue is that for every log2 call in my code there is a divide that follows a log library call in the assembly code.  It appears to be related to these lines in math.h:
> > >   
> > > #if !defined(__cplusplus)
> > > #define log2(x) (log (x) / _M_LN2)
> > > #endif
> > >   
> > > My questions are:  If log(x) is used, wouldn't it be faster to multiply by constant (1/LN2) instead of divide by constant (LN2)?  And wouldn't it be even more efficient to compute log2 directly instead of computing the natural log and then converting it to log2?
> > >   
> > > I realize this is not a "bug", it just doesn't seem very optimal from an speed standpoint.
> > Please note, that on the x86 and x86_64 platforms, the natural log
> > function log() is often implemented in terms of the intrinsic log2()
> > function multiplied by log(2), which is done by the instruction FYL2X,
> > thus preferably the x86 family port of newlib should include an
> > intrinsic or inline implementation such as something doing the
> > assembler sequence FLD1, FXCH, FYL2X, which takes the arg in ST(0)
> > and returns the log2() result in ST(0).  For targets that tend to
> > run the floating point subsystem in a mode other than traditional
> > x87, perhaps finding similar inline code for SSE or MMX would be
> > needed.  Other libc implementations may include the needed code in
> > gcc-compatible format already.
> > 
> > Enjoy
> > 
> > Jakob
> > -- 
> > Jakob Bohm, CIO, Partner, WiseMo A/S.  https://www.wisemo.com
> > Transformervej 29, 2860 Søborg, Denmark.  Direct +45 31 13 16 10
> > This public discussion message is non-binding and may contain errors.
> > WiseMo - Remote Service Management for PCs, Phones and Embedded
> > 
> > 
> > -- 
> > Problem reports:      https://cygwin.com/problems.html
> > FAQ:                  https://cygwin.com/faq/
> > Documentation:        https://cygwin.com/docs.html
> > Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple
> 
> -- 
> Problem reports:      https://cygwin.com/problems.html
> FAQ:                  https://cygwin.com/faq/
> Documentation:        https://cygwin.com/docs.html
> Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

-- 
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

