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