Bug 7546 – 64-bit floating-point issue with negative zero: -0.0 == 0.0 is false.
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
All
Creation time
2012-02-19T11:15:00Z
Last change time
2012-05-14T01:27:19Z
Keywords
wrong-code
Assigned to
nobody
Creator
kennytm
Comments
Comment #0 by kennytm — 2012-02-19T11:15:03Z
Test case:
----------------------
void main()
{
auto p = -0.0;
assert(p == 0);
}
----------------------
Running the program with -m64 caused an Assertion failure. (This works in 32-bit.)
An equivalent test which also fails in 64-bit:
----------------------
void main()
{
double p = -0.0;
assert(p == -0.0);
}
----------------------
Comment #1 by kennytm — 2012-02-19T11:38:41Z
The problem is that, when comparing with 0.0 or -0.0, the backend will generate an integer 'cmp' instruction, but -0.0 has a different bit pattern than 0.0, so the equality will fail.
cmp QWORD PTR [rbp-0x8],0x0
je ...
The code works if the type is 'float' because it uses an 'add eax, eax' trick make both 0.0f and -0.0f set the ZF flag.
This code also work if the type if 'real' because it uses x87.
Comment #2 by bearophile_hugs — 2012-02-19T12:05:22Z
I think 0.0 == -0.0, but 0.0 !is -0.0.
Comment #3 by github-bugzilla — 2012-05-11T19:02:54Z