Bug #81627 [Com]: Incorrect result of php bitwise and with floats using clang13 -O2
Edit report at https://bugs.php.net/bug.php?id=81627&edit=1
ID: 81627
Comment by: shaohua dot li at inf dot ethz dot ch
Reported by: shaohua dot li at inf dot ethz dot ch
Summary: Incorrect result of php bitwise and with floats
using clang13 -O2
Status: Open
Type: Bug
Package: *General Issues
Operating System: Ubuntu 20.04.3 LTS
PHP Version: 8.1Git-2021-11-16 (Git)
Block user comment: N
Private report: N
New Comment:
Yes, I noticed the warning. I'm just worried that shouldn't all compilers/optimizations
emit consistent results even if it's an error?
I also tried gcc11 with -O0 and -O2, on which php emits the same results as clang13 -O2.
Previous Comments:
------------------------------------------------------------------------
[2021-11-17 10:32:20] requinix@php.net
I assume you do *not* get the float-to-int deprecation warning when $n=58? PHP doesn't support
bitwise AND with floats and will round them to ints, but that comes with branch prediction so -O2
may be running afoul of that.
And that there is the limit of my knowledge on this matter.
------------------------------------------------------------------------
[2021-11-17 08:53:03] shaohua dot li at inf dot ethz dot ch
Hi,
Even if I decouple the two operations into two statements, the issue still exists. Also, for the
robustness, correctness, and consistency of php, the outputs should be the same.
Test script:
----------------
<?php
function test() {
$n = 0;
$a = 0;
while($a <= 0) {
$a &= $a + $a;
$a--;
if (++$n > 59) die("bug\n");
}
}
test();
?>
------------------------------------------------------------------------
[2021-11-16 23:24:03] requinix@php.net
> $a &= $a-- + $a;
That statements has two modifications to $a: the -- and the &=. PHP does not guarantee in what
order those will happen.
https://en.wikipedia.org/wiki/Sequence_point
------------------------------------------------------------------------
[2021-11-16 15:49:06] shaohua dot li at inf dot ethz dot ch
Description:
------------
Hi there,
I compiled php-src twice with clang13 -O0 and -O2 (default). However, for the following code sample,
the two ./sapi/cli/php would evaluate it differently.
For "clang13 -O0" compiled one, "bug" would be printed. However, "clang13
-O2" wouldn't.
Test script:
---------------
<?php
function test() {
$n = $a = 0;
while($a <= 0) {
$a &= $a-- + $a;
if (++$n > 59) die("bug\n");
}
}
test();
?>
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=81627&edit=1
Thread (10 messages)