https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129025

            Bug ID: 129025
           Summary: [backprop] wrong code with -frounding-math:
                    ssa-backprop strips fabs/negate from MULT/RDIV
                    operands whose result sign is ignored, but the rounded
                    magnitude depends on the sign under
                    FE_DOWNWARD/FE_UPWARD
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 0599jiangyc at gmail dot com
  Target Milestone: ---

The following code:

```c
/* GCC wrong code: gcc -O1 -frounding-math repro.c -lm && ./a.out   (exit 1 =
miscompiled; also -O2/-O3)
   gimple-ssa-backprop strips sign operations (fabs/negate) from the operands
of a MULT/RDIV whose
   result sign is ignored (here: by the outer fabs), i.e. fabs(fabs(x) * x) ->
fabs(x * x).  That
   assumes |round(a*b)| does not depend on the sign of a*b, which is false
under FE_DOWNWARD /
   FE_UPWARD: with x = -DBL_MAX, fabs(x)*x = -inf (rounds down) but x*x =
DBL_MAX (rounds down).
   Expected (gcc -O0, gcc -O1 -fno-ssa-backprop, clang -O2 -frounding-math):
"inf inf", exit 0.
   gcc 11.4 -O2 -frounding-math also prints 0x1.fffffffffffffp+1023 twice
(long-standing).  */
#include <fenv.h>
#include <math.h>
#include <stdio.h>

__attribute__((noinline)) double f(double x) { return fabs(fabs(x) * x); }
__attribute__((noinline)) double g(double x, double y) { return fabs(-x / y); }

int main(void) {
  fesetround(FE_DOWNWARD);
  double a = f(-0x1.fffffffffffffp1023);
  double b = g(0x1.fffffffffffffp1023, 0.5);
  fesetround(FE_TONEAREST);
  printf("%a %a\n", a, b);
  return !(isinf(a) && isinf(b));
}
```

Resulted in this output:
```
$ gcc -O1 -frounding-math repro.c -lm && ./a.out        (also -O2; GCC 11.4
too)
0x1.fffffffffffffp+1023 0x1.fffffffffffffp+1023
(exit 1; gcc -O0, -O1 -fno-ssa-backprop and clang -O2 -frounding-math print inf
inf)
```

Trunk: https://godbolt.org/z/P39eEG7eb (x86-64 gcc trunk, -O1 -frounding-math
-lm, executed; reproduces as of 2026-11-12
  • [Bug c/129025] New: [backprop] w... 0599jiangyc at gmail dot com via Gcc-bugs

Reply via email to