https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129024
Bug ID: 129024
Summary: [match.pd] wrong code with -frounding-math
-fno-trapping-math: "(FTYPE) N CMP CST" fold uses
round-to-nearest images of the integer range bounds
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: gcc -O2 -frounding-math -fno-trapping-math repro.c -lm
&& ./a.out (exit 1 = miscompiled)
match.pd "(FTYPE) N CMP CST" uses ranger range of N, but the out-of-range
test rounds the range bounds
to nearest: with N in [0,16777217], imax = (float)16777217 = 16777216 (RN)
so (float)n < 16777218.0f folds to 1,
yet under FE_UPWARD (float)16777217 == 16777218.0f. gcc -O0, gcc 11 -O2,
clang -O2: 0 0 1. */
#include <fenv.h>
#include <stdio.h>
__attribute__((noinline)) int lt(int n) {
if (n < 0 || n > 16777217) return -1;
return (float)n < 16777218.0f;
}
__attribute__((noinline)) int gt(int n) {
if (n > 0 || n < -16777217) return -1;
return (float)n > -16777218.0f;
}
__attribute__((noinline)) int eq(long long n) {
if (n < 0 || n > 9007199254740993LL) return -1;
return (double)n == 9007199254740994.0;
}
int main(void) {
fesetround(FE_UPWARD);
int a = lt(16777217), c = eq(9007199254740993LL);
fesetround(FE_DOWNWARD);
int b = gt(-16777217);
fesetround(FE_TONEAREST);
printf("%d %d %d (expect 0 0 1)\n", a, b, c);
return a != 0 || b != 0 || c != 1;
}
```
Resulted in this output:
```
$ gcc -O2 -frounding-math -fno-trapping-math repro.c -lm && ./a.out
1 1 0 (expect 0 0 1)
(exit 1; gcc -O0, GCC 11.4 -O2 and clang -O2 print 0 0 1)
```
Trunk: https://godbolt.org/z/sjMWYn5Ex (x86-64 gcc trunk, -O2 -frounding-math
-fno-trapping-math -lm, executed; reproduces as of 2026-11-12)