GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
middle-end

Math builtins preserve signaling NaN exceptions

Optimizations for math builtins now correctly preserve signaling NaN exceptions, preventing incorrect folding when a signaling NaN input is present.

This change ensures that several math builtins (frexp, modf, logb, significand, ldexp, scalbn, scalbln, hypot, cabs, pow) correctly handle signaling NaNs when the -fsignaling-nans option is enabled. Previously, some folding optimizations would incorrectly process signaling NaNs, leading to the loss of the FE_INVALID exception. The affected folding routines and pattern-matching rules have been updated to skip signaling NaN inputs, aligning their behavior with existing checks in fold_const_pow and gimple_expand_builtin_pow.

In Details

Several folding routines in GCC's middle-end, including fold_builtin_frexp, fold_builtin_modf, fold_const_call, and pattern-matching rules in match.pd, are updated to avoid processing signaling NaN inputs when the -fsignaling-nans flag is active. These folds, as well as cabs lowering in tree-complex.cc, previously dropped the signaling NaN, leading to the loss of the FE_INVALID exception. The fix ensures these folds are skipped if an operand may be a signaling NaN, consistent with fold_const_pow and gimple_expand_builtin_pow.

For Context
signaling NaN
A special floating-point value that, when used in an operation, signals an exception (like invalid operation) and typically propagates itself.
FE_INVALID
A floating-point exception flag indicating an invalid operation, such as dividing by zero or taking the square root of a negative number.
folding
An optimization technique where constant expressions or function calls with constant arguments are evaluated at compile time.
builtins
Functions provided by the compiler that offer special capabilities, often mapping to specific hardware instructions or performing optimized operations.
Filed Under: optimizationfloating-pointnan