[cfe-dev] fast-math, clang-6+ and floating point exceptions

Nicholas Devenish via cfe-dev cfe-dev at lists.llvm.org
Tue Jul 30 15:28:10 PDT 2019

I'm working on scientific code, that  explicitly catches and crashes
on floating point exceptions (I've seen several such projects in the
past in the scientific domain do this). *Everything* is compiled with
-ffast-math and -O3. However, we've got this code in a module:

    float w = std::log(x);
    if (x > 3) {
        w -= std::log(w);

Godbolt: https://godbolt.org/z/yvGqjl

Basically, if I'm reading the assembly output properly, it looks like
clang > 6 optimizes this into assuming that the second log is almost
always run (not unreasonably), always calls log twice, and eliminates
the branch. However, in our code, the second log causes an FPE which
jumps to a crash handler.

My questions are:
- Am I correct in my interpretation of what's going on here?
- Is this a reasonable thing for clang to do - it's not implied by any
of the cases on
https://clang.llvm.org/docs/UsersManual.html#cmdoption-ffast-math ,
although to be fair that does say it's a non-inclusive list. GCC
_doesn't_ seem to make this transform, though obviously that doesn't
mean that it's a bad one
- Can I prevent this from happening, even with -ffast-math enabled?
- Is this sort of configuration sensible or insane?



More information about the cfe-dev mailing list