Hi internals,
While working on 64-bit integers for 32-bit platforms [1], I found floating point results that differ only on 32-bit x86. I would like to reduce the platform differences where PHP is running as much as possible.
Why i386 differs from amd64, arm64 and arm32
On amd64 (SSE2), arm64 and arm32 (VFP), the compiler computes double arithmetic in IEEE-754 binary64: 53-bit mantissa, 11-bit exponent, one rounding per operation. On i386, compilers use the x87 FPU by default. Its registers have a 64-bit mantissa and a 15-bit exponent, and values are rounded to double only when they are stored to memory.
To get closer to binary64, zend_init_fpu() sets the x87 precision to 53 bits for every request (the XPFPA macros in Zend/zend_float.h). That leaves two differences from the other three platforms:
- libm runs with the reduced precision too. glibc's i386
pow(),exp(),expm1(),sinh()etc. compute on the x87 unit and need its extended precision to round their result correctly. Inside PHP they lose it, so they are much less accurate than the same functions outside PHP. - Only the mantissa is reduced, not the exponent. A result in the subnormal range (below PHP_FLOAT_MIN) is rounded once in the wide register and again when stored, and can end up one ULP off.
In both cases i386 is less accurate compared to the other platforms.
Part 1: libm accuracy -- XPFPA wrappers
#23578 pow(), ** and fpow() https://github.com/php/php-src/pull/23578 #23579 exp() https://github.com/php/php-src/pull/23579 #23581 expm1() https://github.com/php/php-src/pull/23581 #23582 sinh() https://github.com/php/php-src/pull/23582 Examples on i386 today:
var_dump(10 ** -2); // float(0.010000000000000002) vs float(0.01) var_dump(10 ** -2 === 0.01); // false on i386, true on amd64/arm64 var_dump(exp(10)); // float(22026.465794806725) vs ...718
Each PR adds a small inline helper to Zend/zend_float.h (zend_pow(), zend_exp(), zend_expm1(), zend_sinh()). It switches the FPU to extended precision for the libm call and rounds the result back to double. Where XPFPA_HAVE_CW is 0 -- amd64, arm64, arm32 -- the helper compiles to the plain libm call, so nothing changes there.
With the PRs, nearly all results I checked on i386 are correctly rounded, compared to roughly one in ten before. It is not a guarantee: a few inputs stay wrong, and one gets 1 ULP worse. Each PR's test contains inputs that are wrong on i386 today and correct with the change.
Cost: two fnstcw/fldcw pairs per call. Measured on an x86_64 Linux host running the i386 build (-m32 -msse2), JIT off. Each function was called 1,000,000 times in a loop, repeated 15 times, and the median time per call is used: function cost added per call
exp() ~5-7 ns (~5% of the libm call) expm1() ~5-7 ns
sinh() ~4 ns
**, fpow() ~3-8 ns
JIT should not need any changes: as far as I can see it has no implementation of its own for these functions or for **. It calls the regular function handlers, and ** goes through pow_function() and from there zend_pow(). Compile-time evaluation calls the same handlers. Is this assumption correct, or is there a path I missed?
Part 2: subnormal double rounding -- SSE2 instead of x87
The wrappers can't fix the second problem, as it happens in PHP's own arithmetic: $a = 5.678650331314532e-158;
$b = 2.3715405351663569e-153;
var_dump($a * $b);
// i386 (x87): 1.34671494457485E-310 // amd64, arm64, arm32: 1.3467149445748E-310 (correctly rounded)
Normal-range results are identical; only some results below PHP_FLOAT_MIN differ. Compiling with -msse2 -mfpmath=sse makes i386 compute in binary64 like the other platforms and removes the difference.
Unlike the other SIMD code in php-src, which falls back to scalar code or picks an implementation at run time, the floating point unit is fixed at compile time and crashes with SIGILL on a CPU without SSE2.
This doesn't make Part 1 unnecessary: zend_init_fpu() still reduces the x87 precision, and the system's i386 libm still runs on x87.
The PRs are open for some time already without any comments - comments welcome! Can we enable -msse2 -mfpmath=sse by default on x86 (require SSE2). WDYT?
Thanks,
Marc Bennewitz
On Sun, Nov 8, 2026, at 14:17, marc@mabe.berlin wrote:
Can we enable
-msse2 -mfpmath=sseby default on x86 (require SSE2).
Yes. As I understand it we are not really interested in supporting actual 32 bit hardware, but running 32 bit builds on WASM or to save memory. So breaking compatibility with old CPUs seems fair.
I think Windows already requires SSE2?
Regards,
Sjoerd Langkemper
Quoted text · 4 lines
On 8.11.2026 15:25 CET Sjoerd Langkemper sjoerd-php@linuxonly.nl wrote:
On Sun, Nov 8, 2026, at 14:17, marc@mabe.berlin mailto:marc@mabe.berlin wrote:
Can we enable
-msse2 -mfpmath=sseby default on x86 (require SSE2).Yes. As I understand it we are not really interested in supporting actual 32 bit hardware, but running 32 bit builds on WASM or to save memory. So breaking compatibility with old CPUs seems fair.
I have opened a PR to enable SSE2 and fpmath=sse on x86 by default without configure option. https://github.com/php/php-src/pull/25362
I think Windows already requires SSE2?
Windows builds should use SSE2 via --enable-native-intrinsics=sse2 and should enable it for double arithmetic. I do not have windows to verify this.
Regards,
Sjoerd Langkemper
Quoted text · 6 lines
On 8.11.2026 15:25 CET Sjoerd Langkemper sjoerd-php@linuxonly.nl wrote:
Can we enable
-msse2 -mfpmath=sseby default on x86 (require SSE2).Yes. As I understand it we are not really interested in supporting actual 32 bit hardware, but running 32 bit builds on WASM or to save memory. So breaking compatibility with old CPUs seems fair.
I have opened a PR to enable SSE2 and fpmath=sse on x86 by default without configure option. https://github.com/php/php-src/pull/25362
I think Windows already requires SSE2?
Windows builds should use SSE2 via
--enable-native-intrinsics=sse2and should enable it for double arithmetic. I do not have windows to verify this.
I didnt see this maillist come, but wouldn't it be better to default to the AMD64_v2 instruction set.
Starting last year, linux distro's started setting this as the minimum requirement for their distributions.
This equates to the following extensions:
- CMPXCHG16B
- LAHF-SAHF
- POPCNT
- SSE3
- SSE4.1
- SSE4.2
- SSSE3
SSSE3 being released in 2006 - 20 years ago.
Hi fennic,
Quoted text · 17 lines
On 22.11.2026 18:18 CET fennic log fenniclog@gmail.com wrote:
On 8.11.2026 15:25 CET Sjoerd Langkemper <sjoerd-php@linuxonly.nl mailto:sjoerd-php@linuxonly.nl> wrote:
On Sun, Nov 8, 2026, at 14:17, marc@mabe.berlin mailto:marc@mabe.berlin wrote:
Can we enable
-msse2 -mfpmath=sseby default on x86 (require SSE2).Yes. As I understand it we are not really interested in supporting actual 32 bit hardware, but running 32 bit builds on WASM or to save memory. So breaking compatibility with old CPUs seems fair.
I have opened a PR to enable SSE2 and fpmath=sse on x86 by default without configure option. https://github.com/php/php-src/pull/25362
I think Windows already requires SSE2?
Windows builds should use SSE2 via
--enable-native-intrinsics=sse2and should enable it for double arithmetic. I do not have windows to verify this.I didnt see this maillist come, but wouldn't it be better to default to the AMD64_v2 instruction set. Starting last year, linux distro's started setting this as the minimum requirement for their distributions. This equates to the following extensions:
- CMPXCHG16B
- LAHF-SAHF
- POPCNT
- SSE3
- SSE4.1
- SSE4.2
- SSSE3
SSSE3 being released in 2006 - 20 years ago.
This thread (and the PR) is about 32-bit x86 builds only. "AMD64_v2 instruction set" is 64-bit where doubles get compute with SSE2 by default anyway and none of them would enable fpmath=sse on 32-bit on it's own.
We could think about enabling other instruction sets as well but this should be discussed on it's own thread.
Marc Bennewitz
This thread (and the PR) is about 32-bit x86 builds only. "AMD64_v2 instruction set" is 64-bit where doubles get compute with SSE2 by default anyway and none of them would enable fpmath=sse on 32-bit on it's own.
We could think about enabling other instruction sets as well but this should be discussed on it's own thread.
Ahh ok, well didn't mean to derail the convo, but maybe this pull request will fall short as I said linux distributions require AMD_64. Debian, Ubuntu, suse, redhat and many linux distros no longer support 32bit in 2025.
The linux kernel is also having active discussions to remove 32bit support from the kernel. 7.3 has already dropped support for arm32 with ongoing discussions and development to drop 32bit and microsoft has announced they will start removing 32bit support next year.
Would seem a waste of effort to add extra support for 32bit while the industry and the operating systems PHP run on are actively in the process of not supporting 32bit.