4 ms·
Here's an interesting anecdote for -ffast-math: the Kahan summation algorithm [1] can be used to reduce numerical errors in sums. Now have a look at the codege
by danieljh 10y ago
Here's an interesting anecdote for -ffast-math: the Kahan summation algorithm [1] can be used to reduce numerical errors in sums.
Now have a look at the codegen [2] without and with -ffast-math.
With -ffast-math the compiler rewrites the Kahan summation algorithm to a simple sum, completely screwing the code's semantic (since we told the compiler to do so!). Keep this in mind.
1 https://en.wikipedia.org/wiki/Kahan_summation_algorithm https://en.wikipedia.org/wiki/Kahan_summation_algorithm
2 https://godbolt.org/g/uWZgW2 https://godbolt.org/g/uWZgW2
- lfowles 10y agoI've also been bit pretty hard by not realizing -ffast-math turns on -ffinite-math-only... which optimizes out nan and inf checks :\
- gok 10y agoMe too. Worse, at least for as recent of clang and GCC that I've tested, it doesn't warn when you do things that -ffinite-math-only makes tautological. So "if (x!=x)" happily compiles to nothing without warning when x is a float. (You get a warning if using integers)
- lfowles 10y agoSame for actual calls, I suspected x!=x could disappear silently but didn't think isfinite(x) would be optimized out.
- deleted 10y ago[deleted]