5

The -ffast-math C++ compiler option allows the compiler to perform more math optimizations that may slightly change behavior. For example, x * 10 / 10 should cancel but due to the possibility of overflow it would slightly change behavior, and x / 10.0 / 10.0 may have different rounding errors than x / 100.0.

However, as many resources have noted, including many questions here on StackOverflow, the -ffast-math C++ compiler option can result in strange behavior across platforms. Instead, the recommended approach is to manually add parentheses around the operations you want the compiler to optimize.

Is there a way to identify these parts of the codebase? Some kind of static analysis tool that can find all lines of code that would be different if -ffast-math were enabled, so that the programmer can manually adjust those lines of code to be optimized even without -ffast-math enabled?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Aaron Franke
  • 3,268
  • 4
  • 31
  • 51
  • 3
    You seem worried about unsafe math optimizations that may very slightly change the numerical results. But this is only one of the many effects of `-ffast-math`. That great answer lists all effects of the flag: https://stackoverflow.com/a/22135559 . To me, the most dangerous is the wrong handling of NaN and infinity, that may change completely the behavior of the program (and the reason why I didn't enabled it on our code base). And also the removal of `errno` variable would be harmful if you use it. – prapin Jun 21 '23 at 18:16
  • @prapin I am only interested in the optimization effects that I can replicate by adjusting my code. – Aaron Franke Jun 22 '23 at 06:19

0 Answers0