File: alt-to-fp32-psimd.cc

package info (click to toggle)
fp16 0.0~git20200514.4dfe081-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 692 kB
  • sloc: cpp: 6,683; ansic: 675; python: 140; makefile: 11
file content (145 lines) | stat: -rw-r--r-- 6,325 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <gtest/gtest.h>

#include <cstdint>

#include <fp16.h>
#include <fp16/psimd.h>


TEST(FP16_ALT_TO_FP32_PSIMD, positive_normalized_values) {
	const uint32_t exponentBias = 15;
	for (int32_t e = -14; e <= 16; e++) {
		for (uint16_t h = 0; h < 0x0400; h += 4) {
			const psimd_u16 fp16 = {
				(uint16_t) (h + ((e + exponentBias) << 10) + 0),
				(uint16_t) (h + ((e + exponentBias) << 10) + 1),
				(uint16_t) (h + ((e + exponentBias) << 10) + 2),
				(uint16_t) (h + ((e + exponentBias) << 10) + 3),
			};
			const psimd_u32 fp32 = (psimd_u32) fp16_alt_to_fp32_psimd(fp16);

			EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[0]), fp32[0]) <<
				std::hex << std::uppercase << std::setfill('0') <<
				"F16 = 0x" << std::setw(4) << fp16[0] << ", " <<
				"F32(F16) = 0x" << std::setw(8) << fp32[0] << ", " <<
				"F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[0]);
			EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[1]), fp32[1]) <<
				std::hex << std::uppercase << std::setfill('0') <<
				"F16 = 0x" << std::setw(4) << fp16[1] << ", " <<
				"F32(F16) = 0x" << std::setw(8) << fp32[1] << ", " <<
				"F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[1]);
			EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[2]), fp32[2]) <<
				std::hex << std::uppercase << std::setfill('0') <<
				"F16 = 0x" << std::setw(4) << fp16[2] << ", " <<
				"F32(F16) = 0x" << std::setw(8) << fp32[2] << ", " <<
				"F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[2]);
			EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[3]), fp32[3]) <<
				std::hex << std::uppercase << std::setfill('0') <<
				"F16 = 0x" << std::setw(4) << fp16[3] << ", " <<
				"F32(F16) = 0x" << std::setw(8) << fp32[3] << ", " <<
				"F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[3]);
		}
	}
}

TEST(FP16_ALT_TO_FP32_PSIMD, negative_normalized_values) {
	const uint32_t exponentBias = 15;
	for (int32_t e = -14; e <= 16; e++) {
		for (uint16_t h = 0; h < 0x0400; h += 4) {
			const psimd_u16 fp16 = {
				(uint16_t) (h + ((e + exponentBias) << 10) + 0x8000),
				(uint16_t) (h + ((e + exponentBias) << 10) + 0x8001),
				(uint16_t) (h + ((e + exponentBias) << 10) + 0x8002),
				(uint16_t) (h + ((e + exponentBias) << 10) + 0x8003),
			};
			const psimd_u32 fp32 = (psimd_u32) fp16_alt_to_fp32_psimd(fp16);

			EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[0]), fp32[0]) <<
				std::hex << std::uppercase << std::setfill('0') <<
				"F16 = 0x" << std::setw(4) << fp16[0] << ", " <<
				"F32(F16) = 0x" << std::setw(8) << fp32[0] << ", " <<
				"F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[0]);
			EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[1]), fp32[1]) <<
				std::hex << std::uppercase << std::setfill('0') <<
				"F16 = 0x" << std::setw(4) << fp16[1] << ", " <<
				"F32(F16) = 0x" << std::setw(8) << fp32[1] << ", " <<
				"F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[1]);
			EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[2]), fp32[2]) <<
				std::hex << std::uppercase << std::setfill('0') <<
				"F16 = 0x" << std::setw(4) << fp16[2] << ", " <<
				"F32(F16) = 0x" << std::setw(8) << fp32[2] << ", " <<
				"F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[2]);
			EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[3]), fp32[3]) <<
				std::hex << std::uppercase << std::setfill('0') <<
				"F16 = 0x" << std::setw(4) << fp16[3] << ", " <<
				"F32(F16) = 0x" << std::setw(8) << fp32[3] << ", " <<
				"F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[3]);
		}
	}
}

TEST(FP16_ALT_TO_FP32_PSIMD, positive_denormalized_values) {
	for (uint16_t h = 0; h < 0x0400; h += 4) {
		const psimd_u16 fp16 = {
			(uint16_t) (h + 0),
			(uint16_t) (h + 1),
			(uint16_t) (h + 2),
			(uint16_t) (h + 3),
		};
		const psimd_u32 fp32 = (psimd_u32) fp16_alt_to_fp32_psimd(fp16);

		EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[0]), fp32[0]) <<
			std::hex << std::uppercase << std::setfill('0') <<
			"F16 = 0x" << std::setw(4) << fp16[0] << ", " <<
			"F32(F16) = 0x" << std::setw(8) << fp32[0] << ", " <<
			"F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[0]);
		EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[1]), fp32[1]) <<
			std::hex << std::uppercase << std::setfill('0') <<
			"F16 = 0x" << std::setw(4) << fp16[1] << ", " <<
			"F32(F16) = 0x" << std::setw(8) << fp32[1] << ", " <<
			"F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[1]);
		EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[2]), fp32[2]) <<
			std::hex << std::uppercase << std::setfill('0') <<
			"F16 = 0x" << std::setw(4) << fp16[2] << ", " <<
			"F32(F16) = 0x" << std::setw(8) << fp32[2] << ", " <<
			"F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[2]);
		EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[3]), fp32[3]) <<
			std::hex << std::uppercase << std::setfill('0') <<
			"F16 = 0x" << std::setw(4) << fp16[3] << ", " <<
			"F32(F16) = 0x" << std::setw(8) << fp32[3] << ", " <<
			"F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[3]);
	}
}

TEST(FP16_ALT_TO_FP32_PSIMD, negative_denormalized_values) {
	for (uint16_t h = 0; h < 0x0400; h += 4) {
		const psimd_u16 fp16 = {
			(uint16_t) (h + 0x8000),
			(uint16_t) (h + 0x8001),
			(uint16_t) (h + 0x8002),
			(uint16_t) (h + 0x8003),
		};
		const psimd_u32 fp32 = (psimd_u32) fp16_alt_to_fp32_psimd(fp16);

		EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[0]), fp32[0]) <<
			std::hex << std::uppercase << std::setfill('0') <<
			"F16 = 0x" << std::setw(4) << fp16[0] << ", " <<
			"F32(F16) = 0x" << std::setw(8) << fp32[0] << ", " <<
			"F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[0]);
		EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[1]), fp32[1]) <<
			std::hex << std::uppercase << std::setfill('0') <<
			"F16 = 0x" << std::setw(4) << fp16[1] << ", " <<
			"F32(F16) = 0x" << std::setw(8) << fp32[1] << ", " <<
			"F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[1]);
		EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[2]), fp32[2]) <<
			std::hex << std::uppercase << std::setfill('0') <<
			"F16 = 0x" << std::setw(4) << fp16[2] << ", " <<
			"F32(F16) = 0x" << std::setw(8) << fp32[2] << ", " <<
			"F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[2]);
		EXPECT_EQ(fp16_alt_to_fp32_bits(fp16[3]), fp32[3]) <<
			std::hex << std::uppercase << std::setfill('0') <<
			"F16 = 0x" << std::setw(4) << fp16[3] << ", " <<
			"F32(F16) = 0x" << std::setw(8) << fp32[3] << ", " <<
			"F32 = 0x" << std::setw(8) << fp16_alt_to_fp32_bits(fp16[3]);
	}
}