floating point exception flags and compiler optimization

Richard B. Kreckel richard.kreckel@areva.com
Fri Sep 22 09:09:00 GMT 2006


I just tried playing with the floating point exceptions using the
<fenv.h> functionality defined in C99. However, it seems to interfere
somehow with optimization. Can anyone give a hint what's going on?
Here is my test program:

$ cat fenv.c
#include <fenv.h>
#include <stdio.h>

int main()
    // disguise constants one and zero, in order to fool the compiler
    double x = (double)printf("") + 1.0; // one
    double y = (double)printf(""); // zero
    if (fetestexcept(FE_ALL_EXCEPT)) {
        printf("flag set before call.\n");
    double z = x / y;
    if (fetestexcept(FE_ALL_EXCEPT)) {
        printf("flag set after call.\n");

$ gcc -O0 fenv.c -lm && ./a.out
flag set after call.
$ gcc -O1 fenv.c -lm && ./a.out

I also found that the behavior appears to depend on the compiler
version used. gcc 3.x and 4.0 print "flat set after call." even with
optimization, but the above is gcc 4.1.

Richard B. Kreckel

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3204 bytes
Desc: S/MIME Cryptographic Signature
URL: <https://gcc.gnu.org/pipermail/gcc-help/attachments/20060922/200d56bc/attachment.bin>

More information about the Gcc-help mailing list