| Message ID | 32109287.1186111.1792934432806@mail1.libero.it |
|---|---|
| State | New |
| Headers |
Return-Path: <newlib-bounces~patchwork=sourceware.org@sourceware.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4AF4E4BA2E32 for <patchwork@sourceware.org>; Sun, 25 Oct 2026 13:21:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4AF4E4BA2E32 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=libero.it header.i=@libero.it header.a=rsa-sha256 header.s=s2021 header.b=YpACvJ0W X-Original-To: newlib@sourceware.org Delivered-To: newlib@sourceware.org Received: from libero.it (smtp-16.italiaonline.it [213.209.10.16]) by sourceware.org (Postfix) with ESMTPS id 69FAA4BA2E04 for <newlib@sourceware.org>; Sun, 25 Oct 2026 13:20:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 69FAA4BA2E04 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=libero.it Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=libero.it ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 69FAA4BA2E04 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=213.209.10.16 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1792934436; cv=none; b=uME6R/M8nEu9ft78ZBoQgY7Z8sasMhMJ1fBXylVLhInNhvDrTrxsTg8IP3lrl96nAXx/OfuEgNgObt9qs/Le+NFHdZUM7L689nFdMsOe6BogD0qGA9AZDfObIKfc43GA0WPRmGv1GQrVYrsHieI7Df7esU48/SjobOnwGuwNYgs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1792934436; c=relaxed/simple; bh=LYEg2WWVZ9CnYpSgaUZZxNx4o6PsDbHRRNZQLlS0PYo=; h=DKIM-Signature:Date:From:To:Message-ID:Subject:MIME-Version; b=aMa73lK9LyP4o4/IWf1IDujEF6SuMJt5ueHkaxChTnC9sI7Hpriq0Z2z3UtPOZi8Z1HVg+BC4hLmdXf3oWxGwIX+8JTTTOdypvAlBjzFLhH5MSDt0GH/8f6MDkT7+fFEd53pw3DxAoe6m+AX83qnrhOoNXs3BmpSB0a6pJVvofY= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=libero.it header.i=@libero.it header.a=rsa-sha256 header.s=s2021 header.b=YpACvJ0W DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 69FAA4BA2E04 Received: from as8prd-stdfull-core-mw-http-api-44.as8prd-stdfull-core-mw-hazelcast-headless.as8prd-stdfull.svc.cluster.local ([10.101.44.16]) by smtp-16.iol.local with ESMTPA id Ky9IxGCi4xQduKy9Ix1xXL; Sun, 25 Oct 2026 14:20:33 +0100 x-libjamoibt: 1601 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=libero.it; s=s2021; t=1792934433; bh=9dlr9TEmRQovr3IoPp2yQJlzC/dJsoK7l0NqwcWVzCA=; h=From; b=YpACvJ0W3D/4uS9URBtptAeEUQFpVwhDBqcduCwd2QRGYjZfZm5s/BW6rzVZkc5jg XWu3UFUMSgK2ZjKTgbwRACIbmD5CIUyUGMqGVVRIH8/JTt9Rj6qiZ0o1M02C+FJR0K 4wlDWmq+qBYLFBJGudsHIwsQnJZEQYEZg0pJAtFrBUrh0BGTYYhT+QxMh4VKq7YF2S 1QfmoRKpbibR2oJTo+M9RTVWH4ZpnulWeDz3VZp9lPLfSdhGlPEOL6PN9pdkc4ttWX 80jHBfIpFRvLXAQv3BHEdbuqw1v1Vp8mBM6cgDzxkqDyKVscKCCAif/HhI5wGjJpoE 2eICZhjQua+gQ== X-Hornet-Cause: gggruggvucftvghtrhhoucdtuddrgeefiedrtddvgdduhedujeegucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecukffvtefnkfetqffpnffkpffgpdfqfgfvnecuuegrihhlohhuthemuceftddtnecunecujfgurhepfffhvffkufggtgfrkgfoihesmhdtsggsgidtjeenucfhrhhomhepvegrrhhlohcuuehrrghmihhnihcuoegtrghrlhhordgsrhgrmhhigieslhhisggvrhhordhitheqnecuggftrfgrthhtvghrnhepkeeukeeiieeuledvheehudeileduvdduleetteejgeeghfevffejhfeghfeuleeunecukfhppeehrdejjedrjedtrddvhedvnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthephedrjeejrdejtddrvdehvddphhgvlhhopegrshekphhrugdqshhtughfuhhllhdqtghorhgvqdhmfidqhhhtthhpqdgrphhiqdeggedrrghskehprhguqdhsthgufhhulhhlqdgtohhrvgdqmhifqdhhrgiivghltggrshhtqdhhvggrughlvghsshdrrghskehprhguqdhsthgufhhulhhlrdhsvhgtrdgtlhhushhtvghrrdhlohgtrghlpdhmrghilhhfrhhomheptggrrhhlohdrsghrrghmihigsehlihgsvghrohdrihhtpdfovfetjfhoshhtpehsmhhtphdqudeirdhiohhlrdhlohgtrghlpdhmohguvgepshhmthhpohhuthdpnhgspghrtghpthhtohepuddpghgvohfkrfepkffvpdhrtghpthhtohepnhgvfihlihgssehsohhurhgtvgifrghrvgdrohhrgh X-Hornet-Score: 0 X-Hornet-Verdict: clean X-CNFS-Analysis: v=2.4 cv=Y8/IdBeN c=1 sm=1 tr=0 ts=6ade0221 cx=a_exe a=r0hZLurSj1mWMLFr4yjEpA==:117 a=8EVYWzvmBU0A:10 a=20KFwNOVAAAA:8 a=cexeJ9FrIgKh0mxTcAUA:9 a=QEXdDO2ut3YA:10 a=DkUFmaY7EWAND-vUJHAA:9 a=-8QGq94pGciQEeXhm2f7:22 Date: Sun, 25 Oct 2026 14:20:32 +0100 (CET) From: Carlo Bramini <carlo.bramix@libero.it> To: "newlib@sourceware.org" <newlib@sourceware.org> Message-ID: <32109287.1186111.1792934432806@mail1.libero.it> Subject: [PATCH] math.h: remove the log2() macro MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_1186110_709915904.1792934432806" X-Priority: 3 Importance: Normal X-Mailer: Open-Xchange Mailer v8.35.159 X-Originating-IP: 5.77.70.252 X-Originating-Port: 47980 X-Originating-Client: open-xchange-appsuite x-libjamsun: 7mwHGIWbEp//WvASn0JMhbUR1FC2vt00 x-libjamv: /sJQ0yRQHAc= X-CMAE-Envelope: MS4xfM/zmeL0eZMxSzLVPryYvEH60SywyM+esIr20hAbtb9tbQvPYUPKrB4M3NrJ2n4Q6OHK8DIv3GtRtKHBlov2zLLRpW9FY5fPHJ7MGjQhseR5DO1GXAxK HL5VVLz0Eb7xyk8WnkSNpO3wM9G9ZZGYOkxmKF9PHkyXJuZYkYXrwBfO9zE1mVIPW+t+W7ECZridTRvv1LcC27Mydow1hgKTELUVanbFvcZngVl/zefJfsDq ZZMZHl3DwSDOtNiiPa2nnvwFSHHIHgW0l6zzvIaGIXWCdKoEXNVMIEyW2A9MRD0qH/s9/SU7DGiosiM62jkunA== X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_PBL, SPF_HELO_PASS, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Newlib mailing list <newlib.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/newlib>, <mailto:newlib-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/newlib/> List-Post: <mailto:newlib@sourceware.org> List-Help: <mailto:newlib-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/newlib>, <mailto:newlib-request@sourceware.org?subject=subscribe> Errors-To: newlib-bounces~patchwork=sourceware.org@sourceware.org |
| Series |
math.h: remove the log2() macro
|
|
Commit Message
Carlo Bramini
Oct. 25, 2026, 1:20 p.m. UTC
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:
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.
--- 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) */