On 11/9/26 6:18 AM, Merlin Warner-Huish wrote:
fma (x, 1.0, y) can be folded to x + y, because multiplying by 1.0 is
exact, so the fma rounds the same way as the addition.  For the same
reason fma (x, -1.0, y) can be folded to y - x.  Adding -0.0 never
changes a value, even when x * y is a signed zero, so fma (x, y, -0.0)
can be folded to x * y.  In each case this saves the fma and loading
the constant.

fma (x, y, +0.0) is not folded, because when x * y is -0.0 the fma
gives +0.0.

        PR tree-optimization/126696

gcc/ChangeLog:

        * match.pd (fma (x, 1.0, y) -> x + y): New simplification.
        (fma (x, -1.0, y) -> y - x): Likewise.
        (fma (x, y, -0.0) -> x * y): Likewise.

gcc/testsuite/ChangeLog:

        * gcc.dg/pr126696.c: New test.
        * gcc.dg/tree-ssa/pr126696.c: New test.
---
  Bootstrapped and regression tested on x86_64-pc-linux-gnu with no new
  failures.  I also checked the aarch64 compile-only tests with a cross
  compiler.

  I didn't add a HONOR_SNANS check: unlike x * 1.0 -> x, these folds
  keep an arithmetic operation, so a signalling NaN operand is still
  quieted and still raises invalid, as with the fma.  I'm happy to add
  one if that's preferred.
Sashiko flagged this patch as generating wrong code with FE_DOWNWARD rounding for __builtin_fma (1.0, 0.0, -0.0).  I haven't checked that myself, but it seems potentially serious enough that you should check.  You do have a runtest, but without FE_DOWNWARD in effect it may not give coverage for this issue.  The full Sashiko review:

https://gcc.sashiko.dev/#/patchset/20261109131811.3747813-1-merlin.warner-huish%40embecosm.com

Jeff

Reply via email to