| 1 |
/* { dg-do run } */ |
| 2 |
/* { dg-options "-march=x86-64-v3 -mavx10.2 -O2 -fno-trapping-math" } */ |
| 3 |
/* { dg-require-effective-target avx10_2 } */ |
| 4 |
|
| 5 |
#include <stdlib.h> |
| 6 |
#include <stdint.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
/* Fast shift conversion here for convenience */ |
| 10 |
static __bf16 |
| 11 |
float_to_bf16 (float f) |
| 12 |
{ |
| 13 |
uint32_t float_bits; |
| 14 |
uint16_t bf16_bits; |
| 15 |
|
| 16 |
memcpy (&float_bits, &f, sizeof (float_bits)); |
| 17 |
bf16_bits = (uint16_t) (float_bits >> 16); |
| 18 |
|
| 19 |
__bf16 bf; |
| 20 |
memcpy (&bf, &bf16_bits, sizeof (bf)); |
| 21 |
return bf; |
| 22 |
} |
| 23 |
|
| 24 |
static float |
| 25 |
bf16_to_float (__bf16 bf) |
| 26 |
{ |
| 27 |
uint32_t float_bits; |
| 28 |
uint16_t bf16_bits; |
| 29 |
|
| 30 |
memcpy (&bf16_bits, &bf, sizeof (bf16_bits)); |
| 31 |
float_bits = ((uint32_t) bf16_bits) << 16; |
| 32 |
|
| 33 |
float f; |
| 34 |
memcpy (&f, &float_bits, sizeof (f)); |
| 35 |
return f; |
| 36 |
} |
| 37 |
|
| 38 |
static void |
| 39 |
test_eq (__bf16 a, __bf16 b) |
| 40 |
{ |
| 41 |
int result = (a == b); |
| 42 |
int expected = (bf16_to_float (a) == bf16_to_float (b)); |
| 43 |
if (result != expected) |
| 44 |
abort (); |
| 45 |
} |
| 46 |
|
| 47 |
static void |
| 48 |
test_ne (__bf16 a, __bf16 b) |
| 49 |
{ |
| 50 |
int result = (a != b); |
| 51 |
int expected = (bf16_to_float (a) != bf16_to_float (b)); |
| 52 |
if (result != expected) |
| 53 |
abort (); |
| 54 |
} |
| 55 |
|
| 56 |
static void |
| 57 |
test_lt (__bf16 a, __bf16 b) |
| 58 |
{ |
| 59 |
int result = (a < b); |
| 60 |
int expected = (bf16_to_float (a) < bf16_to_float (b)); |
| 61 |
if (result != expected) |
| 62 |
abort (); |
| 63 |
} |
| 64 |
|
| 65 |
static void |
| 66 |
test_le (__bf16 a, __bf16 b) |
| 67 |
{ |
| 68 |
int result = (a <= b); |
| 69 |
int expected = (bf16_to_float (a) <= bf16_to_float (b)); |
| 70 |
if (result != expected) |
| 71 |
abort (); |
| 72 |
} |
| 73 |
|
| 74 |
static void |
| 75 |
test_gt (__bf16 a, __bf16 b) |
| 76 |
{ |
| 77 |
int result = (a > b); |
| 78 |
int expected = (bf16_to_float (a) > bf16_to_float (b)); |
| 79 |
if (result != expected) |
| 80 |
abort (); |
| 81 |
} |
| 82 |
|
| 83 |
static void |
| 84 |
test_ge (__bf16 a, __bf16 b) |
| 85 |
{ |
| 86 |
int result = (a >= b); |
| 87 |
int expected = (bf16_to_float (a) >= bf16_to_float (b)); |
| 88 |
if (result != expected) |
| 89 |
abort (); |
| 90 |
} |
| 91 |
|
| 92 |
int |
| 93 |
main (void) |
| 94 |
{ |
| 95 |
if (!__builtin_cpu_supports ("avx10.2")) |
| 96 |
return 0; |
| 97 |
|
| 98 |
float test_values[] = { |
| 99 |
-10.0f, -1.0f, -0.5f, 0.0f, 0.5f, 1.0f, 10.0f, 100.0f, -100.0f |
| 100 |
}; |
| 101 |
|
| 102 |
size_t num_values = sizeof (test_values) / sizeof (test_values[0]); |
| 103 |
|
| 104 |
for (size_t i = 0; i < num_values; i++) |
| 105 |
for (size_t j = 0; j < num_values; j++) |
| 106 |
{ |
| 107 |
__bf16 a = float_to_bf16 (test_values[i]); |
| 108 |
__bf16 b = float_to_bf16 (test_values[j]); |
| 109 |
|
| 110 |
test_eq (a, b); |
| 111 |
test_ne (a, b); |
| 112 |
test_lt (a, b); |
| 113 |
test_le (a, b); |
| 114 |
test_gt (a, b); |
| 115 |
test_ge (a, b); |
| 116 |
} |
| 117 |
|
| 118 |
return 0; |
| 119 |
} |