1 | //===-- Utility class to test different flavors of nexttoward ---*- 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_NEXTTOWARDTEST_H |
10 | #define LLVM_LIBC_TEST_SRC_MATH_NEXTTOWARDTEST_H |
11 | |
12 | #include "hdr/fenv_macros.h" |
13 | #include "hdr/math_macros.h" |
14 | #include "src/__support/CPP/bit.h" |
15 | #include "src/__support/CPP/type_traits.h" |
16 | #include "src/__support/FPUtil/BasicOperations.h" |
17 | #include "src/__support/FPUtil/FPBits.h" |
18 | #include "test/UnitTest/FEnvSafeTest.h" |
19 | #include "test/UnitTest/FPMatcher.h" |
20 | #include "test/UnitTest/Test.h" |
21 | |
22 | #define ASSERT_FP_EQ_WITH_EXCEPTION(result, expected, expected_exception) \ |
23 | ASSERT_FP_EQ(result, expected); \ |
24 | ASSERT_FP_EXCEPTION(expected_exception); \ |
25 | LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT) |
26 | |
27 | #define ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected) \ |
28 | ASSERT_FP_EQ_WITH_EXCEPTION(result, expected, FE_INEXACT | FE_UNDERFLOW) |
29 | |
30 | #define ASSERT_FP_EQ_WITH_OVERFLOW(result, expected) \ |
31 | ASSERT_FP_EQ_WITH_EXCEPTION(result, expected, FE_INEXACT | FE_OVERFLOW) |
32 | |
33 | template <typename T> |
34 | class NextTowardTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
35 | using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>; |
36 | using ToFPBits = LIBC_NAMESPACE::fputil::FPBits<long double>; |
37 | using StorageType = typename FPBits::StorageType; |
38 | |
39 | const T inf = FPBits::inf(Sign::POS).get_val(); |
40 | const T neg_inf = FPBits::inf(Sign::NEG).get_val(); |
41 | const T zero = FPBits::zero(Sign::POS).get_val(); |
42 | const T neg_zero = FPBits::zero(Sign::NEG).get_val(); |
43 | const T nan = FPBits::quiet_nan().get_val(); |
44 | |
45 | const long double to_zero = ToFPBits::zero().get_val(); |
46 | const long double to_neg_zero = ToFPBits::zero(sign: Sign::NEG).get_val(); |
47 | const long double to_nan = ToFPBits::quiet_nan().get_val(); |
48 | |
49 | static constexpr StorageType min_subnormal = |
50 | FPBits::min_subnormal().uintval(); |
51 | static constexpr StorageType max_subnormal = |
52 | FPBits::max_subnormal().uintval(); |
53 | static constexpr StorageType min_normal = FPBits::min_normal().uintval(); |
54 | static constexpr StorageType max_normal = FPBits::max_normal().uintval(); |
55 | |
56 | public: |
57 | typedef T (*NextTowardFunc)(T, long double); |
58 | |
59 | void testNaN(NextTowardFunc func) { |
60 | ASSERT_FP_EQ(func(nan, 0), nan); |
61 | ASSERT_FP_EQ(func(0, to_nan), nan); |
62 | } |
63 | |
64 | void testBoundaries(NextTowardFunc func) { |
65 | ASSERT_FP_EQ(func(zero, to_neg_zero), neg_zero); |
66 | ASSERT_FP_EQ(func(neg_zero, to_zero), zero); |
67 | |
68 | // 'from' is zero|neg_zero. |
69 | T x = zero; |
70 | T result = func(x, 1); |
71 | StorageType expected_bits = 1; |
72 | T expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits); |
73 | ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected); |
74 | |
75 | result = func(x, -1); |
76 | expected_bits = FPBits::SIGN_MASK + 1; |
77 | expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits); |
78 | ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected); |
79 | |
80 | x = neg_zero; |
81 | result = func(x, 1); |
82 | expected_bits = 1; |
83 | expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits); |
84 | ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected); |
85 | |
86 | result = func(x, -1); |
87 | expected_bits = FPBits::SIGN_MASK + 1; |
88 | expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits); |
89 | ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected); |
90 | |
91 | // 'from' is max subnormal value. |
92 | x = LIBC_NAMESPACE::cpp::bit_cast<T>(max_subnormal); |
93 | result = func(x, 1); |
94 | expected = LIBC_NAMESPACE::cpp::bit_cast<T>(min_normal); |
95 | ASSERT_FP_EQ(result, expected); |
96 | |
97 | result = func(x, 0); |
98 | expected_bits = max_subnormal - 1; |
99 | expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits); |
100 | ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected); |
101 | |
102 | x = -x; |
103 | |
104 | result = func(x, -1); |
105 | expected_bits = FPBits::SIGN_MASK + min_normal; |
106 | expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits); |
107 | ASSERT_FP_EQ(result, expected); |
108 | |
109 | result = func(x, 0); |
110 | expected_bits = FPBits::SIGN_MASK + max_subnormal - 1; |
111 | expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits); |
112 | ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected); |
113 | |
114 | // 'from' is min subnormal value. |
115 | x = LIBC_NAMESPACE::cpp::bit_cast<T>(min_subnormal); |
116 | result = func(x, 1); |
117 | expected_bits = min_subnormal + 1; |
118 | expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits); |
119 | ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected); |
120 | ASSERT_FP_EQ_WITH_UNDERFLOW(func(x, 0), 0); |
121 | |
122 | x = -x; |
123 | result = func(x, -1); |
124 | expected_bits = FPBits::SIGN_MASK + min_subnormal + 1; |
125 | expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits); |
126 | ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected); |
127 | ASSERT_FP_EQ_WITH_UNDERFLOW(func(x, 0), T(-0.0)); |
128 | |
129 | // 'from' is min normal. |
130 | x = LIBC_NAMESPACE::cpp::bit_cast<T>(min_normal); |
131 | result = func(x, 0); |
132 | expected_bits = max_subnormal; |
133 | expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits); |
134 | ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected); |
135 | |
136 | result = func(x, inf); |
137 | expected_bits = min_normal + 1; |
138 | expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits); |
139 | ASSERT_FP_EQ(result, expected); |
140 | |
141 | x = -x; |
142 | result = func(x, 0); |
143 | expected_bits = FPBits::SIGN_MASK + max_subnormal; |
144 | expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits); |
145 | ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected); |
146 | |
147 | result = func(x, -inf); |
148 | expected_bits = FPBits::SIGN_MASK + min_normal + 1; |
149 | expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits); |
150 | ASSERT_FP_EQ(result, expected); |
151 | |
152 | // 'from' is max normal |
153 | x = LIBC_NAMESPACE::cpp::bit_cast<T>(max_normal); |
154 | result = func(x, 0); |
155 | expected_bits = max_normal - 1; |
156 | expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits); |
157 | ASSERT_FP_EQ(result, expected); |
158 | ASSERT_FP_EQ_WITH_OVERFLOW(func(x, inf), inf); |
159 | |
160 | x = -x; |
161 | result = func(x, 0); |
162 | expected_bits = FPBits::SIGN_MASK + max_normal - 1; |
163 | expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits); |
164 | ASSERT_FP_EQ(result, expected); |
165 | ASSERT_FP_EQ_WITH_OVERFLOW(func(x, -inf), -inf); |
166 | |
167 | // 'from' is infinity. |
168 | x = inf; |
169 | result = func(x, 0); |
170 | expected_bits = max_normal; |
171 | expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits); |
172 | ASSERT_FP_EQ(result, expected); |
173 | ASSERT_FP_EQ(func(x, inf), inf); |
174 | |
175 | x = neg_inf; |
176 | result = func(x, 0); |
177 | expected_bits = FPBits::SIGN_MASK + max_normal; |
178 | expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits); |
179 | ASSERT_FP_EQ(result, expected); |
180 | ASSERT_FP_EQ(func(x, neg_inf), neg_inf); |
181 | |
182 | // 'from' is a power of 2. |
183 | x = T(32.0); |
184 | result = func(x, 0); |
185 | FPBits x_bits = FPBits(x); |
186 | FPBits result_bits = FPBits(result); |
187 | ASSERT_EQ(result_bits.get_biased_exponent(), |
188 | uint16_t(x_bits.get_biased_exponent() - 1)); |
189 | ASSERT_EQ(result_bits.get_mantissa(), FPBits::FRACTION_MASK); |
190 | |
191 | result = func(x, 33.0); |
192 | result_bits = FPBits(result); |
193 | ASSERT_EQ(result_bits.get_biased_exponent(), x_bits.get_biased_exponent()); |
194 | ASSERT_EQ(result_bits.get_mantissa(), |
195 | x_bits.get_mantissa() + StorageType(1)); |
196 | |
197 | x = -x; |
198 | |
199 | result = func(x, 0); |
200 | result_bits = FPBits(result); |
201 | ASSERT_EQ(result_bits.get_biased_exponent(), |
202 | uint16_t(x_bits.get_biased_exponent() - 1)); |
203 | ASSERT_EQ(result_bits.get_mantissa(), FPBits::FRACTION_MASK); |
204 | |
205 | result = func(x, -33.0); |
206 | result_bits = FPBits(result); |
207 | ASSERT_EQ(result_bits.get_biased_exponent(), x_bits.get_biased_exponent()); |
208 | ASSERT_EQ(result_bits.get_mantissa(), |
209 | x_bits.get_mantissa() + StorageType(1)); |
210 | } |
211 | }; |
212 | |
213 | #define LIST_NEXTTOWARD_TESTS(T, func) \ |
214 | using LlvmLibcNextTowardTest = NextTowardTestTemplate<T>; \ |
215 | TEST_F(LlvmLibcNextTowardTest, TestNaN) { testNaN(&func); } \ |
216 | TEST_F(LlvmLibcNextTowardTest, TestBoundaries) { testBoundaries(&func); } |
217 | |
218 | #endif // LLVM_LIBC_TEST_SRC_MATH_NEXTTOWARDTEST_H |
219 | |