Don't fold negated FMAs when honoring signed zeros
Corrects floating-point FMA folding to preserve the sign of exact zeros.
This change prevents the incorrect folding of negated Fused Multiply-Add (FMA) operations when the compiler honors signed zeros. Previously, folding -fma(a, b, c) into fnms(a, b, c) could result in a loss of the negative sign for exact zero results. The fix ensures these folds are only applied when signed zeros are disabled, preserving mathematical accuracy for floating-point calculations.
In Details
This commit modifies the match.pd pattern-matching definitions to correctly handle negated FMA operations in the presence of signed zeros. Specifically, folds like (negate (fmas ...)) to (IFN_FNMS ...) are now guarded by a !HONOR_SIGNED_ZEROS condition. This addresses an issue where the sign of an exact zero result could be lost due to the folding rules, as demonstrated by -fma(1.0, 1.0, -1.0) resulting in +0.0 and then negating to -0.0, while fnms(1.0, 1.0, -1.0) computes to +0.0. The change aligns the behavior with the HONOR_SIGNED_ZEROS flag.
- FMA
- Fused Multiply-Add, a floating-point operation that computes (a * b) + c in a single step, often with higher precision and performance than separate multiply and add operations.
- signed zeros
- A floating-point representation where zero can have a positive (+) or negative (-) sign, important for certain mathematical and scientific computations.
- match.pd
- A file containing pattern-matching definitions used by GCC's pattern-based instruction selection and optimization passes.
- HONOR_SIGNED_ZEROS
- A compiler option or internal flag that dictates whether the compiler should preserve the sign of zero in floating-point operations.