7 #ifndef __FPO_EXCEPTIONS_H 8 #define __FPO_EXCEPTIONS_H 15 #define _EM_OVERFLOW EM_OVERFLOW 19 #define _EM_UNDERFLOW EM_UNDERFLOW 23 #define _EM_INVALID EM_INVALID 26 #ifndef _EM_ZERODIVIDE 27 #define _EM_ZERODIVIDE EM_ZERODIVIDE 28 #endif//_EM_ZERODIVIDE 42 return fegetenv (&fenv) ? -1 : (fenv.__control & FE_ALL_EXCEPT);
46 feenableexcept (
unsigned int excepts)
49 unsigned int new_excepts = excepts & FE_ALL_EXCEPT,
52 if ( fegetenv (&fenv) )
return -1;
53 old_excepts = fenv.__control & FE_ALL_EXCEPT;
56 fenv.__control &= ~new_excepts;
57 fenv.__mxcsr &= ~(new_excepts << 7);
59 return ( fesetenv (&fenv) ? -1 : old_excepts );
63 fedisableexcept (
unsigned int excepts)
66 unsigned int new_excepts = excepts & FE_ALL_EXCEPT,
69 if ( fegetenv (&fenv) )
return -1;
70 old_excepts = fenv.__control & FE_ALL_EXCEPT;
73 fenv.__control |= new_excepts;
74 fenv.__mxcsr |= new_excepts << 7;
76 return ( fesetenv (&fenv) ? -1 : old_excepts );
81 #define getx87cr(x) asm ("fnstcw %0" : "=m" (x)); 82 #define setx87cr(x) asm ("fldcw %0" : "=m" (x)); 83 #define getx87sr(x) asm ("fnstsw %0" : "=m" (x)); 86 #define getmxcsr(x) asm ("stmxcsr %0" : "=m" (x)); 87 #define setmxcsr(x) asm ("ldmxcsr %0" : "=m" (x)); 93 static std::string fe_code_name[] = {
95 "FPE_FLTDIV",
"FPE_FLTINV",
"FPE_FLTOVF",
"FPE_FLTUND",
96 "FPE_FLTRES",
"FPE_FLTSUB",
"FPE_INTDIV",
"FPE_INTOVF" 115 fhdl (
int sig, siginfo_t *sip, ucontext_t *scp )
117 int fe_code = sip->si_code;
118 unsigned int excepts = fetestexcept (FE_ALL_EXCEPT);
122 #ifdef FPE_NOOP // occurs in OS X 123 case FPE_NOOP: fe_code = 0;
break;
125 case FPE_FLTDIV: fe_code = 1;
break;
126 case FPE_FLTINV: fe_code = 2;
break;
127 case FPE_FLTOVF: fe_code = 3;
break;
128 case FPE_FLTUND: fe_code = 4;
break;
129 case FPE_FLTRES: fe_code = 5;
break;
130 case FPE_FLTSUB: fe_code = 6;
break;
131 case FPE_INTDIV: fe_code = 7;
break;
132 case FPE_INTOVF: fe_code = 8;
break;
133 default: fe_code = 9;
138 unsigned short x87cr,x87sr;
144 printf (
"X87CR: 0x%04X\n", x87cr);
145 printf (
"X87SR: 0x%04X\n", x87sr);
146 printf (
"MXCSR: 0x%08X\n", mxcsr);
149 printf (
"signal: SIGFPE with code %s\n", fe_code_name[fe_code].c_str());
150 printf (
"invalid flag: 0x%04X\n", excepts & FE_INVALID);
151 printf (
"divByZero flag: 0x%04X\n", excepts & FE_DIVBYZERO);
153 else printf (
"Signal is not SIGFPE, it's %i.\n", sig);
172 _controlfp_s(&fPrevConfig, 0, 0);
173 _controlfp(1, _EM_OVERFLOW);
174 _controlfp(1, _EM_INVALID);
175 _controlfp(1, _EM_ZERODIVIDE);
177 fegetenv(&fPrevConfig);
178 feraiseexcept(FE_INVALID | FE_OVERFLOW | FE_DIVBYZERO);
185 _controlfp_s(&temp, fPrevConfig, _MCW_EM);
187 fesetenv(&fPrevConfig);
193 #endif //__FPO_EXCEPTIONS_H