[ale] Trapping a fpe?
Justin Russell
justin.w.russell at lmco.com
Wed Aug 16 13:26:59 EDT 2000
FYI,
Here is some info I found on trapping a floating-point exception under
Linux on Intel (works differently on Alpha). I tried to use signal to
trap SIGFPE, but for some reason (I'm still not clear on this), SIGFPE
is not produced when, for example, a divide-by-zero is encountered. It
just produces an "Inf" and keeps on going (the old nan nan nan
syndrome). I did find this reference, however, and the following works
great:
Floating point trapping
Build with gcc -c trapfpe.c and link with trapfpe.o. Ultimately, the
code could be made somewhat more portable by using the C9X facilities
(fesetenv() declared in fenv.h if special
masks are defined).
(Adapted from: info g77 'Trouble' 'Missing Features' 'Floating-point
Exception Handling')
trapfpe.c
#include <fpu_control.h>
static void __attribute__ ((constructor)) trapfpe (void)
{
fpu_control_t cw =
_FPU_DEFAULT & ~(_FPU_MASK_IM | _FPU_MASK_ZM | _FPU_MASK_OM);
_FPU_SETCW(cw);
}
A core file is dumped and the code dies (with a signal 8) when a fp
exception is encountered making it MUCH easier to debug with gdb. The
web page I found this on also has a lot of other great info on various
*NIX compilers. Here is the link:
http://www.nag.co.uk/projects/Julius/Useful/CompilerTricks.html
I hope this will help others.
--
-----------------------------------------------
Justin W. Russell
Engineer Senior, Aerodynamics/CFD
Lockheed Martin Aeronautics Company
86 South Cobb Drive
Marietta, GA 30063-0685
Tel: (770) 494-6303
Fax: (770) 494-3055
-----------------------------------------------
--
To unsubscribe: mail majordomo at ale.org with "unsubscribe ale" in message body.
More information about the Ale
mailing list