[flang] Accept polymorphic component element in storage_size
[llvm-project.git] / libc / test / src / math / RoundToIntegerTest.h
blob823ec8deaf27e219c58e1ad23cfac6f3e70335cb
1 //===-- Utility class to test different flavors of [l|ll]round --*- 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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_TEST_SRC_MATH_ROUNDTOINTEGERTEST_H
10 #define LLVM_LIBC_TEST_SRC_MATH_ROUNDTOINTEGERTEST_H
12 #include "src/__support/FPUtil/FEnvImpl.h"
13 #include "src/__support/FPUtil/FPBits.h"
14 #include "test/UnitTest/FPMatcher.h"
15 #include "test/UnitTest/Test.h"
16 #include "utils/MPFRWrapper/MPFRUtils.h"
18 #include <errno.h>
19 #include <math.h>
21 namespace mpfr = __llvm_libc::testing::mpfr;
23 static constexpr int ROUNDING_MODES[4] = {FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO,
24 FE_TONEAREST};
26 template <typename F, typename I, bool TestModes = false>
27 class RoundToIntegerTestTemplate : public __llvm_libc::testing::Test {
28 public:
29 typedef I (*RoundToIntegerFunc)(F);
31 private:
32 using FPBits = __llvm_libc::fputil::FPBits<F>;
33 using UIntType = typename FPBits::UIntType;
35 const F zero = F(__llvm_libc::fputil::FPBits<F>::zero());
36 const F neg_zero = F(__llvm_libc::fputil::FPBits<F>::neg_zero());
37 const F inf = F(__llvm_libc::fputil::FPBits<F>::inf());
38 const F neg_inf = F(__llvm_libc::fputil::FPBits<F>::neg_inf());
39 const F nan = F(__llvm_libc::fputil::FPBits<F>::build_quiet_nan(1));
40 static constexpr I INTEGER_MIN = I(1) << (sizeof(I) * 8 - 1);
41 static constexpr I INTEGER_MAX = -(INTEGER_MIN + 1);
43 void test_one_input(RoundToIntegerFunc func, F input, I expected,
44 bool expectError) {
45 errno = 0;
46 __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT);
48 ASSERT_EQ(func(input), expected);
50 if (expectError) {
51 ASSERT_FP_EXCEPTION(FE_INVALID);
52 ASSERT_MATH_ERRNO(EDOM);
53 } else {
54 ASSERT_FP_EXCEPTION(0);
55 ASSERT_MATH_ERRNO(0);
59 static inline mpfr::RoundingMode to_mpfr_rounding_mode(int mode) {
60 switch (mode) {
61 case FE_UPWARD:
62 return mpfr::RoundingMode::Upward;
63 case FE_DOWNWARD:
64 return mpfr::RoundingMode::Downward;
65 case FE_TOWARDZERO:
66 return mpfr::RoundingMode::TowardZero;
67 case FE_TONEAREST:
68 return mpfr::RoundingMode::Nearest;
69 default:
70 __builtin_unreachable();
74 public:
75 void SetUp() override {
76 if (math_errhandling & MATH_ERREXCEPT) {
77 // We will disable all exceptions so that the test will not
78 // crash with SIGFPE. We can still use fetestexcept to check
79 // if the appropriate flag was raised.
80 __llvm_libc::fputil::disable_except(FE_ALL_EXCEPT);
84 void do_infinity_and_na_n_test(RoundToIntegerFunc func) {
85 test_one_input(func, inf, INTEGER_MAX, true);
86 test_one_input(func, neg_inf, INTEGER_MIN, true);
87 // This is currently never enabled, the
88 // LLVM_LIBC_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR CMake option in
89 // libc/CMakeLists.txt is not forwarded to C++.
90 #if LIBC_COPT_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR
91 // Result is not well-defined, we always returns INTEGER_MAX
92 test_one_input(func, nan, INTEGER_MAX, true);
93 #endif // LIBC_COPT_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR
96 void testInfinityAndNaN(RoundToIntegerFunc func) {
97 if (TestModes) {
98 for (int mode : ROUNDING_MODES) {
99 __llvm_libc::fputil::set_round(mode);
100 do_infinity_and_na_n_test(func);
102 } else {
103 do_infinity_and_na_n_test(func);
107 void do_round_numbers_test(RoundToIntegerFunc func) {
108 test_one_input(func, zero, I(0), false);
109 test_one_input(func, neg_zero, I(0), false);
110 test_one_input(func, F(1.0), I(1), false);
111 test_one_input(func, F(-1.0), I(-1), false);
112 test_one_input(func, F(10.0), I(10), false);
113 test_one_input(func, F(-10.0), I(-10), false);
114 test_one_input(func, F(1234.0), I(1234), false);
115 test_one_input(func, F(-1234.0), I(-1234), false);
117 // The rest of this this function compares with an equivalent MPFR function
118 // which rounds floating point numbers to long values. There is no MPFR
119 // function to round to long long or wider integer values. So, we will
120 // the remaining tests only if the width of I less than equal to that of
121 // long.
122 if (sizeof(I) > sizeof(long))
123 return;
125 constexpr int EXPONENT_LIMIT = sizeof(I) * 8 - 1;
126 // We start with 1.0 so that the implicit bit for x86 long doubles
127 // is set.
128 FPBits bits(F(1.0));
129 bits.set_unbiased_exponent(EXPONENT_LIMIT + FPBits::EXPONENT_BIAS);
130 bits.set_sign(1);
131 bits.set_mantissa(0);
133 F x = F(bits);
134 long mpfr_result;
135 bool erangeflag = mpfr::round_to_long(x, mpfr_result);
136 ASSERT_FALSE(erangeflag);
137 test_one_input(func, x, mpfr_result, false);
140 void testRoundNumbers(RoundToIntegerFunc func) {
141 if (TestModes) {
142 for (int mode : ROUNDING_MODES) {
143 __llvm_libc::fputil::set_round(mode);
144 do_round_numbers_test(func);
146 } else {
147 do_round_numbers_test(func);
151 void do_fractions_test(RoundToIntegerFunc func, int mode) {
152 constexpr F FRACTIONS[] = {0.5, -0.5, 0.115, -0.115, 0.715, -0.715};
153 for (F x : FRACTIONS) {
154 long mpfr_long_result;
155 bool erangeflag;
156 if (TestModes)
157 erangeflag = mpfr::round_to_long(x, to_mpfr_rounding_mode(mode),
158 mpfr_long_result);
159 else
160 erangeflag = mpfr::round_to_long(x, mpfr_long_result);
161 ASSERT_FALSE(erangeflag);
162 I mpfr_result = mpfr_long_result;
163 test_one_input(func, x, mpfr_result, false);
167 void testFractions(RoundToIntegerFunc func) {
168 if (TestModes) {
169 for (int mode : ROUNDING_MODES) {
170 __llvm_libc::fputil::set_round(mode);
171 do_fractions_test(func, mode);
173 } else {
174 // Passing 0 for mode has no effect as it is not used in doFractionsTest
175 // when `TestModes` is false;
176 do_fractions_test(func, 0);
180 void testIntegerOverflow(RoundToIntegerFunc func) {
181 // This function compares with an equivalent MPFR function which rounds
182 // floating point numbers to long values. There is no MPFR function to
183 // round to long long or wider integer values. So, we will peform the
184 // comparisons in this function only if the width of I less than equal to
185 // that of long.
186 if (sizeof(I) > sizeof(long))
187 return;
189 constexpr int EXPONENT_LIMIT = sizeof(I) * 8 - 1;
190 // We start with 1.0 so that the implicit bit for x86 long doubles
191 // is set.
192 FPBits bits(F(1.0));
193 bits.set_unbiased_exponent(EXPONENT_LIMIT + FPBits::EXPONENT_BIAS);
194 bits.set_sign(1);
195 bits.set_mantissa(UIntType(0x1)
196 << (__llvm_libc::fputil::MantissaWidth<F>::VALUE - 1));
198 F x = F(bits);
199 if (TestModes) {
200 for (int m : ROUNDING_MODES) {
201 __llvm_libc::fputil::set_round(m);
202 long mpfr_long_result;
203 bool erangeflag =
204 mpfr::round_to_long(x, to_mpfr_rounding_mode(m), mpfr_long_result);
205 ASSERT_TRUE(erangeflag);
206 test_one_input(func, x, INTEGER_MIN, true);
208 } else {
209 long mpfr_long_result;
210 bool erangeflag = mpfr::round_to_long(x, mpfr_long_result);
211 ASSERT_TRUE(erangeflag);
212 test_one_input(func, x, INTEGER_MIN, true);
216 void testSubnormalRange(RoundToIntegerFunc func) {
217 constexpr UIntType COUNT = 1000001;
218 constexpr UIntType STEP =
219 (FPBits::MAX_SUBNORMAL - FPBits::MIN_SUBNORMAL) / COUNT;
220 for (UIntType i = FPBits::MIN_SUBNORMAL; i <= FPBits::MAX_SUBNORMAL;
221 i += STEP) {
222 F x = F(FPBits(i));
223 if (x == F(0.0))
224 continue;
225 // All subnormal numbers should round to zero.
226 if (TestModes) {
227 if (x > 0) {
228 __llvm_libc::fputil::set_round(FE_UPWARD);
229 test_one_input(func, x, I(1), false);
230 __llvm_libc::fputil::set_round(FE_DOWNWARD);
231 test_one_input(func, x, I(0), false);
232 __llvm_libc::fputil::set_round(FE_TOWARDZERO);
233 test_one_input(func, x, I(0), false);
234 __llvm_libc::fputil::set_round(FE_TONEAREST);
235 test_one_input(func, x, I(0), false);
236 } else {
237 __llvm_libc::fputil::set_round(FE_UPWARD);
238 test_one_input(func, x, I(0), false);
239 __llvm_libc::fputil::set_round(FE_DOWNWARD);
240 test_one_input(func, x, I(-1), false);
241 __llvm_libc::fputil::set_round(FE_TOWARDZERO);
242 test_one_input(func, x, I(0), false);
243 __llvm_libc::fputil::set_round(FE_TONEAREST);
244 test_one_input(func, x, I(0), false);
246 } else {
247 test_one_input(func, x, 0L, false);
252 void testNormalRange(RoundToIntegerFunc func) {
253 // This function compares with an equivalent MPFR function which rounds
254 // floating point numbers to long values. There is no MPFR function to
255 // round to long long or wider integer values. So, we will peform the
256 // comparisons in this function only if the width of I less than equal to
257 // that of long.
258 if (sizeof(I) > sizeof(long))
259 return;
261 constexpr UIntType COUNT = 1000001;
262 constexpr UIntType STEP = (FPBits::MAX_NORMAL - FPBits::MIN_NORMAL) / COUNT;
263 for (UIntType i = FPBits::MIN_NORMAL; i <= FPBits::MAX_NORMAL; i += STEP) {
264 F x = F(FPBits(i));
265 // In normal range on x86 platforms, the long double implicit 1 bit can be
266 // zero making the numbers NaN. We will skip them.
267 if (isnan(x)) {
268 continue;
271 if (TestModes) {
272 for (int m : ROUNDING_MODES) {
273 long mpfr_long_result;
274 bool erangeflag = mpfr::round_to_long(x, to_mpfr_rounding_mode(m),
275 mpfr_long_result);
276 I mpfr_result = mpfr_long_result;
277 __llvm_libc::fputil::set_round(m);
278 if (erangeflag)
279 test_one_input(func, x, x > 0 ? INTEGER_MAX : INTEGER_MIN, true);
280 else
281 test_one_input(func, x, mpfr_result, false);
283 } else {
284 long mpfr_long_result;
285 bool erangeflag = mpfr::round_to_long(x, mpfr_long_result);
286 I mpfr_result = mpfr_long_result;
287 if (erangeflag)
288 test_one_input(func, x, x > 0 ? INTEGER_MAX : INTEGER_MIN, true);
289 else
290 test_one_input(func, x, mpfr_result, false);
296 #define LIST_ROUND_TO_INTEGER_TESTS_HELPER(F, I, func, TestModes) \
297 using LlvmLibcRoundToIntegerTest = \
298 RoundToIntegerTestTemplate<F, I, TestModes>; \
299 TEST_F(LlvmLibcRoundToIntegerTest, InfinityAndNaN) { \
300 testInfinityAndNaN(&func); \
302 TEST_F(LlvmLibcRoundToIntegerTest, RoundNumbers) { \
303 testRoundNumbers(&func); \
305 TEST_F(LlvmLibcRoundToIntegerTest, Fractions) { testFractions(&func); } \
306 TEST_F(LlvmLibcRoundToIntegerTest, IntegerOverflow) { \
307 testIntegerOverflow(&func); \
309 TEST_F(LlvmLibcRoundToIntegerTest, SubnormalRange) { \
310 testSubnormalRange(&func); \
312 TEST_F(LlvmLibcRoundToIntegerTest, NormalRange) { testNormalRange(&func); }
314 #define LIST_ROUND_TO_INTEGER_TESTS(F, I, func) \
315 LIST_ROUND_TO_INTEGER_TESTS_HELPER(F, I, func, false)
317 #define LIST_ROUND_TO_INTEGER_TESTS_WITH_MODES(F, I, func) \
318 LIST_ROUND_TO_INTEGER_TESTS_HELPER(F, I, func, true)
320 #endif // LLVM_LIBC_TEST_SRC_MATH_ROUNDTOINTEGERTEST_H