| 1 | //===-- Utility class to test different flavors of rint ---------*- 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_SMOKE_RINTTEST_H |
| 10 | #define LLVM_LIBC_TEST_SRC_MATH_SMOKE_RINTTEST_H |
| 11 | |
| 12 | #include "src/__support/FPUtil/FEnvImpl.h" |
| 13 | #include "src/__support/FPUtil/FPBits.h" |
| 14 | #include "test/UnitTest/FEnvSafeTest.h" |
| 15 | #include "test/UnitTest/FPMatcher.h" |
| 16 | #include "test/UnitTest/Test.h" |
| 17 | |
| 18 | #include "hdr/fenv_macros.h" |
| 19 | #include "hdr/math_macros.h" |
| 20 | |
| 21 | using LIBC_NAMESPACE::Sign; |
| 22 | |
| 23 | static constexpr int ROUNDING_MODES[4] = {FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO, |
| 24 | FE_TONEAREST}; |
| 25 | |
| 26 | template <typename T> |
| 27 | class RIntTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
| 28 | public: |
| 29 | typedef T (*RIntFunc)(T); |
| 30 | |
| 31 | private: |
| 32 | using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>; |
| 33 | using StorageType = typename FPBits::StorageType; |
| 34 | |
| 35 | const T inf = FPBits::inf(Sign::POS).get_val(); |
| 36 | const T neg_inf = FPBits::inf(Sign::NEG).get_val(); |
| 37 | const T zero = FPBits::zero(Sign::POS).get_val(); |
| 38 | const T neg_zero = FPBits::zero(Sign::NEG).get_val(); |
| 39 | const T nan = FPBits::quiet_nan().get_val(); |
| 40 | |
| 41 | public: |
| 42 | void testSpecialNumbers(RIntFunc func) { |
| 43 | for (int mode : ROUNDING_MODES) { |
| 44 | LIBC_NAMESPACE::fputil::set_round(mode); |
| 45 | ASSERT_FP_EQ(inf, func(inf)); |
| 46 | ASSERT_FP_EQ(neg_inf, func(neg_inf)); |
| 47 | ASSERT_FP_EQ(nan, func(nan)); |
| 48 | ASSERT_FP_EQ(zero, func(zero)); |
| 49 | ASSERT_FP_EQ(neg_zero, func(neg_zero)); |
| 50 | } |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | #define LIST_RINT_TESTS(F, func) \ |
| 55 | using LlvmLibcRIntTest = RIntTestTemplate<F>; \ |
| 56 | TEST_F(LlvmLibcRIntTest, specialNumbers) { testSpecialNumbers(&func); } |
| 57 | |
| 58 | #endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_RINTTEST_H |
| 59 | |