[llvm-dev] fenv.h vs the optimizer

Will via llvm-dev llvm-dev at lists.llvm.org
Thu Aug 18 01:12:35 PDT 2016


Howdy all,

I've been playing around with programs that use the C11 fenv.h.

It seems that, currently, the LLVM compiler does not regard to the 
exception-flag side-effects of floating point operations?

When run on my macbook, the example code on 
http://en.cppreference.com/w/c/numeric/fenv/FE_exceptions does not print 
all the expected exceptions.

Other examples:

void foo() {
     fesetround(FE_DOWNWARD);
     printf("foo downward: %f\n", rint(0.5));
     fesetround(FE_UPWARD);
     printf("foo upward: %f\n", rint(0.5));
}

If compiled with optimization, only one call to rint() is made and the 
result is reused.

void bar(double a, double b) {
     feclearexcept(FE_INEXACT);
     a / b;
     printf("bar %f / %f is %sexact\n", a, b, fetestexcept(FE_INEXACT)? 
"in": "");
}

The compiler omits the divide as the result is unused.  And so on.

Presumably this has never worked?  And perhaps LLVM is no worse than 
other compilers in this regard?

Is there any appetite to fix it?  What kind of changes would need to be 
made?

Thx,

Will



More information about the llvm-dev mailing list