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

            Bug ID: 128912
           Summary: [14/15/16/17 Regression] wrong code at any
                    optimization level: (double)(_Float16)(__bf16)x folded
                    to (double)(__bf16)x, losing the _Float16 overflow to
                    +Inf
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: haoxintu at gmail dot com
  Target Milestone: ---

Hi,


$cat repro.c

[[gnu::noipa]] double f (float a)
{
  return (double) (_Float16) (__bf16) a;
}

int main (void)
{
  /* (__bf16) 65504.0f == 65536.0, which overflows _Float16 to +inf.  */
  if (f (65504.0f) != __builtin_inf ())
    __builtin_abort ();
  return 0;
}

$ gcc repro.c && ./a.out          # any -O level, including -O0
Aborted

(__bf16) 65504.0f rounds to 65536.0 (8-bit significand), which exceeds the
largest finite _Float16 (65504) and must convert to +Inf. f returns 65536.0. 

Even at -O0 there is no conversion to _Float16:

```
        call    __truncsfbf2
        call    __extendbfsf2
        cvtss2sd        %xmm0, %xmm0
```

The same fold makes range-guarded code wrong once the value flows through a
variable (repro-cmp.c: `_Float16 v = (_Float16) (__bf16) a; return (long
double) v < 0x1p64L;` returns 1 for a = 65504 at -O1 and above, 0 at -O0).


### Checked Versions
  - Wrong in GCC 13, 14, 15, 16, and trunk (17.0.0 20261119).
  - Correct in Clang


Reproduced in Godbolt (x86-64 gcc trunk -O1 and clang):
https://godbolt.org/z/b598Mrhx9

Thanks.

Reply via email to