https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121233
Bug ID: 121233
Summary: double equal comparison 32bit g++
Product: gcc
Version: 13.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: volconscious at gmail dot com
Target Milestone: ---
Hello,
I observed a strange behavior of double (also float) comparison for the 32bit
version of gcc/g++ on windows. I would expect that this assertion failed should
not happen.
Strange is it only happens with 32bit gcc/g++, not 64bit and only when I use
-O0 or no optimization. Thanks!
#include <cassert>
double testDiv(int a, int b)
{
return (double) a/b;
}
int main()
{
assert(testDiv(1, 3) == testDiv(1, 3));
}
C:\Src\testDiv>g++ testDiv.cpp -Wall -Wextra -o TestDiv.exe
C:\Src\testDiv>TestDiv.exe
Assertion failed: testDiv(1, 3) == testDiv(1, 3), file testDiv.cpp, line 10
C:\Src\testDiv>g++ --version
g++ (Rev5, Built by MSYS2 project) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
C:\Src\testDiv>gcc --version
gcc (Rev5, Built by MSYS2 project) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
> generated assembly
0x1214fd push %ebp
0x1214fe mov %esp,%ebp
0x121500 and $0xfffffff0,%esp
0x121503 sub $0x20,%esp
0x121506 call 0x121610 <__main>
0x12150b movl $0x3,0x4(%esp)
0x121513 movl $0x1,(%esp)
0x12151a call 0x1214f0 <testDiv(int, int)>
0x12151f fstpl 0x18(%esp)
0x121523 movl $0x3,0x4(%esp)
0x12152b movl $0x1,(%esp)
0x121532 call 0x1214f0 <testDiv(int, int)>
0x121537 fldl 0x18(%esp)
0x12153b fucomi %st(1),%st
0x12153d jp 0x121547 <main()+74>
0x12153f fucomip %st(1),%st
0x121541 fstp %st(0)
0x121543 je 0x121569 <main()+108>
0x121545 jmp 0x12154b <main()+78>
0x121547 fstp %st(0)
0x121549 fstp %st(0)
0x12154b movl $0xa,0x8(%esp)
0x121553 movl $0x124044,0x4(%esp)
0x12155b movl $0x124060,(%esp)
0x121562 mov 0x127160,%eax
0x121567 call *%eax
0x121569 mov $0x0,%eax
0x12156e leave
0x12156f ret
> $esp+0x18
0x5fff18: 55 55 55 55 55 55 d5 3f|c4 0c f8 00 25 00 00 00 UUUUUUÕ?Ä.ø.%...
> st0
0.333333333333333333342 (raw 0x3ffdaaaaaaaaaaaaaaab)