I would like to handle fpu exception on windows, something like:
#include <math.h> #include <fenv.h> #include <stdio.h> int main() { double b = 0; int raised; feclearexcept (FE_ALL_EXCEPT); b /= 0; raised = fetestexcept (FE_OVERFLOW | FE_INVALID); if (raised & FE_OVERFLOW) { printf('over\n');} if (raised & FE_INVALID) { printf('invalid\n');} return 0; }
But on windows. I tried reading the MSDN, but the document is not clear at all. I want to do this with Visual Studio compilers, on both x86 and amd64 archs.
I am not interested in translating the exception in C++ – actually, I am not even interested in the FPU exception, only in knowing the FPU state after some computation, like the example above.
== edit ==
Ok, it looks like it is actually much simpler: using _clearfp is enough:
#include <math.h> #include <float.h> #include <stdio.h> int main() { double b = 0; int raised; raised = _clearfp(); b /= 0; raised = _clearfp(); if (raised & SW_INVALID) { printf('invalid\n');} return 0; }
Much better than dealing with exceptions, SEH and other non portable stuff 🙂
You can use _statusfp2() to retrieve the floating point status. Beware that 32-bit uses both FPU and SSE instructions. Some sample code: