https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129283
Bug ID: 129283
Summary: [14/15/16/17 Regression] wrong code at all -O levels:
`(float)bf16 == (float)f16` is compared in __bf16 or
_Float16 instead of float
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: haoxintu at gmail dot com
Target Milestone: ---
Hi,
$ cat repro.c
/* (float)b and (float)h are exact; 65536 != 65504 and 3.0e38 != +Inf, so both
must return 0. */
__attribute__((noipa)) int f(__bf16 b, _Float16 h) {
#ifdef SEP
float x = (float)b, y = (float)h;
return x == y;
#else
return (float)b == (float)h;
#endif
}
__attribute__((noipa)) int g(_Float16 h, __bf16 b) {
#ifdef SEP
float x = (float)h, y = (float)b;
return x == y;
#else
return (float)h == (float)b;
#endif
}
int main(void) {
if (f(65536.0f, 65504.0f16))
__builtin_abort();
if (g(__builtin_inff16(), 3.0e38f))
__builtin_abort();
return 0;
}
$ gcc -O0 repro.c && ./a.out
Aborted
$ gcc -O0 -DSEP repro.c && ./a.out; echo $?
0
$ clang -O0 repro.c && ./a.out; echo $?
0
All -O levels abort. Both values convert to float exactly, so neither
comparison should be true.
Checked versions (x86_64):
- gcc trunk 17.0.0 20261203: wrong
- gcc 16.2, 15.2, 14.2, 13.4: wrong
- gcc trunk with -DSEP (comparison through float variables): correct
- clang trunk, clang 21: correct
Godbolt (gcc trunk, gcc 13.4, gcc trunk -DSEP, clang trunk; all -O0):
https://godbolt.org/z/Wv3Tvjxsx
Thanks.