Node: Floating-point Exception Handling, Next: , Previous: POSIX Standard, Up: Missing Features



Floating-point Exception Handling

The gcc backend and, consequently, g77, currently provides no general control over whether or not floating-point exceptions are trapped or ignored. (Ignoring them typically results in NaN values being propagated in systems that conform to IEEE 754.) The behavior is normally inherited from the system-dependent startup code, though some targets, such as the Alpha, have code generation options which change the behavior.

Most systems provide some C-callable mechanism to change this; this can be invoked at startup using gcc's constructor attribute. For example, just compiling and linking the following C code with your program will turn on exception trapping for the "common" exceptions on a GNU system using glibc 2.2 or newer:

     #define _GNU_SOURCE 1
     #include <fenv.h>
     static void __attribute__ ((constructor))
     trapfpe ()
     {
       /* Enable some exceptions.  At startup all exceptions are masked.  */
     
       feenableexcept (FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW);
     }
     

A convenient trick is to compile this something like:

     gcc -o libtrapfpe.a trapfpe.c
     
and then use it by adding -trapfpe to the g77 command line when linking.