builtins: build fpclassify on the classification optabs [PR66462]
Built-in fpclassify now uses specialized classification optabs for better accuracy and to avoid spurious exceptions.
The __builtin_fpclassify function now leverages target-specific classification optabs, such as isnan, isinf, and isnormal, when available. This change replaces direct floating-point comparisons, which could lead to incorrect results or trap on signaling NaNs and subnormal numbers on certain architectures like AArch64 and LoongArch. The new implementation uses bit manipulation for improved accuracy.
In Details
This patch modifies fold_builtin_fpclassify to use the classification optabs (like TARGET_ISINF, TARGET_ISNORMAL, TARGET_ISNAN) when they are defined for the given mode. Previously, fpclassify always expanded to comparisons, problematic on targets like AArch64 with -fsignaling-nans or Alpha where subnormal comparisons trap. The new expansion uses a bit-shift on the exponent for subnormal detection, preserving correct behavior for signaling NaNs and subnormals, while retaining generic expansion for formats like XFmode or composite formats where the bit-shift is not applicable.
- optabs
- Operation tables. These are data structures used by GCC to map abstract operations to specific machine instructions or sequences of instructions for a target architecture.
- FP_SUBNORMAL
- A floating-point number that is not zero but has a magnitude smaller than the smallest representable normalized number.
- signaling NaN
- A special floating-point value that, when used in an operation, generates an exception (e.g., FE_INVALID) and its value is typically propagated. This contrasts with a quiet NaN, which propagates without raising an exception.
- XFmode
- GCC internal mode for the IEEE 754 quadruple-precision floating-point format (128 bits).
- FE_INVALID
- A floating-point exception flag indicating an invalid operation, such as dividing zero by zero or taking the square root of a negative number.