1 | //===-- Utility class to test different flavors of remquo -------*- 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_REMQUOTEST_H |
10 | #define LLVM_LIBC_TEST_SRC_MATH_REMQUOTEST_H |
11 | |
12 | #include "hdr/math_macros.h" |
13 | #include "src/__support/FPUtil/BasicOperations.h" |
14 | #include "src/__support/FPUtil/FPBits.h" |
15 | #include "test/UnitTest/FEnvSafeTest.h" |
16 | #include "test/UnitTest/FPMatcher.h" |
17 | #include "test/UnitTest/Test.h" |
18 | #include "utils/MPFRWrapper/MPFRUtils.h" |
19 | |
20 | namespace mpfr = LIBC_NAMESPACE::testing::mpfr; |
21 | using LIBC_NAMESPACE::Sign; |
22 | |
23 | template <typename T> |
24 | class RemQuoTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
25 | using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>; |
26 | using StorageType = typename FPBits::StorageType; |
27 | |
28 | const T inf = FPBits::inf(Sign::POS).get_val(); |
29 | const T neg_inf = FPBits::inf(Sign::NEG).get_val(); |
30 | const T zero = FPBits::zero(Sign::POS).get_val(); |
31 | const T neg_zero = FPBits::zero(Sign::NEG).get_val(); |
32 | const T nan = FPBits::quiet_nan().get_val(); |
33 | |
34 | static constexpr StorageType MIN_SUBNORMAL = |
35 | FPBits::min_subnormal().uintval(); |
36 | static constexpr StorageType MAX_SUBNORMAL = |
37 | FPBits::max_subnormal().uintval(); |
38 | static constexpr StorageType MIN_NORMAL = FPBits::min_normal().uintval(); |
39 | static constexpr StorageType MAX_NORMAL = FPBits::max_normal().uintval(); |
40 | |
41 | public: |
42 | typedef T (*RemQuoFunc)(T, T, int *); |
43 | |
44 | void testSpecialNumbers(RemQuoFunc func) { |
45 | int quotient; |
46 | T x, y; |
47 | |
48 | y = T(1.0); |
49 | x = inf; |
50 | EXPECT_FP_EQ(nan, func(x, y, "ient)); |
51 | x = neg_inf; |
52 | EXPECT_FP_EQ(nan, func(x, y, "ient)); |
53 | |
54 | x = T(1.0); |
55 | y = zero; |
56 | EXPECT_FP_EQ(nan, func(x, y, "ient)); |
57 | y = neg_zero; |
58 | EXPECT_FP_EQ(nan, func(x, y, "ient)); |
59 | |
60 | y = nan; |
61 | x = T(1.0); |
62 | EXPECT_FP_EQ(nan, func(x, y, "ient)); |
63 | |
64 | y = T(1.0); |
65 | x = nan; |
66 | EXPECT_FP_EQ(nan, func(x, y, "ient)); |
67 | |
68 | x = nan; |
69 | y = nan; |
70 | EXPECT_FP_EQ(nan, func(x, y, "ient)); |
71 | |
72 | x = zero; |
73 | y = T(1.0); |
74 | EXPECT_FP_EQ(func(x, y, "ient), zero); |
75 | |
76 | x = neg_zero; |
77 | y = T(1.0); |
78 | EXPECT_FP_EQ(func(x, y, "ient), neg_zero); |
79 | |
80 | x = T(1.125); |
81 | y = inf; |
82 | EXPECT_FP_EQ(func(x, y, "ient), x); |
83 | EXPECT_EQ(quotient, 0); |
84 | } |
85 | |
86 | void testEqualNumeratorAndDenominator(RemQuoFunc func) { |
87 | T x = T(1.125), y = T(1.125); |
88 | int q; |
89 | |
90 | // When the remainder is zero, the standard requires it to |
91 | // have the same sign as x. |
92 | |
93 | EXPECT_FP_EQ(func(x, y, &q), zero); |
94 | EXPECT_EQ(q, 1); |
95 | |
96 | EXPECT_FP_EQ(func(x, -y, &q), zero); |
97 | EXPECT_EQ(q, -1); |
98 | |
99 | EXPECT_FP_EQ(func(-x, y, &q), neg_zero); |
100 | EXPECT_EQ(q, -1); |
101 | |
102 | EXPECT_FP_EQ(func(-x, -y, &q), neg_zero); |
103 | EXPECT_EQ(q, 1); |
104 | } |
105 | |
106 | void testSubnormalRange(RemQuoFunc func) { |
107 | constexpr StorageType COUNT = 100'001; |
108 | constexpr StorageType STEP = (MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT; |
109 | for (StorageType v = MIN_SUBNORMAL, w = MAX_SUBNORMAL; |
110 | v <= MAX_SUBNORMAL && w >= MIN_SUBNORMAL; v += STEP, w -= STEP) { |
111 | T x = FPBits(v).get_val(), y = FPBits(w).get_val(); |
112 | mpfr::BinaryOutput<T> result; |
113 | mpfr::BinaryInput<T> input{x, y}; |
114 | result.f = func(x, y, &result.i); |
115 | ASSERT_MPFR_MATCH(mpfr::Operation::RemQuo, input, result, 0.0); |
116 | } |
117 | } |
118 | |
119 | void testNormalRange(RemQuoFunc func) { |
120 | constexpr StorageType COUNT = 1'001; |
121 | constexpr StorageType STEP = (MAX_NORMAL - MIN_NORMAL) / COUNT; |
122 | for (StorageType v = MIN_NORMAL, w = MAX_NORMAL; |
123 | v <= MAX_NORMAL && w >= MIN_NORMAL; v += STEP, w -= STEP) { |
124 | T x = FPBits(v).get_val(), y = FPBits(w).get_val(); |
125 | mpfr::BinaryOutput<T> result; |
126 | mpfr::BinaryInput<T> input{x, y}; |
127 | result.f = func(x, y, &result.i); |
128 | |
129 | // In normal range on x86 platforms, the long double implicit 1 bit can be |
130 | // zero making the numbers NaN. Hence we test for them separately. |
131 | if (FPBits(v).is_nan() || FPBits(w).is_nan()) { |
132 | ASSERT_FP_EQ(result.f, nan); |
133 | continue; |
134 | } |
135 | |
136 | ASSERT_MPFR_MATCH(mpfr::Operation::RemQuo, input, result, 0.0); |
137 | } |
138 | } |
139 | }; |
140 | |
141 | #define LIST_REMQUO_TESTS(T, func) \ |
142 | using LlvmLibcRemQuoTest = RemQuoTestTemplate<T>; \ |
143 | TEST_F(LlvmLibcRemQuoTest, SpecialNumbers) { testSpecialNumbers(&func); } \ |
144 | TEST_F(LlvmLibcRemQuoTest, EqualNumeratorAndDenominator) { \ |
145 | testEqualNumeratorAndDenominator(&func); \ |
146 | } \ |
147 | TEST_F(LlvmLibcRemQuoTest, SubnormalRange) { testSubnormalRange(&func); } \ |
148 | TEST_F(LlvmLibcRemQuoTest, NormalRange) { testNormalRange(&func); } |
149 | |
150 | #endif // LLVM_LIBC_TEST_SRC_MATH_REMQUOTEST_H |
151 | |