How to catch floating point errors early (right at where they occur)?

Viewed 167

When developing floating-point heavy code, it is very useful to enable FPU exceptions. When an operation results in a NaN/inf, we could catch it immediately.

For example, on Linux, I can enable this with:

feenableexcept(FE_DIVBYZERO|FE_INVALID|FE_OVERFLOW);

Before SSE, this technique worked like a charm. Today, with SSE, however this conflicts with -ffast-math (or other equivalent option of other compilers).

One of the reasons is sqrt, because sqrt is implemented as x*rsqrt(x). And this generates an exception when x is zero. When exceptions are disabled, this is not a problem, because the sqrt function handles this.

So, I cannot enable exceptions because of this (and maybe there are other reasons as well).

Do you have any suggestions, how to catch floating-point errors early, right at the place it occurs (I don't want NaN to propagate, and I'd like to catch overflow-to-infinity too), if -ffast-math is enabled?

0 Answers
Related