Does GCC's __builtin_isnan do something other than isnan?

Viewed 216

In the GCC built-ins description, it says:

GCC provides built-in versions of the ISO C99 floating-point comparison macros that avoid raising exceptions for unordered operands. They have the same names as the standard macros ( isgreater, isgreaterequal, isless, islessequal, islessgreater, and isunordered) , with _builtin prefixed. We intend for a library implementor to be able to simply #define each standard macro to its built-in equivalent. In the same fashion, GCC provides fpclassify, isfinite, isinf_sign, isnormal and signbit built-ins used with _builtin prefixed. The isinf and isnan built-in functions appear both with and without the _builtin prefix.

So, I'm not quite able to parse this. When should floating-point comparisons raise exceptions? Does the C standard mandate they do? Mandate they don't? Doesn't mandate anything? And - does __builtin_isnan() act differently than isnan() ?

1 Answers

By exception here, the GCC docs refer to IEEE 754 floating point exceptions. If you do something like

a < b

and one of the operands is a NaN, an FP exception (invalid) will be raised. That means a bit in the FPU will remain set until it's explicitly cleared by the programmer. By instead using isgreater/isless/etc. the programmer can avoid triggering the FP exception.

Related