I'm new to this list, but I've been using Clang for a while (and C++ for
I've encountered a problem when compiling with -O1 on FreeBSD/amd64 11.1
(with bundled Clang, i.e. 4.0.0, as well as with 5.0.1 and 6.0.0 from
I asked on comp.lang.c++ and the code was also confirmed to misbehave on
Linux, so they suggested I post it here.
What follows is the minimal code which should produce the error; notice
something more convoluted is needed at -O3 (the original software still
crashes, but in this example, probably too much is optimized away).
$ clang++ -O1 a.cpp
Floating point exception (core dumped)
NOTE 1: enabling overflow exceptions on FP math will render the
instruction "B=A*2" illegal.
That instruction shouldn't be executed, if following the proper program
flow; probably the compiler still has the CPU compute this value
NOTE 2: inserting the commented statement makes the program behave
correctly again, evidently avoiding the execution of the second branch.
Please let me know if you think this is a bug in the compiler, if using
feenableexcept along with optimizations is considered bad practive or if
you need further info.
On 28 April 2018 at 15:32, Andrea Venturoli via llvm-dev
<[hidden email]> wrote:
> feenableexcept(FE_OVERFLOW); //**** NOTE 1
There are two problems here. One with the code and one with Clang.
First, in order to use these floating-point environment functions, the
C (and C++) language requires you to use "#pragma stdc fenv_access
on". Without that, the compiler is free to assume that exceptions
aren't possible and the default rounding mode prevails regardless of
the calls you make. And Clang does.
Unfortunately, the other half is the Clang bug: it doesn't actually
support that #pragma yet. It'll warn (yay!?) but won't actually make
your code work.
One final silver lining is that there's actually a reasonably active
effort to start supporting the pragma going on now. We've got most of
a plan, bits are implemented, and people are working on it. Now that
you know to search for the keyword "fenv_access" you should be able to
find lots more discussion on these lists. Though probably not a
committed timeline, I'm afraid.