Fix signed zero and rounding issues in constant folding.
Corrects constant folding for `x - x` under `FE_DOWNWARD` rounding to produce `-0.0`.
This patch addresses a corner case in constant folding related to floating-point arithmetic, specifically for the expression x - x. When using -frounding-math with FE_DOWNWARD rounding, the result of x - x (where x is finite and not NaN) should be -0.0 to honor signed zeros, not +0.0. The compiler’s constant folding in fold-const.cc and RTL optimizers in simplify-rtx.cc is updated to reflect this, preventing incorrect simplification to +0.0.
In Details
This commit corrects the behavior of constant folding for the x - x expression when the -frounding-math flag is enabled and the FE_DOWNWARD rounding mode is active. Standard floating-point semantics dictate that for a finite, non-NaN x, x - x should result in -0.0 under FE_DOWNWARD due to the rules of signed zeros. Previously, fold-const.cc and simplify-rtx.cc incorrectly folded this to +0.0. The changes prevent this simplification under the specified conditions, ensuring compliance with IEEE 754 standards for signed zero and rounding.
- constant folding
- A compiler optimization that evaluates constant expressions at compile time rather than runtime.
- -frounding-math
- A GCC flag that enables adherence to the IEEE 754 standard's rounding modes, rather than the C's default behavior.
- FE_DOWNWARD
- A floating-point environment constant specifying the rounding mode to round towards negative infinity.
- signed zero
- Floating-point numbers can have both a positive zero (+0.0) and a negative zero (-0.0). These typically compare as equal but can affect the sign of subsequent calculations.
- RTL
- Register Transfer Language. An intermediate representation used in GCC, representing operations at a lower level than GIMPLE, closer to machine code.