• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <gtest/gtest.h>
2 
3 #include <cstdint>
4 
5 #include <fp16.h>
6 
7 
TEST(FP32_TO_BITS,positive)8 TEST(FP32_TO_BITS, positive) {
9 	for (uint32_t bits = UINT32_C(0x00000000); bits <= UINT32_C(0x7FFFFFFF); bits++) {
10 		float value;
11 		memcpy(&value, &bits, sizeof(value));
12 
13 		ASSERT_EQ(bits, fp32_to_bits(value)) <<
14 			std::hex << std::uppercase << std::setfill('0') <<
15 			"BITS = 0x" << std::setw(8) << bits << ", " <<
16 			"BITCAST(VALUE) = 0x" << std::setw(8) << fp32_to_bits(value);
17 	}
18 }
19 
TEST(FP32_TO_BITS,negative)20 TEST(FP32_TO_BITS, negative) {
21 	for (uint32_t bits = UINT32_C(0xFFFFFFFF); bits >= UINT32_C(0x80000000); bits--) {
22 		float value;
23 		memcpy(&value, &bits, sizeof(value));
24 
25 		ASSERT_EQ(bits, fp32_to_bits(value)) <<
26 			std::hex << std::uppercase << std::setfill('0') <<
27 			"BITS = 0x" << std::setw(8) << bits << ", " <<
28 			"BITCAST(VALUE) = 0x" << std::setw(8) << fp32_to_bits(value);
29 	}
30 }
31 
TEST(FP32_FROM_BITS,positive)32 TEST(FP32_FROM_BITS, positive) {
33 	for (uint32_t bits = UINT32_C(0x00000000); bits <= UINT32_C(0x7FFFFFFF); bits++) {
34 		const float value = fp32_from_bits(bits);
35 		uint32_t bitcast;
36 		memcpy(&bitcast, &value, sizeof(bitcast));
37 
38 		ASSERT_EQ(bits, bitcast) <<
39 			std::hex << std::uppercase << std::setfill('0') <<
40 			"BITS = 0x" << std::setw(8) << bits << ", " <<
41 			"VALUE = 0x" << std::setw(8) << bitcast;
42 	}
43 }
44 
TEST(FP32_FROM_BITS,negative)45 TEST(FP32_FROM_BITS, negative) {
46 	for (uint32_t bits = UINT32_C(0xFFFFFFFF); bits >= UINT32_C(0x80000000); bits--) {
47 		const float value = fp32_from_bits(bits);
48 		uint32_t bitcast;
49 		memcpy(&bitcast, &value, sizeof(bitcast));
50 
51 		ASSERT_EQ(bits, bitcast) <<
52 			std::hex << std::uppercase << std::setfill('0') <<
53 			"BITS = 0x" << std::setw(8) << bits << ", " <<
54 			"VALUE = 0x" << std::setw(8) << bitcast;
55 	}
56 }
57