www.delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2026/01/21/23:42:10

DMARC-Filter: OpenDMARC Filter v1.4.2 delorie.com 60M4g9dl1907911
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 60M4g9dl1907911
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=hgIo4tZk
X-Recipient: archive-cygwin AT delorie DOT com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8D9B94BC8963
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
s=default; t=1769056927;
bh=PXsqVUMx6ZVHhWOQikWehuUZq91dbAq/YaCnfZsO+5I=;
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=hgIo4tZkY6pTyt6atZdEDE90AzcXC3eYpV4AUp9pO8WIWEraaggX2CuwhoeGLiskL
vMxrHceLgai7/iAMABXqp9frJvPFHkHsMThv7KZuZmvHhaew23GRzTg7F6NDQcnmKq
CqpA6mwatpLkEL7V+xPaGQNE7cvJotaAulobaCFI=
X-Original-To: cygwin AT cygwin DOT com
Delivered-To: cygwin AT cygwin DOT com
DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0C3A94BA901E
ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0C3A94BA901E
ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1769056878; cv=none;
b=JoRttlRc2duogoo9ilCqgGQvDFHxE9gpdZSJ9+TQo6heXb2fldeUvH7DZniJvbMq/JaMDEQ0/uaVuUC3MwRwGmJTKE8AxPHZ0uKjjk6uuCLQult7aMuCN61HKkCTerqf+ZJH/hh9VOOQVKHcpnYEVZBNoU3bLI4z6L2Yez00dqg=
ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key;
t=1769056878; c=relaxed/simple;
bh=mFlg6GJJxIQH0QcLGaqPkaukDvwvYkToKeSB8/y57rY=;
h=DKIM-Signature:Date:From:To:Message-ID:Subject:MIME-Version;
b=pwkjHwZM9I43it61zmqTbhgxlIFAf6zheKTw1LsnOqRudqpQFi8UwIdCNt3obg9L1QV7qGvoFCtl4CchdsEP97JL1tKNvu7DX9icv569LDjBbD1VLRoN5TtM+TeHj5Q0kyZ+qdLwM0MQ4lovzu4ox3tQFXQQzgN89ahDD5Q+Jb0=
ARC-Authentication-Results: i=1; server2.sourceware.org
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0C3A94BA901E
Date: Wed, 21 Jan 2026 20:41:14 -0800 (PST)
To: Jakob Bohm <jb-cygwin AT wisemo DOT com>,
Jakob Bohm via Cygwin <cygwin AT cygwin DOT com>
Message-ID: <1534416783.678540.1769056874631@connect.xfinity.com>
In-Reply-To: <cc290097-2b7c-a067-981d-f0c3b6f5ee85@wisemo.com>
References: <226987210 DOT 591128 DOT 1768766440723 AT connect DOT xfinity DOT com>
<cc290097-2b7c-a067-981d-f0c3b6f5ee85 AT wisemo DOT 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: 9776
X-Originating-Client: open-xchange-appsuite
X-CMAE-Envelope: MS4xfEyUPA6GNT7eV6VxX3SDztttcaUwTBKBTImqZQlNtDjgswpx7vlPoB+XAr0MSavZR/3653jQaagIj//TUqN4hPlEWDKYhFJdrmu0DAV83hyYfaklqtHj
/oyw5Oaybkmk19xVvXFkpIj92r5uYAdE0fvv90itGklpmLHrN0s3/ZOr9jcvrBwMRPfMxHslcf30wJ15fR60jEjWr8ArMtIjyQ4g4tRSq1NEfP4hEKXC1ygS
hAEb/C6qmWDadUlZ/2+M+g==
X-BeenThere: cygwin AT cygwin DOT com
X-Mailman-Version: 2.1.30
List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com>
List-Unsubscribe: <https://cygwin.com/mailman/options/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=unsubscribe>
List-Archive: <https://cygwin.com/pipermail/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-request AT cygwin DOT com?subject=help>
List-Subscribe: <https://cygwin.com/mailman/listinfo/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=subscribe>
From: KENNON J CONRAD via Cygwin <cygwin AT cygwin DOT com>
Reply-To: KENNON J CONRAD <kennonconrad AT comcast DOT net>
Errors-To: cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com
Sender: "Cygwin" <cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com>
X-MIME-Autoconverted: from base64 to 8bit by delorie.com id 60M4g9dl1907911

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 AT cygwin DOT 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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019