News for hackers. Stuff that matters.
Re:GCC optimized code gives strange floating point results by acqq on Sunday February 26, 2017 @05:10PM (#13740283)
And I believe he's right, if he was asked specifically what that program should do. If all the statements in the program are really just:

    #include <stdio.h>

    void test(double x, double y)
    {
      const double y2 = x + 1.0;
      if (y != y2) printf("error\n");
    }

    void main()
    {
      const double x = .012;
      const double y = x + 1.0;

      test(x, y);
    }
Then the comparison has to be done on doubles, and "but it's extended precision internally" mentioned in some answers is the wrong answer. All the variables in the program are stated to be doubles. If I'd write the x86 and x87 asm instructions I could easily write them so that they work as expected, even without SSE2.

However if you compile with some "special" optimizations that are documented to relax some specific code generation rules, then it can be OK that these specific optimizations behave differently, but then: you asked for it by choosing that documented optimization.

In the answers there, Richard Henderson wrote 2001-01-16 06:30:04 UTC:

"You are seeing the results of excess precision in the FPU. Either change the rounding precision in the FPCR, or work around the problem with -ffloat-store."

He's also right, if the -ffloat-store is documented to be turned off by -O and if it's documented that that flag also affects the precomputed calculations that are done in the compile time. If the second detail is not documented, the results could be unexpected, but again depends of what you expect from the optimizer. The properly written compiler and optimizer should perform the calculations during the code generation correct to the definition and not cut the corners. If the generated code was generated to actually perform the != during the run time, then the compile-time calculations are irrelevant, but the sane default should be something like -ffloat-store when not using SSE2 even with -O, if -O has the semantic "the optimizations that should not produce really unexpected effects." It sounds complicated, but it has its reasons.

P.S. answer to nsajko's post below: I mention -ffloat-store not ffast-math. Read the description of ffloat-store please:

https://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Optimize-Options....

and also "Note on x86 and m68080 floating-point math" here https://gcc.gnu.org/wiki/FloatingPointMath

Re:GCC optimized code gives strange floating point results by kccqzy on Monday February 27, 2017 @02:20AM (#13742487)
With -O2 gcc actually compiles the program to

    xorl %eax, %eax
    ret
That is, it optimized away everything.
Re:GCC optimized code gives strange floating point results by acqq on Monday February 27, 2017 @08:17AM (#13743925)
Yes, that's the correct optimization. That code obviously can't perform printf("error\n") to manifest the bug, and obviously both additions were made compile-time producing the results that are equal.
Re:GCC optimized code gives strange floating point results by caf on Sunday February 26, 2017 @09:48PM (#13741465)
The relevant option is actually -fexcess-precision=standard, which is implied by -std=c99.
Re:GCC optimized code gives strange floating point results by nsajko on Sunday February 26, 2017 @05:31PM (#13740399)
Nonstandard compiler options (ffast-math) are not relevant to this bug.