Bug 110564

Summary: Incorrect results from floating point computations on x86 when optimized
Product: gcc Reporter: D. Richard Hipp <drh>
Component: targetAssignee: Not yet assigned to anyone <unassigned>
Severity: normal CC: dkm, ebotcazou
Priority: P3    
Version: 9.4.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:
Attachments: bug.c - compile and run to demonstrate the problem

Description D. Richard Hipp 2023-07-05 19:17:57 UTC
Created attachment 55484 [details]
bug.c - compile and run to demonstrate the problem

Simplified test case attached.  Incorrect floating point result on Linux x86 when compiling and running as follows:

    gcc bug.c -m32 -O1 && ./a.out

The problem goes away under any one or more of the following conditions:

  *  Omit the -m32 flag (to generate x86_64 code)
  *  Omit the -O flag or use -O0
  *  Add the -ffloat-store option
  *  Uncomment both of the "printf()" statements in the test case

Please advise if I am doing something wrong (undefined or unspecified behavior) so that I might fix it.

SQLite bug report at https://sqlite.org/forum/forumpost/ee7278611394034c.  A work-around has been checked into SQLite at https://sqlite.org/src/info/5d9e9364808793d6.  Please advise if there is a better work-around.
Comment 1 Andrew Pinski 2023-07-05 19:22:47 UTC
Either use -fexcess-precision=standard or -std=c99 .

Basically this is a dup of bug 323.

*** This bug has been marked as a duplicate of bug 323 ***
Comment 2 Andrew Pinski 2023-07-05 19:25:08 UTC
>undefined or unspecified behavior

Neither. Basically GCC uses excess-precision for the fpu as supplied by x87 (80bit IEEE FPU).  Anyways read bug 323 and all of the specifications around FP math and more.
Comment 3 Eric Botcazou 2023-07-05 23:05:08 UTC
Yes, everybody should use -mfpmath=sse these days on x86.