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:
#include "stdafx.h"
#include <float.h>
#include <math.h>
#include <assert.h>
int _tmain(int argc, _TCHAR* argv[])
{
unsigned x86;
unsigned sse;
// Test zero-divide
double d = 0;
double v = 1 / d;
_statusfp2(&x86, &sse);
assert(x86 & _EM_ZERODIVIDE);
// Test overflow
v = pow(10, 310.0);
_statusfp2(&x86, &sse);
assert(sse & _EM_OVERFLOW);
return 0;
}