Re: Log2 seems inefficient
Jakob Bohm via Cygwin <[email protected]>
| Newsgroups | gmane.os.cygwin |
|---|---|
| Organization | WiseMo A/S |
| Message-ID | <[email protected]> |
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