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