Search code examples
cgccoptimizationfloating-point

What kind of optimizations are included in -funsafe-math-optimizations?


GCC's man page states that -funsafe-math-optimizations allows for optimizations that "(a) assume that arguments and results are valid and (b) may violate IEEE or ANSI standards", but that's not very precise, is it?

What could an "invalid" argument be in this case? NaNs? Infinites? Subnormals? Negative numbers to sqrt()?

How far are results allowed to deviate from IEEE or ANSI standards? Is it "merely" stuff like operation associativity and ordering, or might it include eg. true comparisons with NaNs or incorrect comparisons with infinites? Could a stored variable be re-rounded after already being used (such that, for variables x and y, (x == y) + (x == y) could evaluate to 1)? Could isinf()/isnan() stop working?

Do the GCC devs follow any particular system or discipline with regards to such questions, or could the answer differ wildly from version to version?


Solution

  • According to gcc.gnu.org (my bold):

    This mode enables optimizations that allow arbitrary reassociations and transformations with no accuracy guarantees. It also does not try to preserve the sign of zeros.

    That includes your mention of associative reordering, as well as "Built-in functions [which] have names such as __builtin_sqrt" being applied when they "may have less precision or be restricted to a smaller domain"