DMARC-Filter: OpenDMARC Filter v1.4.2 delorie.com 60M8gKR72006186 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 60M8gKR72006186 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=F3XfLCsq X-Recipient: archive-cygwin AT delorie DOT com DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CB58F4BA9036 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com; s=default; t=1769071337; bh=bmMy95eA7DW4RUFSIncM1IxerQ54z9OF2/BpQNgmIH8=; h=Date:Subject:To:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=F3XfLCsqLK3mGVH1v/tfhJVP3IfBcRHdHfKu5PaB0Hvd1s716Csd83nKZT5sTW+yF qIrAiOCrmzdvaOEHPlzp1Ub7zDnY44UxTpjqJTFsadi3LgkQzrsmfn0JTPlIoi1luU l9QJpM62+0UVL1TxZkyRLHTn1kxbMaAqn48riSMU= X-Original-To: cygwin AT cygwin DOT com Delivered-To: cygwin AT cygwin DOT com DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 40DF14BA540B ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 40DF14BA540B ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1769071316; cv=none; b=LbVvn9pNSG8Ew0A0Pr3HgHcf6f4U1YeeO7BafH4zcRDfRjmfrNmAr0eO1IEU2ZKBBfXwSG6BWb3pwxkKNqCV/BHOhtHkJ2hGYhroR7wtM7FxsWVLuqMn/Fn8K83AVebv+RdP7fIwV7Js3JHnM2FTCTFm2QOks0ey75FuesVS460= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1769071316; c=relaxed/simple; bh=2YbxGHZVssc14Yh65fQMMnqJFIk4wl45SWrbBNz9PCs=; h=Message-ID:Date:MIME-Version:From:Subject:To:DKIM-Signature; b=iPppjQfqsRXSmlviZVhy7MNRd3jSleib+QDWM7C3JHNw56c9mE00OCPwxgNJDXidUCRQKo1N/CiGrOtwDdovkqo6w01uszNElxgouXqr1D0GA9PPPIFkn4s576mDbHbvBVyOb7nSoYY8MCrTC07/w39IC+ggF5AxqPuvOKz/4qk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 40DF14BA540B Message-ID: <7bf7bd13-ecad-4247-818c-31383967188e@SystematicSW.ab.ca> Date: Thu, 22 Jan 2026 01:41:52 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Log2 seems inefficient Content-Language: en-CA To: cygwin AT cygwin DOT com References: <226987210 DOT 591128 DOT 1768766440723 AT connect DOT xfinity DOT com> <1534416783 DOT 678540 DOT 1769056874631 AT connect DOT xfinity DOT com> <1610598307 DOT 678632 DOT 1769057166805 AT connect DOT xfinity DOT com> Organization: Systematic Software In-Reply-To: <1610598307.678632.1769057166805@connect.xfinity.com> X-Rspamd-Server: rspamout06 X-Rspamd-Queue-Id: DD04C35 X-Stat-Signature: 7rdzka8t939xme9mm86cnyxfo3gbcw7z X-Session-Marker: 427269616E2E496E676C69734053797374656D6174696353572E61622E6361 X-Session-ID: U2FsdGVkX1+43YcZCC96BCBGAUAngFjdTxDaEQBsotQ= X-HE-Tag: 1769071313-840390 X-HE-Meta: U2FsdGVkX18+7DttRikzQF6MQ+sY9ou1e2yDJpWTOWXqO4mIg2ntpmQvNqgsvpPFbW7TDwrdvocN4B3r+WbX4KIDtbX1KmI2ki1KJxWjOwHiPurgPVJgC5KSDL4rdXz5sI2a5rBgNAvFRwth8UzraA8315On65yVLpQwJwp5p5y5RC2fsb5WUc7VlOONtUPsKRxCwujrd0skZ2T5QmQ8xJ8hzkeX5MmvQ/Lsy+Ste4d7tQgixpDShQ6Fcr3bkCyf5N9hxtfspapNjTUqyR7ifM8gef7anh8H8pQyx993BxxSQl5geZ5BZL+DTJ7RQo2TFLP/nBFKuygPBm0DEq65vTzi8YEZDSPHNhAsPEiwafdEoLcyLdYvtHAhPXbdy08mlGmKTmiQ3i7RZ6atxN+cdg== X-BeenThere: cygwin AT cygwin DOT com X-Mailman-Version: 2.1.30 Precedence: list List-Id: General Cygwin discussions and problem reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Brian Inglis via Cygwin Reply-To: cygwin AT cygwin DOT com Cc: Brian Inglis Content-Type: text/plain; charset="utf-8"; Format="flowed" Errors-To: cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com Sender: "Cygwin" Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from base64 to 8bit by delorie.com id 60M8gKR72006186 On 2026-01-21 21:46, KENNON J CONRAD via Cygwin wrote: > 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 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 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. Cygwin includes assembler implementations for this and other functions in long double - #undef log2 may allow access to that function, as they appear to be built into libm.a and cygwin1.dll: https://cygwin.com/cgit/newlib-cygwin/tree/winsup/cygwin/math/log2l.S https://cygwin.com/cgit/newlib-cygwin/blame/winsup/cygwin/cygwin.din#n924 -- Take care. Thanks, Brian Inglis Calgary, Alberta, Canada La perfection est atteinte Perfection is achieved non pas lorsqu'il n'y a plus rien à ajouter not when there is no more to add mais lorsqu'il n'y a plus rien à retrancher but when there is no more to cut -- Antoine de Saint-Exupéry -- 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