xref: /aosp_15_r20/external/llvm-libc/test/src/math/smoke/CanonicalizeTest.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Utility class to test canonicalize[f|l] -----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LIBC_TEST_SRC_MATH_SMOKE_CANONICALIZETEST_H
10 #define LLVM_LIBC_TEST_SRC_MATH_SMOKE_CANONICALIZETEST_H
11 
12 #include "src/__support/FPUtil/FEnvImpl.h"
13 #include "src/__support/FPUtil/FPBits.h"
14 #include "src/__support/integer_literals.h"
15 #include "test/UnitTest/FEnvSafeTest.h"
16 #include "test/UnitTest/FPMatcher.h"
17 #include "test/UnitTest/Test.h"
18 
19 #include "hdr/math_macros.h"
20 
21 #define TEST_SPECIAL(x, y, expected, expected_exception)                       \
22   EXPECT_EQ(expected, f(&x, &y));                                              \
23   EXPECT_FP_EXCEPTION(expected_exception);                                     \
24   LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT)
25 
26 #define TEST_REGULAR(x, y, expected) TEST_SPECIAL(x, y, expected, 0)
27 
28 using LIBC_NAMESPACE::operator""_u96;
29 using LIBC_NAMESPACE::operator""_u128;
30 
31 template <typename T>
32 class CanonicalizeTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
33 
34   DECLARE_SPECIAL_CONSTANTS(T)
35 
36 public:
37   typedef int (*CanonicalizeFunc)(T *, const T *);
38 
testSpecialNumbers(CanonicalizeFunc f)39   void testSpecialNumbers(CanonicalizeFunc f) {
40     T cx;
41 
42     TEST_SPECIAL(cx, zero, 0, 0);
43     EXPECT_FP_EQ(cx, zero);
44 
45     TEST_SPECIAL(cx, neg_zero, 0, 0);
46     EXPECT_FP_EQ(cx, neg_zero);
47 
48     TEST_SPECIAL(cx, inf, 0, 0);
49     EXPECT_FP_EQ(cx, inf);
50 
51     TEST_SPECIAL(cx, neg_inf, 0, 0);
52     EXPECT_FP_EQ(cx, neg_inf);
53 
54     TEST_SPECIAL(cx, sNaN, 1, FE_INVALID);
55     EXPECT_FP_EQ(cx, aNaN);
56   }
57 
testX64_80SpecialNumbers(CanonicalizeFunc f)58   void testX64_80SpecialNumbers(CanonicalizeFunc f) {
59     if constexpr (LIBC_NAMESPACE::fputil::get_fp_type<T>() ==
60                   LIBC_NAMESPACE::fputil::FPType::X86_Binary80) {
61       T cx;
62       // Exponent   |       Significand      | Meaning
63       //            | Bits 63-62 | Bits 61-0 |
64       // All Ones   |     00     |    Zero   | Pseudo Infinity, Value = SNaN
65 #if __SIZEOF_LONG_DOUBLE__ == 12
66       FPBits test1(0x00007FFF'00000000'00000000_u96);
67 #else
68       FPBits test1(0x00000000'00007FFF'00000000'00000000_u128);
69 #endif
70       const T test1_val = test1.get_val();
71       TEST_SPECIAL(cx, test1_val, 1, FE_INVALID);
72       EXPECT_FP_EQ(cx, aNaN);
73 
74       // Exponent   |       Significand      | Meaning
75       //            | Bits 63-62 | Bits 61-0 |
76       // All Ones   |     00     |  Non-Zero | Pseudo NaN, Value = SNaN
77 #if __SIZEOF_LONG_DOUBLE__ == 12
78       FPBits test2_1(0x00007FFF'00000000'00000001_u96);
79 #else
80       FPBits test2_1(0x00000000'00007FFF'00000000'00000001_u128);
81 #endif
82       const T test2_1_val = test2_1.get_val();
83       TEST_SPECIAL(cx, test2_1_val, 1, FE_INVALID);
84       EXPECT_FP_EQ(cx, aNaN);
85 
86 #if __SIZEOF_LONG_DOUBLE__ == 12
87       FPBits test2_2(0x00007FFF'00000042'70000001_u96);
88 #else
89       FPBits test2_2(0x00000000'00007FFF'00000042'70000001_u128);
90 #endif
91       const T test2_2_val = test2_2.get_val();
92       TEST_SPECIAL(cx, test2_2_val, 1, FE_INVALID);
93       EXPECT_FP_EQ(cx, aNaN);
94 
95 #if __SIZEOF_LONG_DOUBLE__ == 12
96       FPBits test2_3(0x00007FFF'00000000'08261001_u96);
97 #else
98       FPBits test2_3(0x00000000'00007FFF'00000000'08261001_u128);
99 #endif
100       const T test2_3_val = test2_3.get_val();
101       TEST_SPECIAL(cx, test2_3_val, 1, FE_INVALID);
102       EXPECT_FP_EQ(cx, aNaN);
103 
104 #if __SIZEOF_LONG_DOUBLE__ == 12
105       FPBits test2_4(0x00007FFF'00007800'08261001_u96);
106 #else
107       FPBits test2_4(0x00000000'00007FFF'00007800'08261001_u128);
108 #endif
109       const T test2_4_val = test2_4.get_val();
110       TEST_SPECIAL(cx, test2_4_val, 1, FE_INVALID);
111       EXPECT_FP_EQ(cx, aNaN);
112 
113       // Exponent   |       Significand      | Meaning
114       //            | Bits 63-62 | Bits 61-0 |
115       // All Ones   |     01     | Anything  | Pseudo NaN, Value = SNaN
116 #if __SIZEOF_LONG_DOUBLE__ == 12
117       FPBits test3_1(0x00007FFF'40000000'00000000_u96);
118 #else
119       FPBits test3_1(0x00000000'00007FFF'40000000'00000000_u128);
120 #endif
121       const T test3_1_val = test3_1.get_val();
122       TEST_SPECIAL(cx, test3_1_val, 1, FE_INVALID);
123       EXPECT_FP_EQ(cx, aNaN);
124 
125 #if __SIZEOF_LONG_DOUBLE__ == 12
126       FPBits test3_2(0x00007FFF'40000042'70000001_u96);
127 #else
128       FPBits test3_2(0x00000000'00007FFF'40000042'70000001_u128);
129 #endif
130       const T test3_2_val = test3_2.get_val();
131       TEST_SPECIAL(cx, test3_2_val, 1, FE_INVALID);
132       EXPECT_FP_EQ(cx, aNaN);
133 
134 #if __SIZEOF_LONG_DOUBLE__ == 12
135       FPBits test3_3(0x00007FFF'40000000'08261001_u96);
136 #else
137       FPBits test3_3(0x00000000'00007FFF'40000000'08261001_u128);
138 #endif
139       const T test3_3_val = test3_3.get_val();
140       TEST_SPECIAL(cx, test3_3_val, 1, FE_INVALID);
141       EXPECT_FP_EQ(cx, aNaN);
142 
143 #if __SIZEOF_LONG_DOUBLE__ == 12
144       FPBits test3_4(0x00007FFF'40007800'08261001_u96);
145 #else
146       FPBits test3_4(0x00000000'00007FFF'40007800'08261001_u128);
147 #endif
148       const T test3_4_val = test3_4.get_val();
149       TEST_SPECIAL(cx, test3_4_val, 1, FE_INVALID);
150       EXPECT_FP_EQ(cx, aNaN);
151 
152       // Exponent   |       Significand      | Meaning
153       //            |   Bit 63   | Bits 62-0 |
154       // All zeroes |   One      | Anything  | Pseudo Denormal, Value =
155       //            |            |           | (−1)**s × m × 2**−16382
156 #if __SIZEOF_LONG_DOUBLE__ == 12
157       FPBits test4_1(0x00000000'80000000'00000000_u96);
158 #else
159       FPBits test4_1(0x00000000'00000000'80000000'00000000_u128);
160 #endif
161       const T test4_1_val = test4_1.get_val();
162       TEST_SPECIAL(cx, test4_1_val, 0, 0);
163       EXPECT_FP_EQ(
164           cx, FPBits::make_value(test4_1.get_explicit_mantissa(), 0).get_val());
165 
166 #if __SIZEOF_LONG_DOUBLE__ == 12
167       FPBits test4_2(0x00000000'80000042'70000001_u96);
168 #else
169       FPBits test4_2(0x00000000'00000000'80000042'70000001_u128);
170 #endif
171       const T test4_2_val = test4_2.get_val();
172       TEST_SPECIAL(cx, test4_2_val, 0, 0);
173       EXPECT_FP_EQ(
174           cx, FPBits::make_value(test4_2.get_explicit_mantissa(), 0).get_val());
175 
176 #if __SIZEOF_LONG_DOUBLE__ == 12
177       FPBits test4_3(0x00000000'80000000'08261001_u96);
178 #else
179       FPBits test4_3(0x00000000'00000000'80000000'08261001_u128);
180 #endif
181       const T test4_3_val = test4_3.get_val();
182       TEST_SPECIAL(cx, test4_3_val, 0, 0);
183       EXPECT_FP_EQ(
184           cx, FPBits::make_value(test4_3.get_explicit_mantissa(), 0).get_val());
185 
186       // Exponent   |       Significand      | Meaning
187       //            |   Bit 63   | Bits 62-0 |
188       // All Other  |   Zero     | Anything  | Unnormal, Value = SNaN
189       //  Values    |            |           |
190 #if __SIZEOF_LONG_DOUBLE__ == 12
191       FPBits test5_1(0x00000040'00000000'00000001_u96);
192 #else
193       FPBits test5_1(0x00000000'00000040'00000000'00000001_u128);
194 #endif
195       const T test5_1_val = test5_1.get_val();
196       TEST_SPECIAL(cx, test5_1_val, 1, FE_INVALID);
197       EXPECT_FP_EQ(cx, aNaN);
198 
199 #if __SIZEOF_LONG_DOUBLE__ == 12
200       FPBits test5_2(0x00000230'00000042'70000001_u96);
201 #else
202       FPBits test5_2(0x00000000'00000230'00000042'70000001_u128);
203 #endif
204       const T test5_2_val = test5_2.get_val();
205       TEST_SPECIAL(cx, test5_2_val, 1, FE_INVALID);
206       EXPECT_FP_EQ(cx, aNaN);
207 
208 #if __SIZEOF_LONG_DOUBLE__ == 12
209       FPBits test5_3(0x00000560'00000000'08261001_u96);
210 #else
211       FPBits test5_3(0x00000000'00000560'00000000'08261001_u128);
212 #endif
213       const T test5_3_val = test5_3.get_val();
214       TEST_SPECIAL(cx, test5_3_val, 1, FE_INVALID);
215       EXPECT_FP_EQ(cx, aNaN);
216 
217 #if __SIZEOF_LONG_DOUBLE__ == 12
218       FPBits test5_4(0x00000780'00000028'16000000_u96);
219 #else
220       FPBits test5_4(0x00000000'00000780'00000028'16000000_u128);
221 #endif
222       const T test5_4_val = test5_4.get_val();
223       TEST_SPECIAL(cx, test5_4_val, 1, FE_INVALID);
224       EXPECT_FP_EQ(cx, aNaN);
225 
226 #if __SIZEOF_LONG_DOUBLE__ == 12
227       FPBits test5_5(0x00000900'00000042'70000001_u96);
228 #else
229       FPBits test5_5(0x00000000'00000900'00000042'70000001_u128);
230 #endif
231       const T test5_5_val = test5_5.get_val();
232       TEST_SPECIAL(cx, test5_5_val, 1, FE_INVALID);
233       EXPECT_FP_EQ(cx, aNaN);
234 
235 #if __SIZEOF_LONG_DOUBLE__ == 12
236       FPBits test5_6(0x00000AB0'00000000'08261001_u96);
237 #else
238       FPBits test5_6(0x00000000'00000AB0'00000000'08261001_u128);
239 #endif
240       const T test5_6_val = test5_6.get_val();
241       TEST_SPECIAL(cx, test5_6_val, 1, FE_INVALID);
242       EXPECT_FP_EQ(cx, aNaN);
243     }
244   }
245 
testRegularNumbers(CanonicalizeFunc f)246   void testRegularNumbers(CanonicalizeFunc f) {
247     T cx;
248     const T test_var_1 = T(1.0);
249     TEST_REGULAR(cx, test_var_1, 0);
250     EXPECT_FP_EQ(cx, test_var_1);
251     const T test_var_2 = T(-1.0);
252     TEST_REGULAR(cx, test_var_2, 0);
253     EXPECT_FP_EQ(cx, test_var_2);
254     const T test_var_3 = T(10.0);
255     TEST_REGULAR(cx, test_var_3, 0);
256     EXPECT_FP_EQ(cx, test_var_3);
257     const T test_var_4 = T(-10.0);
258     TEST_REGULAR(cx, test_var_4, 0);
259     EXPECT_FP_EQ(cx, test_var_4);
260     const T test_var_5 = T(1234.0);
261     TEST_REGULAR(cx, test_var_5, 0);
262     EXPECT_FP_EQ(cx, test_var_5);
263     const T test_var_6 = T(-1234.0);
264     TEST_REGULAR(cx, test_var_6, 0);
265     EXPECT_FP_EQ(cx, test_var_6);
266   }
267 };
268 
269 #define LIST_CANONICALIZE_TESTS(T, func)                                       \
270   using LlvmLibcCanonicalizeTest = CanonicalizeTest<T>;                        \
271   TEST_F(LlvmLibcCanonicalizeTest, SpecialNumbers) {                           \
272     testSpecialNumbers(&func);                                                 \
273   }                                                                            \
274   TEST_F(LlvmLibcCanonicalizeTest, RegularNubmers) {                           \
275     testRegularNumbers(&func);                                                 \
276   }
277 
278 #define X86_80_SPECIAL_CANONICALIZE_TEST(T, func)                              \
279   using LlvmLibcCanonicalizeTest = CanonicalizeTest<T>;                        \
280   TEST_F(LlvmLibcCanonicalizeTest, X64_80SpecialNumbers) {                     \
281     testX64_80SpecialNumbers(&func);                                           \
282   }
283 
284 #endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_CANONICALIZETEST_H
285