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

            Bug ID: 128836
           Summary: wrong code on x86_64 when using bitwise AND on a
                    casted floating point number at -O1 and above
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: [email protected]
  Target Milestone: ---

```
/* GCC x86-64 wrong code: the AND that truncates a float to bf16 is dropped.
 *
 *   gcc -O1 gcc_bf16_and_repro.c && ./a.out
 *
 * Expected: -0x1.5cp+6 (exit 0). Wrong: -0x1.5d3334p+6 (exit 1).
 * Fails with GCC 12, 14, 15 and trunk at -O1/-O2/-O3; correct at -O0 and
 * with clang.
 */
#include <stdio.h>
#include <string.h>

__attribute__((noinline)) float f(int p, float x) {
    if (p == 1) {
        unsigned u;
        memcpy(&u, &x, 4);
        u &= 0xFFFF0000u;
        memcpy(&x, &u, 4);
        return x;
    }
    return p == 2 ? -x : x;
}

int main(void) {
    volatile int p = 1;
    volatile float v = -87.3f;
    float r = f(p, v);
    printf("%a\n", (double)r);
    return r != -87.0f;
}
```

This works fine at -O0 and on clang.

https://godbolt.org/z/ox5szq5sE

Reply via email to