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

            Bug ID: 128666
           Summary: [ranger] wrong code at -O1 and above: reverse range of
                    MULT_EXPR (operator_mult::op1_range) drops -0.0 when
                    the product may be NaN, so 1.0 / y < 0.0 is folded to
                    false
           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 trunk: wrong code at -O1 and above. Ranger's reverse range for
 * multiplication (operator_mult::op1_range) drops -0.0: from
 * !(x >= y * inf) with x == 0 it concludes y in [+0, +Inf] U NaN, but
 * y == -0.0 also gets there (-0.0 * inf is NaN). 1.0 / y < 0.0 is then
 * folded to false, although 1.0 / -0.0 == -inf.
 * Godbolt: x86-64 gcc (trunk), flags: -O2   (correct: -O0; GCC 11.4 -O2; clang
-O2)
 * EXPECTED: prints 1, exit 0.   BUGGY: prints 0, exit 1.
 */
#include <stdio.h>

__attribute__((noinline)) int f(double x, double y) {
  double inf = 1.0 / 0.0;
  if (x == 0.0 && !(x >= y * inf))   /* y * inf is NaN for y == -0.0 */
    return 1.0 / y < 0.0;            /* 1.0 / -0.0 == -inf */
  return 7;
}

int main(void) {
  volatile double x = 0.0, y = -0.0;
  int r = f(x, y);
  printf("%d\n", r);
  return r != 1;
}
```

Resulted in this output:
```
$ gcc -O2 repro.c && ./a.out        (also -O1, -O3)
0
(exit 1; gcc -O0, GCC 11.4 -O2 and clang -O2 print 1)
```

Trunk: https://godbolt.org/z/M5rqYneEh (x86-64 gcc trunk, -O2, executed;
reproduces as of 2026-11-12)

Root cause: On the path where `!(x >= y * inf)` holds with `x == 0`, ranger
solves `_1 = inf * y` for `y` in `operator_mult::op1_range`
(gcc/range-op-float.cc) with lhs `[+denorm_min, +Inf] +NAN` and op2 `[DBL_MAX,
+Inf] +NAN` (`1.0 / 0.0` is not folded under -ftrapping-math). Because both lhs
and op2 may be infinite, it takes the `zero_to_inf_range` path with the sign
derived from lhs/op2 and returns `y = [+0.0, +Inf] +NAN`;
`float_binary_op_range_finish` then only adds NAN. But the NAN in lhs can come
from `-0.0 * inf` (so `-0.0` must stay in the range), and since op2 itself may
be NAN, any `y` can produce a NAN product, i.e. op1 should be VARYING. dom2
then folds `1.0 / y < 0.0` to 0, which is wrong for `y == -0.0` (`1.0 / -0.0 ==
-inf`). The RDIV op2_range (`INF / y`, `INF / INF` is NAN, `+INF` dropped) has
the same flaw; PR128401 reports the RDIV op1_range case (`-INF / -INF`).

Suggested fix: When `lhs.maybe_isnan ()`, the reverse operations must include
the operand values that produce NAN for the given other operand (MULT: +-0 if
op2 may be +-INF and +-INF if op2 may be +-0; RDIV op2: +-INF if op1 may be
+-INF, +-0 if op1 may be +-0), and return VARYING if the other operand may be
NAN. A conservative patch (VARYING from `float_binary_op_range_finish` whenever
`lhs.maybe_isnan ()`) fixes the reproducer and all variants we found.

Reply via email to