On 08/06/2026 10:48, Carlo B. via Cygwin wrote:
Hello,
actually, we have to say that the source code of log2() into newlib:

https://sourceware.org/git/?p=newlib-cygwin.git;a=blob;f=newlib/libm/common/log2.c;h=e48c16cf8014b5ba4b3a89af261f54472d643f65;hb=HEAD

and the source code of log2() into glibc:

https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/ieee754/dbl-64/e_log2.c;h=872cebddd8b4283edd2e653167035c0fd527f3ea;hb=HEAD

are identical, so we can assume that this code is correct and probably
the best we can get in terms of speed.
However, here the problem is different.
Using log() and the property of the logarithms for changing the base
is mathematically correct.
But here, it is WRONG.
Unfortunately, this method doesn't provide the right result because
the intrinsic format of IEEE-754 numbers propagates a precision error.
Calculating the log2 of 2^1023 is a perfect integer number and it's
1023, not 1023.00000000000011.
So, in my opinion that macro MUST be removed and the genuine log2()
function must be used instead.

Thank you very much for your time.
Sincerely,

Carlo Bramini.
That implementation is not what the OP apparently found in the Cygwin
copy of newlib.

And both implementations are suboptimal for the x86 family, which has
a dedicated singleinstruction log2(double) function named FY2LX in its
floating point hardware, as mentionedduring the January discussion, in
fact the basic floating point x87 instruction set offerslog10() and
ln() only via multiplying log2() by a constant from the FLDLxx
instruction family.

Something like:

#if defined(__x86_64__) || defined(__i386)
double logfuncnam(double v) {
 __asm__(
   "...lines below..."  // Put ifndef __x86_64__ around "FWAIT"
   : "=t" (v) : "=t" (v) : "st(7)");
 return v;
}
#endif

; x87 assembler implementations of floating point logarithms
; Initially input is in ST(0) via register calling convention or inlining
; Somehow make sure there is at least one free entry in the ST() stack, perhaps by
;    telling gcc __asm__ that this is needed as shown above

"FWAIT\n\t"       // Only needed if 386-, waits for 80x87 ready for new instruction
"FLDLxx\n\t"      // Different Lxx for log2, ln and log10
                  // This pushes a relevant constant onto the ST() stack
                  // Use FLD1 = 1.0 = log2(2) for log2(a)
                  // Use FLDLN2 = ln(2) for ln(a)
                  // Use FLDLG2 = log10(2) for log10(a)
"FWAIT\n\t"       // Only needed if 386-, waits for 80x87 ready for new instruction
"FXCH ST(1)\n\t"  // Put constant in ST(1), input in ST(0)
"FWAIT\n\t"       // Only needed if 386-, waits for 80x87 ready for new instruction
"FYL2X\n\t"       // replace ST(0) and ST(1) by ST(1)*log2(ST(0))
"FWAIT\n\t"       // Optional: Pass exception (such as zero #Z or negative
                  //   #IA arg) from FPU to CPU on Pentium and later
                  //   On 386 and older, it is enough to do nothing and
                  //   let the exception interrupt the CPU later. An FWAIT
                  //   here would just waste time by halting the CPU while the
                  //   x87 chip calculates the logarithm.
; logarithm is now in ST(0) for return

; ln() Machine code for 486+ is thus                D9 ED //    D9 C9 //    D9 F1 ; log10() Machine code for all x86 chips is thus 9B D9 EC // 9B D9 C9 // 9B D9 F1

The SSE instruction set seems to require using the x87 instruction
for logarithm anyway.


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

Reply via email to