1 #include <benchmark/benchmark.h>
2
3 #include <fp16.h>
4 #ifndef EMSCRIPTEN
5 #include <fp16/psimd.h>
6 #endif
7
next_xorshift16(uint16_t x)8 static inline uint16_t next_xorshift16(uint16_t x) {
9 x ^= x >> 8;
10 x ^= x << 9;
11 x ^= x >> 5;
12 return x;
13 }
14
next_xorshift32(uint32_t x)15 static inline uint32_t next_xorshift32(uint32_t x) {
16 x ^= x >> 13;
17 x ^= x << 17;
18 x ^= x >> 5;
19 return x;
20 }
21 #ifndef EMSCRIPTEN
next_xorshift16_psimd(psimd_u16 x)22 PSIMD_INTRINSIC psimd_u16 next_xorshift16_psimd(psimd_u16 x) {
23 x ^= x >> psimd_splat_u16(8);
24 x ^= x << psimd_splat_u16(9);
25 x ^= x >> psimd_splat_u16(5);
26 return x;
27 }
28 #endif
29
30
fp16_alt_to_fp32_bits(benchmark::State & state)31 static void fp16_alt_to_fp32_bits(benchmark::State& state) {
32 uint16_t fp16 = UINT16_C(0x7C00);
33 while (state.KeepRunning()) {
34 const uint32_t fp32 = fp16_alt_to_fp32_bits(fp16);
35
36 fp16 = next_xorshift16(fp16);
37 benchmark::DoNotOptimize(fp32);
38 }
39 }
40 BENCHMARK(fp16_alt_to_fp32_bits);
41
fp16_alt_to_fp32_value(benchmark::State & state)42 static void fp16_alt_to_fp32_value(benchmark::State& state) {
43 uint16_t fp16 = UINT16_C(0x7C00);
44 while (state.KeepRunning()) {
45 const float fp32 = fp16_alt_to_fp32_value(fp16);
46
47 fp16 = next_xorshift16(fp16);
48 benchmark::DoNotOptimize(fp32);
49 }
50 }
51 BENCHMARK(fp16_alt_to_fp32_value);
52
53 #ifndef EMSCRIPTEN
fp16_alt_to_fp32_psimd(benchmark::State & state)54 static void fp16_alt_to_fp32_psimd(benchmark::State& state) {
55 psimd_u16 fp16 = (psimd_u16) { 0x7C00, 0x7C01, 0x7C02, 0x7C03 };
56 while (state.KeepRunning()) {
57 const psimd_f32 fp32 = fp16_alt_to_fp32_psimd(fp16);
58
59 fp16 = next_xorshift16_psimd(fp16);
60 benchmark::DoNotOptimize(fp32);
61 }
62 }
63 BENCHMARK(fp16_alt_to_fp32_psimd);
64
fp16_alt_to_fp32x2_psimd(benchmark::State & state)65 static void fp16_alt_to_fp32x2_psimd(benchmark::State& state) {
66 psimd_u16 fp16 =
67 (psimd_u16) { 0x7C00, 0x7C01, 0x7C02, 0x7C03, 0x7C04, 0x7C05, 0x7C06, 0x7C07 };
68 while (state.KeepRunning()) {
69 const psimd_f32x2 fp32 = fp16_alt_to_fp32x2_psimd(fp16);
70
71 fp16 = next_xorshift16_psimd(fp16);
72 benchmark::DoNotOptimize(fp32);
73 }
74 }
75 BENCHMARK(fp16_alt_to_fp32x2_psimd);
76 #endif
77
fp16_alt_from_fp32_value(benchmark::State & state)78 static void fp16_alt_from_fp32_value(benchmark::State& state) {
79 uint32_t fp32 = UINT32_C(0x7F800000);
80 while (state.KeepRunning()) {
81 const uint16_t fp16 = fp16_alt_from_fp32_value(fp32_from_bits(fp32));
82
83 fp32 = next_xorshift32(fp32);
84 benchmark::DoNotOptimize(fp16);
85 }
86 }
87 BENCHMARK(fp16_alt_from_fp32_value);
88
89 BENCHMARK_MAIN();
90