| 1 |
/* { dg-do run } */ |
| 2 |
/* { dg-additional-options "-O2 -std=c99" } */ |
| 3 |
|
| 4 |
#include <stdint.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <string.h> |
| 7 |
|
| 8 |
__attribute__ ((noipa)) |
| 9 |
float convert(__bf16 value) { |
| 10 |
return (float)value; |
| 11 |
} |
| 12 |
|
| 13 |
static inline uint32_t f32_bits(float f) { |
| 14 |
uint32_t u; memcpy(&u, &f, sizeof u); return u; |
| 15 |
} |
| 16 |
static inline __bf16 bf16_from_bits(uint16_t u) { |
| 17 |
__bf16 b; memcpy(&b, &u, sizeof b); return b; |
| 18 |
} |
| 19 |
|
| 20 |
/* Fixed bf16 inputs (as raw 16-bit payloads) covering edge cases. */ |
| 21 |
static const uint16_t inputs[] = { |
| 22 |
0x0000, // +0 |
| 23 |
0x8000, // -0 |
| 24 |
0x7F80, // +inf |
| 25 |
0xFF80, // -inf |
| 26 |
0x7FC0, // qNaN (+) (quiet bit set in bf16) |
| 27 |
0xFFC0, // qNaN (-) |
| 28 |
0x7F01, // sNaN (+) (will be quieted by conversion) |
| 29 |
0xFF01, // sNaN (-) |
| 30 |
0x0001, // smallest +subnormal |
| 31 |
0x007F, // largest +subnormal |
| 32 |
0x8001, // smallest -subnormal |
| 33 |
0x807F, // largest -subnormal |
| 34 |
0x0080, // smallest +normal |
| 35 |
0x3F80, // +1.0 |
| 36 |
0xBF80, // -1.0 |
| 37 |
0x3F00, // +0.5 |
| 38 |
0xBF00, // -0.5 |
| 39 |
0x3FC0, // +1.5 |
| 40 |
0x7F7F, // max finite + |
| 41 |
0xFF7F, // max finite - |
| 42 |
}; |
| 43 |
|
| 44 |
int main(void) { |
| 45 |
const size_t N = sizeof(inputs)/sizeof(inputs[0]); |
| 46 |
size_t fails = 0; |
| 47 |
|
| 48 |
for (size_t i = 0; i < N; ++i) { |
| 49 |
__bf16 in = bf16_from_bits(inputs[i]); |
| 50 |
float out = convert(in); |
| 51 |
uint32_t got = f32_bits(out); |
| 52 |
uint32_t exp = inputs[i] << 16; |
| 53 |
|
| 54 |
if (got != exp) { |
| 55 |
printf("FAIL[%zu]: in_bf16=0x%04X exp_f32=0x%08X got_f32=0x%08X\n", |
| 56 |
i, inputs[i], exp, got); |
| 57 |
++fails; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
if (fails != 0) |
| 62 |
__builtin_abort (); |
| 63 |
} |
| 64 |
|