1*5f32b710SXin Li #include <gtest/gtest.h>
2*5f32b710SXin Li
3*5f32b710SXin Li #include <cstdint>
4*5f32b710SXin Li
5*5f32b710SXin Li #include <fp16.h>
6*5f32b710SXin Li #include <fp16/psimd.h>
7*5f32b710SXin Li
8*5f32b710SXin Li
TEST(FP16_ALT_TO_FP32_PSIMD,positive_normalized_values)9*5f32b710SXin Li TEST(FP16_ALT_TO_FP32_PSIMD, positive_normalized_values) {
10*5f32b710SXin Li const uint32_t exponentBias = 15;
11*5f32b710SXin Li for (int32_t e = -14; e <= 16; e++) {
12*5f32b710SXin Li for (uint16_t h = 0; h < 0x0400; h += 4) {
13*5f32b710SXin Li const psimd_u16 fp16 = {
14*5f32b710SXin Li (uint16_t) (h + ((e + exponentBias) << 10) + 0),
15*5f32b710SXin Li (uint16_t) (h + ((e + exponentBias) << 10) + 1),
16*5f32b710SXin Li (uint16_t) (h + ((e + exponentBias) << 10) + 2),
17*5f32b710SXin Li (uint16_t) (h + ((e + exponentBias) << 10) + 3),
18*5f32b710SXin Li };
19*5f32b710SXin Li const psimd_u32 fp32 = (psimd_u32) fp16_alt_to_fp32_psimd(fp16);
20*5f32b710SXin Li
21*5f32b710SXin Li EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[0]), fp32[0]) <<
22*5f32b710SXin Li std::hex << std::uppercase << std::setfill('0') <<
23*5f32b710SXin Li "F16 = 0x" << std::setw(4) << fp16[0] << ", " <<
24*5f32b710SXin Li "F32(F16) = 0x" << std::setw(8) << fp32[0] << ", " <<
25*5f32b710SXin Li "F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[0]);
26*5f32b710SXin Li EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[1]), fp32[1]) <<
27*5f32b710SXin Li std::hex << std::uppercase << std::setfill('0') <<
28*5f32b710SXin Li "F16 = 0x" << std::setw(4) << fp16[1] << ", " <<
29*5f32b710SXin Li "F32(F16) = 0x" << std::setw(8) << fp32[1] << ", " <<
30*5f32b710SXin Li "F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[1]);
31*5f32b710SXin Li EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[2]), fp32[2]) <<
32*5f32b710SXin Li std::hex << std::uppercase << std::setfill('0') <<
33*5f32b710SXin Li "F16 = 0x" << std::setw(4) << fp16[2] << ", " <<
34*5f32b710SXin Li "F32(F16) = 0x" << std::setw(8) << fp32[2] << ", " <<
35*5f32b710SXin Li "F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[2]);
36*5f32b710SXin Li EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[3]), fp32[3]) <<
37*5f32b710SXin Li std::hex << std::uppercase << std::setfill('0') <<
38*5f32b710SXin Li "F16 = 0x" << std::setw(4) << fp16[3] << ", " <<
39*5f32b710SXin Li "F32(F16) = 0x" << std::setw(8) << fp32[3] << ", " <<
40*5f32b710SXin Li "F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[3]);
41*5f32b710SXin Li }
42*5f32b710SXin Li }
43*5f32b710SXin Li }
44*5f32b710SXin Li
TEST(FP16_ALT_TO_FP32_PSIMD,negative_normalized_values)45*5f32b710SXin Li TEST(FP16_ALT_TO_FP32_PSIMD, negative_normalized_values) {
46*5f32b710SXin Li const uint32_t exponentBias = 15;
47*5f32b710SXin Li for (int32_t e = -14; e <= 16; e++) {
48*5f32b710SXin Li for (uint16_t h = 0; h < 0x0400; h += 4) {
49*5f32b710SXin Li const psimd_u16 fp16 = {
50*5f32b710SXin Li (uint16_t) (h + ((e + exponentBias) << 10) + 0x8000),
51*5f32b710SXin Li (uint16_t) (h + ((e + exponentBias) << 10) + 0x8001),
52*5f32b710SXin Li (uint16_t) (h + ((e + exponentBias) << 10) + 0x8002),
53*5f32b710SXin Li (uint16_t) (h + ((e + exponentBias) << 10) + 0x8003),
54*5f32b710SXin Li };
55*5f32b710SXin Li const psimd_u32 fp32 = (psimd_u32) fp16_alt_to_fp32_psimd(fp16);
56*5f32b710SXin Li
57*5f32b710SXin Li EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[0]), fp32[0]) <<
58*5f32b710SXin Li std::hex << std::uppercase << std::setfill('0') <<
59*5f32b710SXin Li "F16 = 0x" << std::setw(4) << fp16[0] << ", " <<
60*5f32b710SXin Li "F32(F16) = 0x" << std::setw(8) << fp32[0] << ", " <<
61*5f32b710SXin Li "F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[0]);
62*5f32b710SXin Li EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[1]), fp32[1]) <<
63*5f32b710SXin Li std::hex << std::uppercase << std::setfill('0') <<
64*5f32b710SXin Li "F16 = 0x" << std::setw(4) << fp16[1] << ", " <<
65*5f32b710SXin Li "F32(F16) = 0x" << std::setw(8) << fp32[1] << ", " <<
66*5f32b710SXin Li "F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[1]);
67*5f32b710SXin Li EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[2]), fp32[2]) <<
68*5f32b710SXin Li std::hex << std::uppercase << std::setfill('0') <<
69*5f32b710SXin Li "F16 = 0x" << std::setw(4) << fp16[2] << ", " <<
70*5f32b710SXin Li "F32(F16) = 0x" << std::setw(8) << fp32[2] << ", " <<
71*5f32b710SXin Li "F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[2]);
72*5f32b710SXin Li EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[3]), fp32[3]) <<
73*5f32b710SXin Li std::hex << std::uppercase << std::setfill('0') <<
74*5f32b710SXin Li "F16 = 0x" << std::setw(4) << fp16[3] << ", " <<
75*5f32b710SXin Li "F32(F16) = 0x" << std::setw(8) << fp32[3] << ", " <<
76*5f32b710SXin Li "F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[3]);
77*5f32b710SXin Li }
78*5f32b710SXin Li }
79*5f32b710SXin Li }
80*5f32b710SXin Li
TEST(FP16_ALT_TO_FP32_PSIMD,positive_denormalized_values)81*5f32b710SXin Li TEST(FP16_ALT_TO_FP32_PSIMD, positive_denormalized_values) {
82*5f32b710SXin Li for (uint16_t h = 0; h < 0x0400; h += 4) {
83*5f32b710SXin Li const psimd_u16 fp16 = {
84*5f32b710SXin Li (uint16_t) (h + 0),
85*5f32b710SXin Li (uint16_t) (h + 1),
86*5f32b710SXin Li (uint16_t) (h + 2),
87*5f32b710SXin Li (uint16_t) (h + 3),
88*5f32b710SXin Li };
89*5f32b710SXin Li const psimd_u32 fp32 = (psimd_u32) fp16_alt_to_fp32_psimd(fp16);
90*5f32b710SXin Li
91*5f32b710SXin Li EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[0]), fp32[0]) <<
92*5f32b710SXin Li std::hex << std::uppercase << std::setfill('0') <<
93*5f32b710SXin Li "F16 = 0x" << std::setw(4) << fp16[0] << ", " <<
94*5f32b710SXin Li "F32(F16) = 0x" << std::setw(8) << fp32[0] << ", " <<
95*5f32b710SXin Li "F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[0]);
96*5f32b710SXin Li EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[1]), fp32[1]) <<
97*5f32b710SXin Li std::hex << std::uppercase << std::setfill('0') <<
98*5f32b710SXin Li "F16 = 0x" << std::setw(4) << fp16[1] << ", " <<
99*5f32b710SXin Li "F32(F16) = 0x" << std::setw(8) << fp32[1] << ", " <<
100*5f32b710SXin Li "F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[1]);
101*5f32b710SXin Li EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[2]), fp32[2]) <<
102*5f32b710SXin Li std::hex << std::uppercase << std::setfill('0') <<
103*5f32b710SXin Li "F16 = 0x" << std::setw(4) << fp16[2] << ", " <<
104*5f32b710SXin Li "F32(F16) = 0x" << std::setw(8) << fp32[2] << ", " <<
105*5f32b710SXin Li "F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[2]);
106*5f32b710SXin Li EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[3]), fp32[3]) <<
107*5f32b710SXin Li std::hex << std::uppercase << std::setfill('0') <<
108*5f32b710SXin Li "F16 = 0x" << std::setw(4) << fp16[3] << ", " <<
109*5f32b710SXin Li "F32(F16) = 0x" << std::setw(8) << fp32[3] << ", " <<
110*5f32b710SXin Li "F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[3]);
111*5f32b710SXin Li }
112*5f32b710SXin Li }
113*5f32b710SXin Li
TEST(FP16_ALT_TO_FP32_PSIMD,negative_denormalized_values)114*5f32b710SXin Li TEST(FP16_ALT_TO_FP32_PSIMD, negative_denormalized_values) {
115*5f32b710SXin Li for (uint16_t h = 0; h < 0x0400; h += 4) {
116*5f32b710SXin Li const psimd_u16 fp16 = {
117*5f32b710SXin Li (uint16_t) (h + 0x8000),
118*5f32b710SXin Li (uint16_t) (h + 0x8001),
119*5f32b710SXin Li (uint16_t) (h + 0x8002),
120*5f32b710SXin Li (uint16_t) (h + 0x8003),
121*5f32b710SXin Li };
122*5f32b710SXin Li const psimd_u32 fp32 = (psimd_u32) fp16_alt_to_fp32_psimd(fp16);
123*5f32b710SXin Li
124*5f32b710SXin Li EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[0]), fp32[0]) <<
125*5f32b710SXin Li std::hex << std::uppercase << std::setfill('0') <<
126*5f32b710SXin Li "F16 = 0x" << std::setw(4) << fp16[0] << ", " <<
127*5f32b710SXin Li "F32(F16) = 0x" << std::setw(8) << fp32[0] << ", " <<
128*5f32b710SXin Li "F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[0]);
129*5f32b710SXin Li EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[1]), fp32[1]) <<
130*5f32b710SXin Li std::hex << std::uppercase << std::setfill('0') <<
131*5f32b710SXin Li "F16 = 0x" << std::setw(4) << fp16[1] << ", " <<
132*5f32b710SXin Li "F32(F16) = 0x" << std::setw(8) << fp32[1] << ", " <<
133*5f32b710SXin Li "F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[1]);
134*5f32b710SXin Li EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[2]), fp32[2]) <<
135*5f32b710SXin Li std::hex << std::uppercase << std::setfill('0') <<
136*5f32b710SXin Li "F16 = 0x" << std::setw(4) << fp16[2] << ", " <<
137*5f32b710SXin Li "F32(F16) = 0x" << std::setw(8) << fp32[2] << ", " <<
138*5f32b710SXin Li "F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[2]);
139*5f32b710SXin Li EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[3]), fp32[3]) <<
140*5f32b710SXin Li std::hex << std::uppercase << std::setfill('0') <<
141*5f32b710SXin Li "F16 = 0x" << std::setw(4) << fp16[3] << ", " <<
142*5f32b710SXin Li "F32(F16) = 0x" << std::setw(8) << fp32[3] << ", " <<
143*5f32b710SXin Li "F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[3]);
144*5f32b710SXin Li }
145*5f32b710SXin Li }
146