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

            Bug ID: 129303
           Summary: wrong code at -O3 -fno-math-errno: if-conversion
                    hoists a conditional lrint() out of the loop and
                    raises FE_INVALID that the program never raises
           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
#include <fenv.h>
#include <stdio.h>

__attribute__((noipa)) long
foo (double x, int *p, int n)
{
  long s = 0;
  for (int i = 0; i < n; i++)
    if (p[i])
      s += __builtin_lrint (x);
  return s;
}

int p[64];

int
main (void)
{
  feclearexcept (FE_INVALID);
  long s = foo (__builtin_nan (""), p, 64);
  int inv = fetestexcept (FE_INVALID) != 0;
  printf ("s=%ld FE_INVALID=%d\n", s, inv);
  return s != 0 || inv;
}
```

Resulted in this output:
```
$ gcc -O0 -fno-math-errno repro.c -lm && ./a.out
s=0 FE_INVALID=0
$ gcc -O3 -fno-math-errno repro.c -lm && ./a.out
s=0 FE_INVALID=1
(also -O2 -ftree-vectorize -fno-math-errno; -O2 -fno-math-errno, -O3
-fno-math-errno -fno-tree-vectorize and plain -O3 exit 0)
```

Trunk: https://godbolt.org/z/jTnEx7PP3
- Trunk, GCC 15.1, 14.2, 13.2, 12.2 and 11.4 abort.

Reply via email to