[PATCH] math.h: remove the log2() macro

Jeff Johnston jjohnstn@redhat.com
Thu Nov 12 18:43:51 GMT 2026


Patch pushed to main.  Thanks.

-- Jeff J.

On Sun, Oct 25, 2026 at 9:21 AM Carlo Bramini <carlo.bramix@libero.it>
wrote:

> Hi all,
>
> The math.h of Newlib currently contains this:
>
> extern double log2 (double);
> #if !defined(__cplusplus)
> #define log2(x) (log (x) / _M_LN2)
> #endif
>
> When building Python on Cygwin, the tests for math.log2() fail.
> The cause is this macro: it replaces calls to log2() with log(x) / _M_LN2,
> so C code never reaches the dedicated log2() function from libm. The two
> expressions are mathematically equivalent, but they are not numerically
> equivalent: the result goes through two roundings (the log() and the
> division) plus the error in the constant, so it is generally less accurate
> than a proper log2().
>
> Here is a tiny test case that shows the problem:
>
> #include <stdio.h>
> #include <math.h>
>
> int main(void)
> {
>     volatile double value = 0x1p1023;
>
>     printf("log2=%.14f\n", log2(value));
>
>     /* Remove the macro definition from math.h */
> #undef log2
>     printf("log2=%.14f\n", log2(value));
>
>     return 0;
> }
>
> Compiling and running it gives:
>
> $ gcc log2.c -o log2
> $ ./log2
> log2=1023.00000000000011
> log2=1023.00000000000000
>
> The exact result is 1023. The first line goes through the macro (log(x) /
> _M_LN2) and is off by 1 ULP.
> After "#undef log2", the call reaches the real log2() from libm and
> returns the exact value.
> Removing the macro from math.h fixes this test, as well as the log2 tests
> in Python.
>
> The test was run on:
> CYGWIN_NT-10.0-26300 3.6.11-1.x86_64 2026-10-01 22:12 UTC x86_64 Cygwin
> gcc version 14.4.0 (GCC)
>
> There is also a precedent in the project history: the equivalent macro for
> log2f() was removed from math.h in commit
> 17ecab12a81003db4362156044a75df35defa227 (Jeff Johnston, 2012-12-18,
> "Remove the log2f macro").
>
> $ git show 17ecab12a81003db4362156044a75df35defa227
> commit 17ecab12a81003db4362156044a75df35defa227
> Author: Jeff Johnston <jjohnstn@redhat.com>
> Date:   Tue Dec 18 22:41:53 2012 +0000
>
>     2012-12-18  Jeff Johnston  <jjohnstn@redhat.com>
>
>             * libc/include/math.h: Remove the log2f macro.
>
> Which corresponds to this change in math.h:
>
> --- a/newlib/libc/include/math.h
> +++ b/newlib/libc/include/math.h
> @@ -378,9 +378,6 @@ extern float lgammaf _PARAMS((float));
>  extern float erff _PARAMS((float));
>  extern float erfcf _PARAMS((float));
>  extern float log2f _PARAMS((float));
> -#if !defined(__cplusplus)
> -#define log2f(x) (logf (x) / (float_t) _M_LN2)
> -#endif
>  extern float hypotf _PARAMS((float, float));
>  #endif /* ! defined (_REENT_ONLY) */
>
> Unfortunately, the log2() macro was left in place, which looks like an
> oversight, but I could not find any discussion of that change in the list
> archives.
>
> I think correctness of the result should take priority, since log2() is
> already declared and implemented in libm, and a function-like macro that
> silently changes the numerical result of a standard function is surprising
> for users.
>
> I propose the removal of the log2() macro from newlib/libc/include/math.h.
> The patch is attached to this email.
>
> Thank you very much for your time.
> Sincerely,
>
> Carlo Bramini.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://sourceware.org/pipermail/newlib/attachments/20261112/22d4c9ec/attachment.htm>


More information about the Newlib mailing list