diff mbox series

[1/3] musl: Use FENV_ACCESS pragma instead of maytrap exception behavior

Message ID 20261111083711.549321-1-khem.raj@oss.qualcomm.com
State New
Headers show
Series [1/3] musl: Use FENV_ACCESS pragma instead of maytrap exception behavior | expand

Commit Message

Khem Raj Nov. 11, 2026, 8:37 a.m. UTC
Upstream review [1] asked whether the real problem is a missing
#pragma STDC FENV_ACCESS ON in the affected sources. It is: with the
pragma in expm1.c, expm1f.c, hypot.c and hypotf.c, clang stops
speculating the floating point operations that raise the spurious
exceptions, and libc-test math/expm1, expm1f, hypot, hypotf and sinh
pass.

Tested natively on aarch64 with clang 23.1.3 and gcc 14.2 for
generic, cortex-a57, cortex-a72 and neoverse-n1: those tests now pass
with both compilers, and gcc builds are unchanged. The remaining clang
only math failure, yn, comes from fma contraction and is already
deleted from the libc-test ptest.

Replace the configure flag with the source change.

[1] https://www.openwall.com/lists/musl/2026/10/28/2

Signed-off-by: Khem Raj <raj.khem@gmail.com>
AI-Generated: Uses Claude Code
---
 ...p-exception-behavior-maytrap-when-su.patch | 46 ------------
 ..._ACCESS-in-expm1-expm1f-hypot-and-hy.patch | 73 +++++++++++++++++++
 meta/recipes-core/musl/musl_git.bb            |  2 +-
 3 files changed, 74 insertions(+), 47 deletions(-)
 delete mode 100644 meta/recipes-core/musl/musl/0004-configure-use-ffp-exception-behavior-maytrap-when-su.patch
 create mode 100644 meta/recipes-core/musl/musl/0004-math-enable-FENV_ACCESS-in-expm1-expm1f-hypot-and-hy.patch
diff mbox series

Patch

diff --git a/meta/recipes-core/musl/musl/0004-configure-use-ffp-exception-behavior-maytrap-when-su.patch b/meta/recipes-core/musl/musl/0004-configure-use-ffp-exception-behavior-maytrap-when-su.patch
deleted file mode 100644
index dfff81299b..0000000000
--- a/meta/recipes-core/musl/musl/0004-configure-use-ffp-exception-behavior-maytrap-when-su.patch
+++ /dev/null
@@ -1,46 +0,0 @@ 
-From fe310a5e2fe6be3c28f800bde383d7df5aac4b49 Mon Sep 17 00:00:00 2001
-From: Khem Raj <khem.raj@oss.qualcomm.com>
-Date: Sun, 25 Oct 2026 21:54:26 -0700
-Subject: [PATCH] configure: use -ffp-exception-behavior=maytrap when supported
-
-The math library relies on floating point exception flags being raised
-exactly as the C code would raise them. GCC keeps that by default
-(-ftrapping-math), but clang assumes floating point exceptions are never
-inspected unless told otherwise, and may then evaluate both arms of a
-branch and select the result. Built with clang 23 for aarch64, expm1()
-computes both y*2.0*0x1p1023 and y*twopk for the k < 0 || k > 56 case
-and picks one with fcsel, so the unused multiply raises a spurious
-OVERFLOW. libc-test's math/expm1, expm1f, hypot, hypotf and sinh fail
-because of that.
-
--ffp-exception-behavior=maytrap tells clang to avoid transformations
-that raise exceptions the original code would not raise. GCC does not
-accept the option, so tryflag leaves GCC builds unchanged.
-
-Upstream-Status: Submitted [https://www.openwall.com/lists/musl/2026/10/28/1]
-Signed-off-by: Khem Raj <khem.raj@oss.qualcomm.com>
----
- configure | 10 ++++++++++
- 1 file changed, 10 insertions(+)
-
-diff --git a/configure b/configure
-index bc9fbe48..7e1e1b72 100755
---- a/configure
-+++ b/configure
-@@ -355,6 +355,16 @@ tryflag CFLAGS_C99FSE -fexcess-precision=standard \
- || { test "$ARCH" = i386 && tryflag CFLAGS_C99FSE -ffloat-store ; }
- tryflag CFLAGS_C99FSE -frounding-math
- 
-+#
-+# The math library relies on floating point exception flags being
-+# raised exactly as the C code would raise them. GCC does not move
-+# floating point operations that may trap across branches by default
-+# (-ftrapping-math), but clang assumes exceptions are never inspected
-+# unless told otherwise, and may then evaluate both sides of a branch,
-+# raising spurious exceptions (e.g. OVERFLOW from expm1, hypot, sinh).
-+#
-+tryflag CFLAGS_C99FSE -ffp-exception-behavior=maytrap
-+
- #
- # Semantically we want to insist that our sources follow the
- # C rules for type-based aliasing, but most if not all real-world
diff --git a/meta/recipes-core/musl/musl/0004-math-enable-FENV_ACCESS-in-expm1-expm1f-hypot-and-hy.patch b/meta/recipes-core/musl/musl/0004-math-enable-FENV_ACCESS-in-expm1-expm1f-hypot-and-hy.patch
new file mode 100644
index 0000000000..594e97a348
--- /dev/null
+++ b/meta/recipes-core/musl/musl/0004-math-enable-FENV_ACCESS-in-expm1-expm1f-hypot-and-hy.patch
@@ -0,0 +1,73 @@ 
+From 5009e7cdcd11c585f2bdebacf2fb4f8540508efe Mon Sep 17 00:00:00 2001
+From: Khem Raj <khem.raj@oss.qualcomm.com>
+Date: Tue, 10 Nov 2026 18:36:03 -0800
+Subject: [PATCH] math: enable FENV_ACCESS in expm1, expm1f, hypot and hypotf
+
+These functions rely on floating point exception flags being raised
+exactly as the C code would raise them, but without FENV_ACCESS the
+compiler may assume the flags are never inspected. Clang does, and
+speculates floating point operations across branches. Built with
+clang 23 for aarch64 (-mcpu=cortex-a57), expm1() computes both
+y*2.0*0x1p1023 and y*twopk for the k < 0 || k > 56 case and picks
+one with fcsel, so the unused multiply raises a spurious OVERFLOW.
+libc-test's math/expm1, expm1f, hypot, hypotf and sinh (via expm1)
+fail because of that.
+
+GCC does not speculate these operations, so GCC builds are unchanged.
+
+Upstream-Status: Submitted [https://www.openwall.com/lists/musl/2026/11/11/3]
+Signed-off-by: Khem Raj <khem.raj@oss.qualcomm.com>
+---
+ src/math/expm1.c  | 1 +
+ src/math/expm1f.c | 1 +
+ src/math/hypot.c  | 1 +
+ src/math/hypotf.c | 1 +
+ 4 files changed, 4 insertions(+)
+
+diff --git a/src/math/expm1.c b/src/math/expm1.c
+index ac1e61e..81d0c80 100644
+--- a/src/math/expm1.c
++++ b/src/math/expm1.c
+@@ -105,6 +105,7 @@
+  */
+ 
+ #include "libm.h"
++#pragma STDC FENV_ACCESS ON
+ 
+ static const double
+ o_threshold = 7.09782712893383973096e+02, /* 0x40862E42, 0xFEFA39EF */
+diff --git a/src/math/expm1f.c b/src/math/expm1f.c
+index 09a41af..8c7aa18 100644
+--- a/src/math/expm1f.c
++++ b/src/math/expm1f.c
+@@ -14,6 +14,7 @@
+  */
+ 
+ #include "libm.h"
++#pragma STDC FENV_ACCESS ON
+ 
+ static const float
+ ln2_hi      = 6.9313812256e-01, /* 0x3f317180 */
+diff --git a/src/math/hypot.c b/src/math/hypot.c
+index 6071bf1..0efc546 100644
+--- a/src/math/hypot.c
++++ b/src/math/hypot.c
+@@ -1,6 +1,7 @@
+ #include <math.h>
+ #include <stdint.h>
+ #include <float.h>
++#pragma STDC FENV_ACCESS ON
+ 
+ #if FLT_EVAL_METHOD > 1U && LDBL_MANT_DIG == 64
+ #define SPLIT (0x1p32 + 1)
+diff --git a/src/math/hypotf.c b/src/math/hypotf.c
+index 2fc214b..91fc086 100644
+--- a/src/math/hypotf.c
++++ b/src/math/hypotf.c
+@@ -1,5 +1,6 @@
+ #include <math.h>
+ #include <stdint.h>
++#pragma STDC FENV_ACCESS ON
+ 
+ float hypotf(float x, float y)
+ {
diff --git a/meta/recipes-core/musl/musl_git.bb b/meta/recipes-core/musl/musl_git.bb
index b53b4be84c..0be24b22f1 100644
--- a/meta/recipes-core/musl/musl_git.bb
+++ b/meta/recipes-core/musl/musl_git.bb
@@ -14,7 +14,7 @@  SRC_URI = "git://git.musl-libc.org/musl;branch=master \
            file://0001-Make-dynamic-linker-a-relative-symlink-to-libc.patch \
            file://0002-ldso-Use-syslibdir-and-libdir-as-default-pathes-to-l.patch \
            file://0003-tools-version.sh-fall-back-to-VERSION-when-git-descr.patch \
-           file://0004-configure-use-ffp-exception-behavior-maytrap-when-su.patch \
+           file://0004-math-enable-FENV_ACCESS-in-expm1-expm1f-hypot-and-hy.patch \
           "
 
 PROVIDES += "virtual/libc virtual/libiconv virtual/libintl virtual/crypt"