Announcement

Collapse
No announcement yet.

GCC 6.1 Compiler Optimization Level Benchmarks: -O0 To -Ofast + FLTO

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #21
    Originally posted by float View Post
    So, by saying legal do you define optimisations that are allowed by the standard? In that case, what makes you think that the optimisations enforced by -ffast-math are not allowed by the C standard?
    Not getting into what it legal or not, but here is an example where -ffast-math would change the results.
    Suppose you want to calculate a^4 and you write the following fragment:

    Code:
    double a = ...;
    double x = a * a * a * a;
    without -ffast-math, the compiler would issue 3 multiplication instructions.

    Another way to calculate a^4 is this:
    Code:
    double a = ...
    double x = a * a;
    x = x * x;
    In this case, the compiler would issue 2 multiplication instructions.
    If you use -ffast-math, this is what the compiler would optimize the first code fragment to.

    You would think they would yield the same result, but they don't in some cases.
    Floating point numbers are not exact, they have limited precision, so each multiplication operation has to round the result a little bit.
    In the first fragment, each multiplication will have some rounding error.
    In the second fragment, the first multiplication will have some rounding error and it will be carried over into the second multiplication, which would have additional rounding error.
    Thus, the rounding errors will be slightly different. The 2 code fragments would produce slightly different results in the least significant bits.

    Most programs would not care about the minimal floating point rounding errors, and would be fine with this optimization, for those programs -ffast-math is appropriate.
    Last edited by paulpach; 16 May 2016, 02:19 PM.

    Comment


    • #22
      I just saw something when looking at it again, how come -O1 was faster than -O2, which was in turn faster than -O3? That's weird.

      Comment


      • #23
        Originally posted by chithanh View Post
        One example which actually violates C99 is that -funsafe-math-optimizations (implied by -ffast-math) contracts expressions to FMA instructions in cases where this is not allowed by the standard.
        This is an interesting observation I was not aware of. Would you mind explaining it a bit more or providing a specific example? Because to my knowledge until now -funsafe-math-optimizations only reduced IEEE 754 compatibility.

        Comment


        • #24
          C99 says that contracting expressions (such as FMA) are only allowed when there is a corresponding source level expression, and gcc admits that it does not always observe this:


          Some more to read:

          Some more discussion on when this is allowed and when not:
          https://software.intel.com/en-us/for...r/topic/579190 (applies to both gcc and icc)

          Last edited by chithanh; 16 May 2016, 06:51 PM.

          Comment


          • #25
            Enable it on a Javascript engine, and it stops working.
            This was true when javascript implementations were pure interpreters. Modern JIT implementations should be less sensitive to this since FP handling is mostly a function of the code emitted by the runtime rather than options used while building the runtime.
            Last edited by ormaaj; 23 May 2016, 11:00 PM.

            Comment

            Working...
            X