1 //===-- FPMatchers.h --------------------------------------------*- 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_UNITTEST_FPMATCHER_H
10 #define LLVM_LIBC_TEST_UNITTEST_FPMATCHER_H
11
12 #include "src/__support/CPP/array.h"
13 #include "src/__support/CPP/type_traits.h"
14 #include "src/__support/CPP/type_traits/is_complex.h"
15 #include "src/__support/FPUtil/FEnvImpl.h"
16 #include "src/__support/FPUtil/FPBits.h"
17 #include "src/__support/FPUtil/fpbits_str.h"
18 #include "src/__support/macros/config.h"
19 #include "src/__support/macros/properties/architectures.h"
20 #include "test/UnitTest/RoundingModeUtils.h"
21 #include "test/UnitTest/StringUtils.h"
22 #include "test/UnitTest/Test.h"
23
24 #include "hdr/math_macros.h"
25
26 using LIBC_NAMESPACE::Sign;
27
28 namespace LIBC_NAMESPACE_DECL {
29 namespace testing {
30
31 template <typename T, TestCond Condition> class FPMatcher : public Matcher<T> {
32 static_assert(cpp::is_floating_point_v<T>,
33 "FPMatcher can only be used with floating point values.");
34 static_assert(Condition == TestCond::EQ || Condition == TestCond::NE,
35 "Unsupported FPMatcher test condition.");
36
37 T expected;
38 T actual;
39
40 public:
FPMatcher(T expectedValue)41 FPMatcher(T expectedValue) : expected(expectedValue) {}
42
match(T actualValue)43 bool match(T actualValue) {
44 actual = actualValue;
45 fputil::FPBits<T> actualBits(actual), expectedBits(expected);
46 if (Condition == TestCond::EQ)
47 return (actualBits.is_nan() && expectedBits.is_nan()) ||
48 (actualBits.uintval() == expectedBits.uintval());
49
50 // If condition == TestCond::NE.
51 if (actualBits.is_nan())
52 return !expectedBits.is_nan();
53 return expectedBits.is_nan() ||
54 (actualBits.uintval() != expectedBits.uintval());
55 }
56
explainError()57 void explainError() override {
58 tlog << "Expected floating point value: "
59 << str(fputil::FPBits<T>(expected)) << '\n';
60 tlog << "Actual floating point value: " << str(fputil::FPBits<T>(actual))
61 << '\n';
62 }
63 };
64
65 template <typename T, TestCond Condition> class CFPMatcher : public Matcher<T> {
66 static_assert(
67 cpp::is_complex_v<T>,
68 "CFPMatcher can only be used with complex floating point values.");
69 static_assert(Condition == TestCond::EQ || Condition == TestCond::NE,
70 "Unsupported CFPMatcher test condition.");
71
72 T expected;
73 T actual;
74
75 public:
CFPMatcher(T expectedValue)76 CFPMatcher(T expectedValue) : expected(expectedValue) {}
77
matchComplex()78 template <typename CFT> bool matchComplex() {
79 CFT *actualCmplxPtr = reinterpret_cast<CFT *>(&actual);
80 CFT *expectedCmplxPtr = reinterpret_cast<CFT *>(&expected);
81 CFT actualReal = actualCmplxPtr[0];
82 CFT actualImag = actualCmplxPtr[1];
83 CFT expectedReal = expectedCmplxPtr[0];
84 CFT expectedImag = expectedCmplxPtr[1];
85 fputil::FPBits<CFT> actualRealBits(actualReal),
86 expectedRealBits(expectedReal);
87 fputil::FPBits<CFT> actualImagBits(actualImag),
88 expectedImagBits(expectedImag);
89 if (Condition == TestCond::EQ)
90 return ((actualRealBits.is_nan() && expectedRealBits.is_nan()) ||
91 (actualRealBits.uintval() == expectedRealBits.uintval())) &&
92 ((actualImagBits.is_nan() && expectedImagBits.is_nan()) ||
93 (actualImagBits.uintval() == expectedImagBits.uintval()));
94
95 // If condition == TestCond::NE.
96 if (actualRealBits.is_nan() && expectedRealBits.is_nan())
97 return !expectedRealBits.is_nan() && !expectedImagBits.is_nan();
98 if (actualRealBits.is_nan())
99 return !expectedRealBits.is_nan();
100 if (actualImagBits.is_nan())
101 return !expectedImagBits.is_nan();
102 return (expectedRealBits.is_nan() ||
103 actualRealBits.uintval() != expectedRealBits.uintval()) &&
104 (expectedImagBits.is_nan() ||
105 actualImagBits.uintval() != expectedImagBits.uintval());
106 }
107
explainErrorComplex()108 template <typename CFT> void explainErrorComplex() {
109 CFT *actualCmplxPtr = reinterpret_cast<CFT *>(&actual);
110 CFT *expectedCmplxPtr = reinterpret_cast<CFT *>(&expected);
111 CFT actualReal = actualCmplxPtr[0];
112 CFT actualImag = actualCmplxPtr[1];
113 CFT expectedReal = expectedCmplxPtr[0];
114 CFT expectedImag = expectedCmplxPtr[1];
115 tlog << "Expected complex floating point value: "
116 << str(fputil::FPBits<CFT>(expectedReal)) + " + " +
117 str(fputil::FPBits<CFT>(expectedImag)) + "i"
118 << '\n';
119 tlog << "Actual complex floating point value: "
120 << str(fputil::FPBits<CFT>(actualReal)) + " + " +
121 str(fputil::FPBits<CFT>(actualImag)) + "i"
122 << '\n';
123 }
124
match(T actualValue)125 bool match(T actualValue) {
126 actual = actualValue;
127 if constexpr (cpp::is_complex_type_same<T, _Complex float>())
128 return matchComplex<float>();
129 else if constexpr (cpp::is_complex_type_same<T, _Complex double>())
130 return matchComplex<double>();
131 else if constexpr (cpp::is_complex_type_same<T, _Complex long double>())
132 return matchComplex<long double>();
133 #ifdef LIBC_TYPES_HAS_CFLOAT16
134 else if constexpr (cpp::is_complex_type_same<T, cfloat16>)
135 return matchComplex<float16>();
136 #endif
137 #ifdef LIBC_TYPES_HAS_CFLOAT128
138 else if constexpr (cpp::is_complex_type_same<T, cfloat128>)
139 return matchComplex<float128>();
140 #endif
141 }
142
explainError()143 void explainError() override {
144 if constexpr (cpp::is_complex_type_same<T, _Complex float>())
145 return explainErrorComplex<float>();
146 else if constexpr (cpp::is_complex_type_same<T, _Complex double>())
147 return explainErrorComplex<double>();
148 else if constexpr (cpp::is_complex_type_same<T, _Complex long double>())
149 return explainErrorComplex<long double>();
150 #ifdef LIBC_TYPES_HAS_CFLOAT16
151 else if constexpr (cpp::is_complex_type_same<T, cfloat16>)
152 return explainErrorComplex<float16>();
153 #endif
154 #ifdef LIBC_TYPES_HAS_CFLOAT128
155 else if constexpr (cpp::is_complex_type_same<T, cfloat128>)
156 return explainErrorComplex<float128>();
157 #endif
158 }
159 };
160
getMatcher(T expectedValue)161 template <TestCond C, typename T> FPMatcher<T, C> getMatcher(T expectedValue) {
162 return FPMatcher<T, C>(expectedValue);
163 }
164
165 template <TestCond C, typename T>
getMatcherComplex(T expectedValue)166 CFPMatcher<T, C> getMatcherComplex(T expectedValue) {
167 return CFPMatcher<T, C>(expectedValue);
168 }
169
170 template <typename T> struct FPTest : public Test {
171 using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
172 using StorageType = typename FPBits::StorageType;
173 static constexpr StorageType STORAGE_MAX =
174 LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max();
175 static constexpr T zero = FPBits::zero(Sign::POS).get_val();
176 static constexpr T neg_zero = FPBits::zero(Sign::NEG).get_val();
177 static constexpr T aNaN = FPBits::quiet_nan(Sign::POS).get_val();
178 static constexpr T neg_aNaN = FPBits::quiet_nan(Sign::NEG).get_val();
179 static constexpr T sNaN = FPBits::signaling_nan().get_val();
180 static constexpr T inf = FPBits::inf(Sign::POS).get_val();
181 static constexpr T neg_inf = FPBits::inf(Sign::NEG).get_val();
182 static constexpr T min_normal = FPBits::min_normal().get_val();
183 static constexpr T max_normal = FPBits::max_normal(Sign::POS).get_val();
184 static constexpr T neg_max_normal = FPBits::max_normal(Sign::NEG).get_val();
185 static constexpr T min_denormal = FPBits::min_subnormal().get_val();
186 static constexpr T max_denormal = FPBits::max_subnormal().get_val();
187
188 static constexpr int N_ROUNDING_MODES = 4;
189 static constexpr fputil::testing::RoundingMode ROUNDING_MODES[4] = {
190 fputil::testing::RoundingMode::Nearest,
191 fputil::testing::RoundingMode::Upward,
192 fputil::testing::RoundingMode::Downward,
193 fputil::testing::RoundingMode::TowardZero,
194 };
195 };
196
197 // Add facility to test Flush-Denormal-To-Zero (FTZ) and Denormal-As-Zero (DAZ)
198 // modes.
199 // These tests to ensure that our implementations will not crash under these
200 // modes.
201 #if defined(LIBC_TARGET_ARCH_IS_X86_64) && __has_builtin(__builtin_ia32_stmxcsr)
202
203 #define LIBC_TEST_FTZ_DAZ
204
205 static constexpr unsigned FTZ = 0x8000; // Flush denormal to zero
206 static constexpr unsigned DAZ = 0x0040; // Denormal as zero
207
208 struct ModifyMXCSR {
ModifyMXCSRModifyMXCSR209 ModifyMXCSR(unsigned flags) {
210 old_mxcsr = __builtin_ia32_stmxcsr();
211 __builtin_ia32_ldmxcsr(old_mxcsr | flags);
212 }
213
~ModifyMXCSRModifyMXCSR214 ~ModifyMXCSR() { __builtin_ia32_ldmxcsr(old_mxcsr); }
215
216 private:
217 unsigned old_mxcsr;
218 };
219
220 #endif
221
222 } // namespace testing
223 } // namespace LIBC_NAMESPACE_DECL
224
225 #define DECLARE_SPECIAL_CONSTANTS(T) \
226 using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>; \
227 using StorageType = typename FPBits::StorageType; \
228 \
229 static constexpr StorageType STORAGE_MAX = \
230 LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max(); \
231 const T zero = FPBits::zero(Sign::POS).get_val(); \
232 const T neg_zero = FPBits::zero(Sign::NEG).get_val(); \
233 const T aNaN = FPBits::quiet_nan(Sign::POS).get_val(); \
234 const T neg_aNaN = FPBits::quiet_nan(Sign::NEG).get_val(); \
235 const T sNaN = FPBits::signaling_nan(Sign::POS).get_val(); \
236 const T neg_sNaN = FPBits::signaling_nan(Sign::NEG).get_val(); \
237 const T inf = FPBits::inf(Sign::POS).get_val(); \
238 const T neg_inf = FPBits::inf(Sign::NEG).get_val(); \
239 const T min_normal = FPBits::min_normal().get_val(); \
240 const T max_normal = FPBits::max_normal(Sign::POS).get_val(); \
241 const T neg_max_normal = FPBits::max_normal(Sign::NEG).get_val(); \
242 const T min_denormal = FPBits::min_subnormal(Sign::POS).get_val(); \
243 const T neg_min_denormal = FPBits::min_subnormal(Sign::NEG).get_val(); \
244 const T max_denormal = FPBits::max_subnormal().get_val(); \
245 static constexpr int UNKNOWN_MATH_ROUNDING_DIRECTION = 99; \
246 static constexpr LIBC_NAMESPACE::cpp::array<int, 6> \
247 MATH_ROUNDING_DIRECTIONS_INCLUDING_UNKNOWN = { \
248 FP_INT_UPWARD, FP_INT_DOWNWARD, \
249 FP_INT_TOWARDZERO, FP_INT_TONEARESTFROMZERO, \
250 FP_INT_TONEAREST, UNKNOWN_MATH_ROUNDING_DIRECTION, \
251 };
252
253 #define EXPECT_FP_EQ(expected, actual) \
254 EXPECT_THAT(actual, LIBC_NAMESPACE::testing::getMatcher< \
255 LIBC_NAMESPACE::testing::TestCond::EQ>(expected))
256
257 #define EXPECT_CFP_EQ(expected, actual) \
258 EXPECT_THAT(actual, LIBC_NAMESPACE::testing::getMatcherComplex< \
259 LIBC_NAMESPACE::testing::TestCond::EQ>(expected))
260
261 #define TEST_FP_EQ(expected, actual) \
262 LIBC_NAMESPACE::testing::getMatcher<LIBC_NAMESPACE::testing::TestCond::EQ>( \
263 expected) \
264 .match(actual)
265
266 #define EXPECT_FP_IS_NAN(actual) EXPECT_TRUE((actual) != (actual))
267
268 #define ASSERT_FP_EQ(expected, actual) \
269 ASSERT_THAT(actual, LIBC_NAMESPACE::testing::getMatcher< \
270 LIBC_NAMESPACE::testing::TestCond::EQ>(expected))
271
272 #define EXPECT_FP_NE(expected, actual) \
273 EXPECT_THAT(actual, LIBC_NAMESPACE::testing::getMatcher< \
274 LIBC_NAMESPACE::testing::TestCond::NE>(expected))
275
276 #define ASSERT_FP_NE(expected, actual) \
277 ASSERT_THAT(actual, LIBC_NAMESPACE::testing::getMatcher< \
278 LIBC_NAMESPACE::testing::TestCond::NE>(expected))
279
280 #define EXPECT_MATH_ERRNO(expected) \
281 do { \
282 if (math_errhandling & MATH_ERRNO) { \
283 int actual = LIBC_NAMESPACE::libc_errno; \
284 LIBC_NAMESPACE::libc_errno = 0; \
285 EXPECT_EQ(actual, expected); \
286 } \
287 } while (0)
288
289 #define ASSERT_MATH_ERRNO(expected) \
290 do { \
291 if (math_errhandling & MATH_ERRNO) { \
292 int actual = LIBC_NAMESPACE::libc_errno; \
293 LIBC_NAMESPACE::libc_errno = 0; \
294 ASSERT_EQ(actual, expected); \
295 } \
296 } while (0)
297
298 #define EXPECT_FP_EXCEPTION(expected) \
299 do { \
300 if (math_errhandling & MATH_ERREXCEPT) { \
301 EXPECT_EQ( \
302 LIBC_NAMESPACE::fputil::test_except( \
303 static_cast<int>(FE_ALL_EXCEPT)) & \
304 ((expected) ? (expected) : static_cast<int>(FE_ALL_EXCEPT)), \
305 (expected)); \
306 } \
307 } while (0)
308
309 #define ASSERT_FP_EXCEPTION(expected) \
310 do { \
311 if (math_errhandling & MATH_ERREXCEPT) { \
312 ASSERT_EQ( \
313 LIBC_NAMESPACE::fputil::test_except( \
314 static_cast<int>(FE_ALL_EXCEPT)) & \
315 ((expected) ? (expected) : static_cast<int>(FE_ALL_EXCEPT)), \
316 (expected)); \
317 } \
318 } while (0)
319
320 #define EXPECT_FP_EQ_WITH_EXCEPTION(expected_val, actual_val, expected_except) \
321 do { \
322 LIBC_NAMESPACE::fputil::clear_except(static_cast<int>(FE_ALL_EXCEPT)); \
323 EXPECT_FP_EQ(expected_val, actual_val); \
324 EXPECT_FP_EXCEPTION(expected_except); \
325 } while (0)
326
327 #define EXPECT_FP_IS_NAN_WITH_EXCEPTION(actual_val, expected_except) \
328 do { \
329 LIBC_NAMESPACE::fputil::clear_except(static_cast<int>(FE_ALL_EXCEPT)); \
330 EXPECT_FP_IS_NAN(actual_val); \
331 EXPECT_FP_EXCEPTION(expected_except); \
332 } while (0)
333
334 #define EXPECT_FP_EQ_ALL_ROUNDING(expected, actual) \
335 do { \
336 using namespace LIBC_NAMESPACE::fputil::testing; \
337 ForceRoundingMode __r1(RoundingMode::Nearest); \
338 if (__r1.success) { \
339 EXPECT_FP_EQ((expected), (actual)); \
340 } \
341 ForceRoundingMode __r2(RoundingMode::Upward); \
342 if (__r2.success) { \
343 EXPECT_FP_EQ((expected), (actual)); \
344 } \
345 ForceRoundingMode __r3(RoundingMode::Downward); \
346 if (__r3.success) { \
347 EXPECT_FP_EQ((expected), (actual)); \
348 } \
349 ForceRoundingMode __r4(RoundingMode::TowardZero); \
350 if (__r4.success) { \
351 EXPECT_FP_EQ((expected), (actual)); \
352 } \
353 } while (0)
354
355 #define EXPECT_FP_EQ_ROUNDING_MODE(expected, actual, rounding_mode) \
356 do { \
357 using namespace LIBC_NAMESPACE::fputil::testing; \
358 ForceRoundingMode __r((rounding_mode)); \
359 if (__r.success) { \
360 EXPECT_FP_EQ((expected), (actual)); \
361 } \
362 } while (0)
363
364 #define EXPECT_FP_EQ_ROUNDING_NEAREST(expected, actual) \
365 EXPECT_FP_EQ_ROUNDING_MODE((expected), (actual), RoundingMode::Nearest)
366
367 #define EXPECT_FP_EQ_ROUNDING_UPWARD(expected, actual) \
368 EXPECT_FP_EQ_ROUNDING_MODE((expected), (actual), RoundingMode::Upward)
369
370 #define EXPECT_FP_EQ_ROUNDING_DOWNWARD(expected, actual) \
371 EXPECT_FP_EQ_ROUNDING_MODE((expected), (actual), RoundingMode::Downward)
372
373 #define EXPECT_FP_EQ_ROUNDING_TOWARD_ZERO(expected, actual) \
374 EXPECT_FP_EQ_ROUNDING_MODE((expected), (actual), RoundingMode::TowardZero)
375
376 #define EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_MODE( \
377 expected, actual, expected_except, rounding_mode) \
378 do { \
379 using namespace LIBC_NAMESPACE::fputil::testing; \
380 ForceRoundingMode __r((rounding_mode)); \
381 if (__r.success) { \
382 LIBC_NAMESPACE::fputil::clear_except(static_cast<int>(FE_ALL_EXCEPT)); \
383 EXPECT_FP_EQ((expected), (actual)); \
384 EXPECT_FP_EXCEPTION(expected_except); \
385 } \
386 } while (0)
387
388 #define EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(expected, actual, \
389 expected_except) \
390 EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_MODE( \
391 (expected), (actual), (expected_except), RoundingMode::Nearest)
392
393 #define EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(expected, actual, \
394 expected_except) \
395 EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_MODE( \
396 (expected), (actual), (expected_except), RoundingMode::Upward)
397
398 #define EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(expected, actual, \
399 expected_except) \
400 EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_MODE( \
401 (expected), (actual), (expected_except), RoundingMode::Downward)
402
403 #define EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(expected, actual, \
404 expected_except) \
405 EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_MODE( \
406 (expected), (actual), (expected_except), RoundingMode::TowardZero)
407
408 #define EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(expected, actual, \
409 expected_except) \
410 do { \
411 EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST((expected), (actual), \
412 (expected_except)); \
413 EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD((expected), (actual), \
414 (expected_except)); \
415 EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD((expected), (actual), \
416 (expected_except)); \
417 EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO((expected), (actual), \
418 (expected_except)); \
419 } while (0)
420
421 #endif // LLVM_LIBC_TEST_UNITTEST_FPMATCHER_H
422