From 96c8af5d9f800eea2a2bbee12645a3bb1f10feef Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Fri, 4 Sep 2026 16:38:26 +0200 Subject: [PATCH 1/2] Add test for sinh() precision on a double-precision-clamped FPU On i386 the x87 FPU is the platform default and zend_init_fpu() clamps it to double precision (53-bit mantissa) for the whole request. libm's sinh() computes on the x87 unit and relies on the extended range to round correctly to double, so while that clamp is in place sinh() is off by one or more ULP. This matters because it makes sinh() silently architecture-dependent: the same script prints different floats on i386 than on x86-64 or arm64, comparisons against precomputed constants fail, and the error propagates into anything built on sinh(). Every expected value is the correctly rounded double, verified against a high-precision reference rather than against another platform, and written as the shortest decimal that round-trips back to it. All arguments are exact binary fractions. The first group is inputs that are already correct everywhere, so a failure points at the affected inputs rather than at the whole function. This test fails on i386 and passes wherever XPFPA_HAVE_CW is 0, which includes x86-64 and arm64. The following commit fixes i386. --- .../tests/math/sinh_fpu_precision.phpt | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 ext/standard/tests/math/sinh_fpu_precision.phpt diff --git a/ext/standard/tests/math/sinh_fpu_precision.phpt b/ext/standard/tests/math/sinh_fpu_precision.phpt new file mode 100644 index 000000000000..a646d388474d --- /dev/null +++ b/ext/standard/tests/math/sinh_fpu_precision.phpt @@ -0,0 +1,48 @@ +--TEST-- +sinh(): results must not lose precision when the FPU is clamped to double precision +--INI-- +serialize_precision=-1 +--FILE-- + +--EXPECT-- +-- reference points already correct on every platform -- +float(0) +float(1.1752011936438014) +float(12077476.376787629) +-- inputs the FPU clamp gets wrong -- +float(10.017874927409903) +float(74.20321057778875) +float(201.71315737027922) +float(548.3161232732465) +float(1490.4788257895502) +float(4051.54190208279) +float(11013.232874703393) +float(29937.07084924806) +float(221206.6960033301) +float(601302.1420819727) From 695cea113eb415050d3b2f878a3ecf591843118a Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Fri, 4 Sep 2026 16:38:26 +0200 Subject: [PATCH 2/2] Use XPFPA for sinh() Restore extended FPU precision around the libm sinh() call and truncate the result back to double. Why this matters: zend_init_fpu() clamps the x87 FPU to a 53-bit mantissa for the whole request so that PHP's own double arithmetic behaves like IEEE-754 binary64. glibc's sinh(), however, computes on the x87 unit and relies on the extended range to round correctly to double, so the clamp costs sinh() accuracy. The loss is observable from userland and makes sinh() silently architecture-dependent, which breaks float comparisons, cached or serialized results, and any computation that accumulates the error. Measured against a high-precision reference rounded to nearest double, over 720 sampled inputs: i386 before this change 75/720 correctly rounded (10.4%) i386 after this change 715/720 correctly rounded (99.3%) arm64, unchanged 601/720 correctly rounded (83.5%) Note that arm64 is itself not a correctly rounded baseline for sinh(), so these figures are stated against the high-precision reference rather than against another platform. This is an improvement, not a guarantee: sinh(67) regresses by 1 ULP, because the clamped result there is genuinely the correctly rounded one. The accompanying test therefore asserts only inputs that this change fixes and that are also correct on x86-64 and arm64. zend_sinh() in zend_float.h compiles down to a plain sinh() wherever XPFPA_HAVE_CW is 0, which includes x86-64 and arm64, so no other platform is affected. --- Zend/zend_float.h | 19 +++++++++++++++++++ ext/standard/math.c | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Zend/zend_float.h b/Zend/zend_float.h index 12aa02005c2a..133f58460c33 100644 --- a/Zend/zend_float.h +++ b/Zend/zend_float.h @@ -20,6 +20,8 @@ #include "zend_portability.h" +#include + BEGIN_EXTERN_C() /* @@ -413,4 +415,21 @@ END_EXTERN_C() #endif /* FPU CONTROL */ +/* zend_init_fpu() clamps the x87 FPU to double precision (53-bit mantissa) for + * the whole request. libm's sinh() computes on the x87 unit and relies on the + * extended range to round correctly to double, so while that clamp is in place + * its result is off by one or more ULP. Restore extended precision for the call + * and truncate the result back to double. Compiles down to a plain sinh() + * everywhere XPFPA_HAVE_CW is 0, which includes x86-64 and arm64. */ +static zend_always_inline double zend_sinh(double num) +{ +#if XPFPA_HAVE_CW + XPFPA_DECLARE + XPFPA_SWITCH_DOUBLE_EXTENDED(); + XPFPA_RETURN_DOUBLE(sinh(num)); +#else + return sinh(num); +#endif +} + #endif diff --git a/ext/standard/math.c b/ext/standard/math.c index e13f153f63e2..3e4f3678775d 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -535,7 +535,7 @@ PHP_FUNCTION(sinh) ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_DOUBLE(num) ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(sinh(num)); + RETURN_DOUBLE(zend_sinh(num)); } /* }}} */