floating point exception flags and compiler optimization

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

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");
   }
   printf("%f/%f==%f\n",x,y,z);

}
$ gcc -O0 fenv.c -lm && ./a.out
flag set after call.
1.000000/0.000000==inf
$ gcc -O1 fenv.c -lm && ./a.out
1.000000/0.000000==inf

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.

TIA
 -richy.
--
Richard B. Kreckel
<kreckel@xxxxxxxx>

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux