[php-src] PR #25608: Fix DFA pass turning a float + 0.0 into a copy that keeps -0.0
| From: | ChrisAbner | Date: | Thu, 19 Nov 2026 18:31:25 +0000 |
| Subject: | [php-src] PR #25608: Fix DFA pass turning a float + 0.0 into a copy that keeps -0.0 | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-40829@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25608
Author: ChrisAbner
The DFA pass replaces
$x + 0.0 and $x - -0.0 with a copy of
$x when $x is a float. That is wrong for -0.0: -0.0 +
0.0 is 0.0, but the copy keeps -0.0. $x + 0 is
affected too, because the int 0 is turned into 0.0 first.
Adding zero is the usual way to drop the -0 that round() can leave, and
with OPcache it no longer works:
```php
function amount(float $v): float { return round($v, 2) + 0.0; }
var_dump(amount(-0.004)); // float(0) without OPcache, float(-0) with it
```
PhpSpreadsheet does this in Formatter.php ("avoid weird '-0'
problem"). With OPcache, a -0.0 value with the 0% format is shown as
-0%.
The fix keeps the rewrite only when it is exact: ADD with -0.0 and
SUB with 0.0, checked with signbit(). Ints are unchanged. So
$x - 0.0 and $x + -0.0 are still turned into a copy, and only $x +
0.0 stays an addition. I did not remove the float case completely, because those two forms
are correct and would lose the optimization for no reason. This is the same family as GH-24063,
where SCCP treated 0.0 and -0.0 as the same value.
Introduced by 3024d9c56d in 8.0.0, confirmed by bisect. Reproduced on 8.0.30, 8.1.34, 8.4.28, 8.5.13
and 8.6.0. The patch applies cleanly on PHP-8.5, PHP-8.6 and master.
**Verification**
| Case, with $x = -0.0 | Without OPcache | Before | After |
|---|---|---|---|
| $x + 0, 0 + $x, $x + 0.0, $x - -0.0 |
float(0) | float(-0) | float(0) |
| round(-0.004, 2) + 0.0 | float(0) | float(-0) |
float(0) |
| $x - 0.0, $x + -0.0 (already correct) | float(-0) |
float(-0) | float(-0) |
The new test ext/opcache/tests/opt/add_sub_zero_signed_zero.phpt fails without the fix
and passes with it, without the JIT, with tracing and with function JIT.
On PHP-8.4, Zend/tests passes with and without OPcache. ext/opcache/tests
has the same failures as the base commit in my environment (gh8739 and
gh17733, plus two preload_restart_* tests with tracing). They restart
OPcache or start a sub-process. On master, Zend/tests and
ext/opcache/tests pass.
Not covered: I looked for the same mistake in the rest of the optimizer. block_pass.c
and sccp.c only fold an addition when both sides are constants, so they compute the
real result. I found no other place that treats 0.0 and -0.0 as the same.