https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102129
Bug ID: 102129 Summary: -ftrapping-math is broken or badly documented Product: gcc Version: 11.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: vincent-gcc at vinc17 dot net Target Milestone: --- Testcase: void g (void); double f (double x) { double r = x * 5.0; g (); return r; } In the generated code with -O1 and above, one can see that the multiplication is done after g is called, even if -ftrapping-math is used (which is the default). This is incorrect as x * 5.0 may trap, terminating the program with a floating-point exception, in which case g must not be executed. There is a similar discussion in PR29186 (which is actually about flag reading), and -ftrapping-math is also mentioned in PR34678 ("I suspect that if -ftrapping-math actually implemented everything required for the floating-point exceptions aspects of FENV_ACCESS, [...]"). The STDC FENV_ACCESS pragma is not implemented, but by definition of -ftrapping-math, it should imply some behavior similar to this pragma being on. This bug is different from PR34678 because not everything from this pragma needs to be implemented, just a subset of the "when a program might [...] run under non-default floating-point control modes." part. This seems to be the whole purpose of this option. Note also that https://gcc.gnu.org/wiki/FloatingPointMath doesn't mention a limitation for traps (only for the change of the rounding mode). For the reference, the current documentation: -fno-trapping-math Compile code assuming that floating-point operations cannot generate user-visible traps. These traps include division by zero, overflow, underflow, inexact result and invalid operation. This option requires that -fno-signaling-nans be in effect. Setting this option may allow faster code if one relies on “non-stop” IEEE arithmetic, for example. This option should never be turned on by any -O option since it can result in incorrect output for programs that depend on an exact implementation of IEEE or ISO rules/specifications for math functions. The default is -ftrapping-math. (from https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html).