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/FPBits.h"
13#include "src/__support/integer_literals.h"
14#include "test/UnitTest/FEnvSafeTest.h"
15#include "test/UnitTest/FPMatcher.h"
16#include "test/UnitTest/Test.h"
17
18#include "hdr/math_macros.h"
19
20#define TEST_SPECIAL(x, y, expected, expected_exception) \
21 EXPECT_EQ(expected, f(&x, &y)); \
22 EXPECT_FP_EXCEPTION(expected_exception); \
23 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT)
24
25#define TEST_REGULAR(x, y, expected) TEST_SPECIAL(x, y, expected, 0)
26
27using LIBC_NAMESPACE::operator""_u128;
28
29template <typename T>
30class CanonicalizeTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
31
32 DECLARE_SPECIAL_CONSTANTS(T)
33
34public:
35 typedef int (*CanonicalizeFunc)(T *, const T *);
36
37 void testSpecialNumbers(CanonicalizeFunc f) {
38 T cx;
39
40 TEST_SPECIAL(cx, zero, 0, 0);
41 EXPECT_FP_EQ(cx, zero);
42
43 TEST_SPECIAL(cx, neg_zero, 0, 0);
44 EXPECT_FP_EQ(cx, neg_zero);
45
46 TEST_SPECIAL(cx, inf, 0, 0);
47 EXPECT_FP_EQ(cx, inf);
48
49 TEST_SPECIAL(cx, neg_inf, 0, 0);
50 EXPECT_FP_EQ(cx, neg_inf);
51
52 TEST_SPECIAL(cx, sNaN, 1, FE_INVALID);
53 EXPECT_FP_EQ(cx, aNaN);
54 }
55
56 void testX64_80SpecialNumbers(CanonicalizeFunc f) {
57 if constexpr (LIBC_NAMESPACE::fputil::get_fp_type<T>() ==
58 LIBC_NAMESPACE::fputil::FPType::X86_Binary80) {
59 T cx;
60 // Exponent | Significand | Meaning
61 // | Bits 63-62 | Bits 61-0 |
62 // All Ones | 00 | Zero | Pseudo Infinity, Value = SNaN
63 FPBits test1(0x00000000'00007FFF'00000000'00000000_u128);
64 const T test1_val = test1.get_val();
65 TEST_SPECIAL(cx, test1_val, 1, FE_INVALID);
66 EXPECT_FP_EQ(cx, aNaN);
67
68 // Exponent | Significand | Meaning
69 // | Bits 63-62 | Bits 61-0 |
70 // All Ones | 00 | Non-Zero | Pseudo NaN, Value = SNaN
71 FPBits test2_1(0x00000000'00007FFF'00000000'00000001_u128);
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);
75
76 FPBits test2_2(0x00000000'00007FFF'00000042'70000001_u128);
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);
80
81 FPBits test2_3(0x00000000'00007FFF'00000000'08261001_u128);
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(0x00000000'00007FFF'00007800'08261001_u128);
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);
90
91 // Exponent | Significand | Meaning
92 // | Bits 63-62 | Bits 61-0 |
93 // All Ones | 01 | Anything | Pseudo NaN, Value = SNaN
94 FPBits test3_1(0x00000000'00007FFF'40000000'00000000_u128);
95 const T test3_1_val = test3_1.get_val();
96 TEST_SPECIAL(cx, test3_1_val, 1, FE_INVALID);
97 EXPECT_FP_EQ(cx, aNaN);
98
99 FPBits test3_2(0x00000000'00007FFF'40000042'70000001_u128);
100 const T test3_2_val = test3_2.get_val();
101 TEST_SPECIAL(cx, test3_2_val, 1, FE_INVALID);
102 EXPECT_FP_EQ(cx, aNaN);
103
104 FPBits test3_3(0x00000000'00007FFF'40000000'08261001_u128);
105 const T test3_3_val = test3_3.get_val();
106 TEST_SPECIAL(cx, test3_3_val, 1, FE_INVALID);
107 EXPECT_FP_EQ(cx, aNaN);
108
109 FPBits test3_4(0x00000000'00007FFF'40007800'08261001_u128);
110 const T test3_4_val = test3_4.get_val();
111 TEST_SPECIAL(cx, test3_4_val, 1, FE_INVALID);
112 EXPECT_FP_EQ(cx, aNaN);
113
114 // Exponent | Significand | Meaning
115 // | Bit 63 | Bits 62-0 |
116 // All zeroes | One | Anything | Pseudo Denormal, Value =
117 // | | | (−1)**s × m × 2**−16382
118 FPBits test4_1(0x00000000'00000000'80000000'00000000_u128);
119 const T test4_1_val = test4_1.get_val();
120 TEST_SPECIAL(cx, test4_1_val, 0, 0);
121 EXPECT_FP_EQ(
122 cx, FPBits::make_value(test4_1.get_explicit_mantissa(), 0).get_val());
123
124 FPBits test4_2(0x00000000'00000000'80000042'70000001_u128);
125 const T test4_2_val = test4_2.get_val();
126 TEST_SPECIAL(cx, test4_2_val, 0, 0);
127 EXPECT_FP_EQ(
128 cx, FPBits::make_value(test4_2.get_explicit_mantissa(), 0).get_val());
129
130 FPBits test4_3(0x00000000'00000000'80000000'08261001_u128);
131 const T test4_3_val = test4_3.get_val();
132 TEST_SPECIAL(cx, test4_3_val, 0, 0);
133 EXPECT_FP_EQ(
134 cx, FPBits::make_value(test4_3.get_explicit_mantissa(), 0).get_val());
135
136 // Exponent | Significand | Meaning
137 // | Bit 63 | Bits 62-0 |
138 // All Other | Zero | Anything | Unnormal, Value = SNaN
139 // Values | | |
140 FPBits test5_1(0x00000000'00000040'00000000'00000001_u128);
141 const T test5_1_val = test5_1.get_val();
142 TEST_SPECIAL(cx, test5_1_val, 1, FE_INVALID);
143 EXPECT_FP_EQ(cx, aNaN);
144
145 FPBits test5_2(0x00000000'00000230'00000042'70000001_u128);
146 const T test5_2_val = test5_2.get_val();
147 TEST_SPECIAL(cx, test5_2_val, 1, FE_INVALID);
148 EXPECT_FP_EQ(cx, aNaN);
149
150 FPBits test5_3(0x00000000'00000560'00000000'08261001_u128);
151 const T test5_3_val = test5_3.get_val();
152 TEST_SPECIAL(cx, test5_3_val, 1, FE_INVALID);
153 EXPECT_FP_EQ(cx, aNaN);
154
155 FPBits test5_4(0x00000000'00000780'00000028'16000000_u128);
156 const T test5_4_val = test5_4.get_val();
157 TEST_SPECIAL(cx, test5_4_val, 1, FE_INVALID);
158 EXPECT_FP_EQ(cx, aNaN);
159
160 FPBits test5_5(0x00000000'00000900'00000042'70000001_u128);
161 const T test5_5_val = test5_5.get_val();
162 TEST_SPECIAL(cx, test5_5_val, 1, FE_INVALID);
163 EXPECT_FP_EQ(cx, aNaN);
164
165 FPBits test5_6(0x00000000'00000AB0'00000000'08261001_u128);
166 const T test5_6_val = test5_6.get_val();
167 TEST_SPECIAL(cx, test5_6_val, 1, FE_INVALID);
168 EXPECT_FP_EQ(cx, aNaN);
169 }
170 }
171
172 void testRegularNumbers(CanonicalizeFunc f) {
173 T cx;
174 const T test_var_1 = T(1.0);
175 TEST_REGULAR(cx, test_var_1, 0);
176 EXPECT_FP_EQ(cx, test_var_1);
177 const T test_var_2 = T(-1.0);
178 TEST_REGULAR(cx, test_var_2, 0);
179 EXPECT_FP_EQ(cx, test_var_2);
180 const T test_var_3 = T(10.0);
181 TEST_REGULAR(cx, test_var_3, 0);
182 EXPECT_FP_EQ(cx, test_var_3);
183 const T test_var_4 = T(-10.0);
184 TEST_REGULAR(cx, test_var_4, 0);
185 EXPECT_FP_EQ(cx, test_var_4);
186 const T test_var_5 = T(1234.0);
187 TEST_REGULAR(cx, test_var_5, 0);
188 EXPECT_FP_EQ(cx, test_var_5);
189 const T test_var_6 = T(-1234.0);
190 TEST_REGULAR(cx, test_var_6, 0);
191 EXPECT_FP_EQ(cx, test_var_6);
192 }
193};
194
195#define LIST_CANONICALIZE_TESTS(T, func) \
196 using LlvmLibcCanonicalizeTest = CanonicalizeTest<T>; \
197 TEST_F(LlvmLibcCanonicalizeTest, SpecialNumbers) { \
198 testSpecialNumbers(&func); \
199 } \
200 TEST_F(LlvmLibcCanonicalizeTest, RegularNubmers) { \
201 testRegularNumbers(&func); \
202 }
203
204#define X86_80_SPECIAL_CANONICALIZE_TEST(T, func) \
205 using LlvmLibcCanonicalizeTest = CanonicalizeTest<T>; \
206 TEST_F(LlvmLibcCanonicalizeTest, X64_80SpecialNumbers) { \
207 testX64_80SpecialNumbers(&func); \
208 }
209
210#endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_CANONICALIZETEST_H
211

source code of libc/test/src/math/smoke/CanonicalizeTest.h