Bug 110564 - Incorrect results from floating point computations on x86 when optimized
Summary: Incorrect results from floating point computations on x86 when optimized
Status: RESOLVED DUPLICATE of bug 323
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 9.4.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-07-05 19:17 UTC by D. Richard Hipp
Modified: 2023-07-05 23:05 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
bug.c - compile and run to demonstrate the problem (506 bytes, text/x-csrc)
2023-07-05 19:17 UTC, D. Richard Hipp
Details

Note You need to log in before you can comment on or make changes to this bug.
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.