|
16 | 16 | #include "include/llvm-libc-macros/math-macros.h"
|
17 | 17 |
|
18 | 18 | #define TEST_SPECIAL(x, y, expected, expected_exception) \
|
19 |
| - EXPECT_FP_EQ(expected, f(&x, &y)); \ |
| 19 | + EXPECT_EQ(expected, f(&x, &y)); \ |
20 | 20 | EXPECT_FP_EXCEPTION(expected_exception); \
|
21 | 21 | LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT)
|
22 | 22 |
|
23 | 23 | #define TEST_REGULAR(x, y, expected) TEST_SPECIAL(x, y, expected, 0)
|
24 | 24 |
|
| 25 | +#define LIBC_NAMESPACE __llvm_libc_19_0_0_git |
| 26 | + |
25 | 27 | template <typename T>
|
26 | 28 | class CanonicalizeTest : public LIBC_NAMESPACE::testing::Test {
|
27 | 29 |
|
28 | 30 | DECLARE_SPECIAL_CONSTANTS(T)
|
29 | 31 |
|
30 | 32 | public:
|
31 |
| - typedef T (*CanonicalizeFunc)(T *, T *); |
32 |
| - using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>; |
33 |
| - using StorageType = typename FPBits::StorageType; |
| 33 | + typedef int (*CanonicalizeFunc)(T *, const T *); |
34 | 34 |
|
35 | 35 | void testSpecialNumbers(CanonicalizeFunc f) {
|
36 | 36 | T cx;
|
| 37 | + |
37 | 38 | TEST_SPECIAL(cx, zero, 0, 0);
|
38 |
| - EXPECT_EQ(cx, T(0.0)); |
| 39 | + EXPECT_FP_EQ(cx, zero); |
| 40 | + |
39 | 41 | TEST_SPECIAL(cx, neg_zero, 0, 0);
|
40 |
| - EXPECT_EQ(cx, T(-0.0)); |
| 42 | + EXPECT_FP_EQ(cx, neg_zero); |
| 43 | + |
41 | 44 | TEST_SPECIAL(cx, inf, 0, 0);
|
42 |
| - EXPECT_EQ(cx, inf); |
| 45 | + EXPECT_FP_EQ(cx, inf); |
| 46 | + |
43 | 47 | TEST_SPECIAL(cx, neg_inf, 0, 0);
|
44 |
| - EXPECT_EQ(cx, neg_inf); |
| 48 | + EXPECT_FP_EQ(cx, neg_inf); |
| 49 | + |
45 | 50 | TEST_SPECIAL(cx, sNaN, 1, FE_INVALID);
|
46 |
| - EXPECT_EQ(cx, aNaN); |
47 |
| - TEST_SPECIAL(cx, -sNaN, 1, FE_INVALID); |
48 |
| - EXPECT_EQ(cx, -aNaN); |
| 51 | + EXPECT_FP_EQ(cx, aNaN); |
49 | 52 | }
|
50 | 53 |
|
51 | 54 | void testX64_80SpecialNumbers(CanonicalizeFunc f) {
|
52 |
| - if constexpr (LIBC_NAMESPACE::fputil::get_fp_type() == |
53 |
| - FPType::X86_Binary80) { |
| 55 | + if constexpr (LIBC_NAMESPACE::fputil::get_fp_type<T>() == |
| 56 | + LIBC_NAMESPACE::fputil::FPType::X86_Binary80) { |
54 | 57 | T cx;
|
55 | 58 | // Exponent | Significand | Meaning
|
56 | 59 | // | Bits 63-62 | Bits 61-0 |
|
57 | 60 | // All Ones | 00 | Zero | Pseudo Infinity, Value = Infinty
|
58 | 61 |
|
59 |
| - FPBits test1(UInt128(0x7FFF) << 64 + UInt128(0x0000000000000000)); |
60 |
| - TEST_SPECIAL(cx, test1.get_val(), 0, 0); |
61 |
| - EXPECT_EQ(cx, inf); |
| 62 | + FPBits test1((UInt128(0x7FFF) << 64) + UInt128(0x0000000000000000)); |
| 63 | + const T test1_val = test1.get_val(); |
| 64 | + TEST_SPECIAL(cx, test1_val, 0, 0); |
| 65 | + EXPECT_FP_EQ(cx, inf); |
62 | 66 |
|
63 | 67 | // Exponent | Significand | Meaning
|
64 | 68 | // | Bits 63-62 | Bits 61-0 |
|
65 | 69 | // All Ones | 00 | Non-Zero | Pseudo NaN, Value = SNaN
|
66 | 70 |
|
67 |
| - FPBits test2_1(UInt128(0x7FFF) << 64 + UInt128(0x0000000000000001)); |
68 |
| - TEST_SPECIAL(cx, test2_1.get_val(), 1, FE_INVALID); |
69 |
| - EXPECT_EQ(cx, aNaN); |
| 71 | + FPBits test2_1((UInt128(0x7FFF) << 64) + UInt128(0x0000000000000001)); |
| 72 | + const T test2_1_val = test2_1.get_val(); |
| 73 | + TEST_SPECIAL(cx, test2_1_val, 1, FE_INVALID); |
| 74 | + EXPECT_FP_EQ(cx, aNaN); |
70 | 75 |
|
71 |
| - FPBits test2_2(UInt128(0x7FFF) << 64 + UInt128(0x0000004270000001)); |
72 |
| - TEST_SPECIAL(cx, test2_2.get_val(), 1, FE_INVALID); |
73 |
| - EXPECT_EQ(cx, aNaN); |
| 76 | + FPBits test2_2((UInt128(0x7FFF) << 64) + UInt128(0x0000004270000001)); |
| 77 | + const T test2_2_val = test2_2.get_val(); |
| 78 | + TEST_SPECIAL(cx, test2_2_val, 1, FE_INVALID); |
| 79 | + EXPECT_FP_EQ(cx, aNaN); |
74 | 80 |
|
75 |
| - FPBits test2_3(UInt128(0x7FFF) << 64 + UInt128(0x0000000008261001)); |
76 |
| - TEST_SPECIAL(cx, test2_3.get_val(), 1, FE_INVALID); |
77 |
| - EXPECT_EQ(cx, aNaN); |
| 81 | + FPBits test2_3((UInt128(0x7FFF) << 64) + UInt128(0x0000000008261001)); |
| 82 | + const T test2_3_val = test2_3.get_val(); |
| 83 | + TEST_SPECIAL(cx, test2_3_val, 1, FE_INVALID); |
| 84 | + EXPECT_FP_EQ(cx, aNaN); |
| 85 | + |
| 86 | + FPBits test2_4((UInt128(0x7FFF) << 64) + UInt128(0x0000780008261001)); |
| 87 | + const T test2_4_val = test2_4.get_val(); |
| 88 | + TEST_SPECIAL(cx, test2_4_val, 1, FE_INVALID); |
| 89 | + EXPECT_FP_EQ(cx, aNaN); |
78 | 90 |
|
79 | 91 | // Exponent | Significand | Meaning
|
80 | 92 | // | Bits 63-62 | Bits 61-0 |
|
81 | 93 | // All Ones | 01 | Anything | Pseudo NaN, Value = SNaN
|
82 | 94 |
|
83 |
| - FPBits test3_1(UInt128(0x7FFF) << 64 + UInt128(0x4000000000000000)); |
84 |
| - TEST_SPECIAL(cx, test3_1.get_val(), 1, FE_INVALID); |
85 |
| - EXPECT_EQ(cx, aNaN); |
| 95 | + FPBits test3_1((UInt128(0x7FFF) << 64) + UInt128(0x4000000000000000)); |
| 96 | + const T test3_1_val = test3_1.get_val(); |
| 97 | + TEST_SPECIAL(cx, test3_1_val, 1, FE_INVALID); |
| 98 | + EXPECT_FP_EQ(cx, aNaN); |
| 99 | + |
| 100 | + FPBits test3_2((UInt128(0x7FFF) << 64) + UInt128(0x4000004270000001)); |
| 101 | + const T test3_2_val = test3_2.get_val(); |
| 102 | + TEST_SPECIAL(cx, test3_2_val, 1, FE_INVALID); |
| 103 | + EXPECT_FP_EQ(cx, aNaN); |
86 | 104 |
|
87 |
| - FPBits test3_2(UInt128(0x7FFF) << 64 + UInt128(0x4000004270000001)); |
88 |
| - TEST_SPECIAL(cx, test3_2.get_val(), 1, FE_INVALID); |
89 |
| - EXPECT_EQ(cx, aNaN); |
| 105 | + FPBits test3_3((UInt128(0x7FFF) << 64) + UInt128(0x4000000008261001)); |
| 106 | + const T test3_3_val = test3_3.get_val(); |
| 107 | + TEST_SPECIAL(cx, test3_3_val, 1, FE_INVALID); |
| 108 | + EXPECT_FP_EQ(cx, aNaN); |
90 | 109 |
|
91 |
| - FPBits test3_3(UInt128(0x7FFF) << 64 + UInt128(0x4000000008261001)); |
92 |
| - TEST_SPECIAL(cx, test3_3.get_val(), 1, FE_INVALID); |
93 |
| - EXPECT_EQ(cx, aNaN); |
| 110 | + FPBits test3_4((UInt128(0x7FFF) << 64) + UInt128(0x4007800008261001)); |
| 111 | + const T test3_4_val = test3_4.get_val(); |
| 112 | + TEST_SPECIAL(cx, test3_4_val, 1, FE_INVALID); |
| 113 | + EXPECT_FP_EQ(cx, aNaN); |
94 | 114 |
|
95 | 115 | // Exponent | Significand | Meaning
|
96 | 116 | // | Bit 63 | Bits 62-0 |
|
97 | 117 | // All zeroes | One | Anything | Pseudo Denormal, Value =
|
98 | 118 | // | | | (−1)**s × m × 2**−16382
|
99 | 119 |
|
100 |
| - FPBits test4_1(UInt128(0x0000) << 64 + UInt128(0x8000000000000000)); |
101 |
| - TEST_SPECIAL(cx, test4_1.get_val(), 0, 0); |
102 |
| - EXPECT_EQ( |
103 |
| - cx, |
104 |
| - FPBits::make_value(test4_1.get_explicit_mantissa(), 1).get_val();); |
| 120 | + FPBits test4_1((UInt128(0x0000) << 64) + UInt128(0x8000000000000000)); |
| 121 | + const T test4_1_val = test4_1.get_val(); |
| 122 | + TEST_SPECIAL(cx, test4_1_val, 0, 0); |
| 123 | + EXPECT_FP_EQ( |
| 124 | + cx, FPBits::make_value(test4_1.get_explicit_mantissa(), 0).get_val()); |
105 | 125 |
|
106 |
| - FPBits test4_2(UInt128(0x0000) << 64 + UInt128(0x8000004270000001)); |
107 |
| - TEST_SPECIAL(cx, test4_2.get_val(), 0, 0); |
108 |
| - EXPECT_EQ( |
109 |
| - cx, |
110 |
| - FPBits::make_value(test4_2.get_explicit_mantissa(), 1).get_val();); |
| 126 | + FPBits test4_2((UInt128(0x0000) << 64) + UInt128(0x8000004270000001)); |
| 127 | + const T test4_2_val = test4_2.get_val(); |
| 128 | + TEST_SPECIAL(cx, test4_2_val, 0, 0); |
| 129 | + EXPECT_FP_EQ( |
| 130 | + cx, FPBits::make_value(test4_2.get_explicit_mantissa(), 0).get_val()); |
111 | 131 |
|
112 |
| - FPBits test4_3(UInt128(0x0000) << 64 + UInt128(0x8000000008261001)); |
113 |
| - TEST_SPECIAL(cx, test4_3.get_val(), 0, 0); |
114 |
| - EXPECT_EQ( |
115 |
| - cx, |
116 |
| - FPBits::make_value(test4_3.get_explicit_mantissa(), 1).get_val();); |
| 132 | + FPBits test4_3((UInt128(0x0000) << 64) + UInt128(0x8000000008261001)); |
| 133 | + const T test4_3_val = test4_3.get_val(); |
| 134 | + TEST_SPECIAL(cx, test4_3_val, 0, 0); |
| 135 | + EXPECT_FP_EQ( |
| 136 | + cx, FPBits::make_value(test4_3.get_explicit_mantissa(), 0).get_val()); |
117 | 137 |
|
118 | 138 | // Exponent | Significand | Meaning
|
119 | 139 | // | Bit 63 | Bits 62-0 |
|
120 | 140 | // All Other | Zero | Anything | Unnormal, Value =
|
121 | 141 | // Values | | | (−1)**s × m × 2**−16382
|
122 | 142 |
|
123 |
| - FPBits test5_1(UInt128(0x0001) << 64 + UInt128(0x0000000000000000)); |
124 |
| - TEST_SPECIAL(cx, test5_1.get_val(), 0, 0); |
125 |
| - EXPECT_EQ( |
126 |
| - cx, |
127 |
| - FPBits::make_value(test5_1.get_explicit_mantissa(), 1).get_val();); |
128 |
| - |
129 |
| - FPBits test5_2(UInt128(0x0001) << 64 + UInt128(0x0000004270000001)); |
130 |
| - TEST_SPECIAL(cx, test5_2.get_val(), 0, 0); |
131 |
| - EXPECT_EQ( |
132 |
| - cx, |
133 |
| - FPBits::make_value(test5_2.get_explicit_mantissa(), 1).get_val();); |
134 |
| - |
135 |
| - FPBits test5_3(UInt128(0x0001) << 64 + UInt128(0x0000000008261001)); |
136 |
| - TEST_SPECIAL(cx, test5_3.get_val(), 0, 0); |
137 |
| - EXPECT_EQ( |
138 |
| - cx, |
139 |
| - FPBits::make_value(test5_3.get_explicit_mantissa(), 1).get_val();); |
140 |
| - |
141 |
| - FPBits test5_4(UInt128(0x0012) << 64 + UInt128(0x0000000000000000)); |
142 |
| - TEST_SPECIAL(cx, test5_4.get_val(), 0, 0); |
143 |
| - EXPECT_EQ( |
144 |
| - cx, |
145 |
| - FPBits::make_value(test5_4.get_explicit_mantissa(), 1).get_val();); |
146 |
| - |
147 |
| - FPBits test5_5(UInt128(0x0027) << 64 + UInt128(0x0000004270000001)); |
148 |
| - TEST_SPECIAL(cx, test5_5.get_val(), 0, 0); |
149 |
| - EXPECT_EQ( |
150 |
| - cx, |
151 |
| - FPBits::make_value(test5_5.get_explicit_mantissa(), 1).get_val();); |
152 |
| - |
153 |
| - FPBits test5_6(UInt128(0x0034) << 64 + UInt128(0x0000000008261001)); |
154 |
| - TEST_SPECIAL(cx, test5_6.get_val(), 0, 0); |
155 |
| - EXPECT_EQ( |
156 |
| - cx, |
157 |
| - FPBits::make_value(test5_6.get_explicit_mantissa(), 1).get_val();); |
| 143 | + FPBits test5_1(UInt128(0x0000000000000001)); |
| 144 | + int exponent = test5_1.get_biased_exponent(); |
| 145 | + const T test5_1_val = test5_1.get_val(); |
| 146 | + TEST_SPECIAL(cx, test5_1_val, 0, 0); |
| 147 | + EXPECT_FP_EQ( |
| 148 | + cx, FPBits::make_value(test5_1.get_explicit_mantissa(), 0).get_val()); |
| 149 | + |
| 150 | + FPBits test5_2(UInt128(0x0000004270000001)); |
| 151 | + const T test5_2_val = test5_2.get_val(); |
| 152 | + TEST_SPECIAL(cx, test5_2_val, 0, 0); |
| 153 | + EXPECT_FP_EQ( |
| 154 | + cx, FPBits::make_value(test5_2.get_explicit_mantissa(), 0).get_val()); |
| 155 | + |
| 156 | + FPBits test5_3(UInt128(0x0000000008261001)); |
| 157 | + const T test5_3_val = test5_3.get_val(); |
| 158 | + TEST_SPECIAL(cx, test5_3_val, 0, 0); |
| 159 | + EXPECT_FP_EQ( |
| 160 | + cx, FPBits::make_value(test5_3.get_explicit_mantissa(), 0).get_val()); |
| 161 | + |
| 162 | + FPBits test5_4(UInt128(0x0000002816000000)); |
| 163 | + const T test5_4_val = test5_4.get_val(); |
| 164 | + TEST_SPECIAL(cx, test5_4_val, 0, 0); |
| 165 | + EXPECT_FP_EQ( |
| 166 | + cx, FPBits::make_value(test5_4.get_explicit_mantissa(), 0).get_val()); |
| 167 | + |
| 168 | + FPBits test5_5(UInt128(0x0000004270000001)); |
| 169 | + const T test5_5_val = test5_5.get_val(); |
| 170 | + TEST_SPECIAL(cx, test5_5_val, 0, 0); |
| 171 | + EXPECT_FP_EQ( |
| 172 | + cx, FPBits::make_value(test5_5.get_explicit_mantissa(), 0).get_val()); |
| 173 | + |
| 174 | + FPBits test5_6(UInt128(0x0000000008261001)); |
| 175 | + const T test5_6_val = test5_6.get_val(); |
| 176 | + TEST_SPECIAL(cx, test5_6_val, 0, 0); |
| 177 | + EXPECT_FP_EQ( |
| 178 | + cx, FPBits::make_value(test5_6.get_explicit_mantissa(), 0).get_val()); |
158 | 179 | }
|
159 | 180 | }
|
160 | 181 |
|
161 |
| - void testRegularNumbers(CanonicalizeFunc func) { |
| 182 | + void testRegularNumbers(CanonicalizeFunc f) { |
162 | 183 | T cx;
|
163 |
| - TEST_REGULAR(cx, T(1.0), 0); |
164 |
| - EXPECT_EQ(cx, T(1.0)); |
165 |
| - TEST_REGULAR(cx, T(-1.0), 0); |
166 |
| - EXPECT_EQ(cx, T(-1.0)); |
167 |
| - TEST_REGULAR(cx, T(10.0), 0); |
168 |
| - EXPECT_EQ(cx, T(10.0)); |
169 |
| - TEST_REGULAR(cx, T(-10.0), 0); |
170 |
| - EXPECT_EQ(cx, T(-10.0)); |
171 |
| - TEST_REGULAR(cx, T(1234.0), 0); |
172 |
| - EXPECT_EQ(cx, T(1234.0)); |
173 |
| - TEST_REGULAR(cx, T(-1234.0), 0); |
174 |
| - EXPECT_EQ(cx, T(-1234.0)); |
| 184 | + const T test_var_1 = T(1.0); |
| 185 | + TEST_REGULAR(cx, test_var_1, 0); |
| 186 | + EXPECT_FP_EQ(cx, test_var_1); |
| 187 | + const T test_var_2 = T(-1.0); |
| 188 | + TEST_REGULAR(cx, test_var_2, 0); |
| 189 | + EXPECT_FP_EQ(cx, test_var_2); |
| 190 | + const T test_var_3 = T(10.0); |
| 191 | + TEST_REGULAR(cx, test_var_3, 0); |
| 192 | + EXPECT_FP_EQ(cx, test_var_3); |
| 193 | + const T test_var_4 = T(-10.0); |
| 194 | + TEST_REGULAR(cx, test_var_4, 0); |
| 195 | + EXPECT_FP_EQ(cx, test_var_4); |
| 196 | + const T test_var_5 = T(1234.0); |
| 197 | + TEST_REGULAR(cx, test_var_5, 0); |
| 198 | + EXPECT_FP_EQ(cx, test_var_5); |
| 199 | + const T test_var_6 = T(-1234.0); |
| 200 | + TEST_REGULAR(cx, test_var_6, 0); |
| 201 | + EXPECT_FP_EQ(cx, test_var_6); |
175 | 202 | }
|
176 | 203 | };
|
177 | 204 |
|
|
0 commit comments