| 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 "src/__support/FPUtil/FPBits.h" |
| 13 | #include "test/UnitTest/FEnvSafeTest.h" |
| 14 | #include "test/UnitTest/FPMatcher.h" |
| 15 | #include "test/UnitTest/Test.h" |
| 16 | |
| 17 | using LIBC_NAMESPACE::Sign; |
| 18 | |
| 19 | template <typename T> |
| 20 | class RemQuoTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
| 21 | using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>; |
| 22 | using StorageType = typename FPBits::StorageType; |
| 23 | |
| 24 | const T inf = FPBits::inf(Sign::POS).get_val(); |
| 25 | const T neg_inf = FPBits::inf(Sign::NEG).get_val(); |
| 26 | const T zero = FPBits::zero(Sign::POS).get_val(); |
| 27 | const T neg_zero = FPBits::zero(Sign::NEG).get_val(); |
| 28 | const T nan = FPBits::quiet_nan().get_val(); |
| 29 | |
| 30 | public: |
| 31 | typedef T (*RemQuoFunc)(T, T, int *); |
| 32 | |
| 33 | void testSpecialNumbers(RemQuoFunc func) { |
| 34 | int quotient; |
| 35 | T x, y; |
| 36 | |
| 37 | y = T(1.0); |
| 38 | x = inf; |
| 39 | EXPECT_FP_EQ(nan, func(x, y, "ient)); |
| 40 | x = neg_inf; |
| 41 | EXPECT_FP_EQ(nan, func(x, y, "ient)); |
| 42 | |
| 43 | x = T(1.0); |
| 44 | y = zero; |
| 45 | EXPECT_FP_EQ(nan, func(x, y, "ient)); |
| 46 | y = neg_zero; |
| 47 | EXPECT_FP_EQ(nan, func(x, y, "ient)); |
| 48 | |
| 49 | y = nan; |
| 50 | x = T(1.0); |
| 51 | EXPECT_FP_EQ(nan, func(x, y, "ient)); |
| 52 | |
| 53 | y = T(1.0); |
| 54 | x = nan; |
| 55 | EXPECT_FP_EQ(nan, func(x, y, "ient)); |
| 56 | |
| 57 | x = nan; |
| 58 | y = nan; |
| 59 | EXPECT_FP_EQ(nan, func(x, y, "ient)); |
| 60 | |
| 61 | x = zero; |
| 62 | y = T(1.0); |
| 63 | EXPECT_FP_EQ(func(x, y, "ient), zero); |
| 64 | |
| 65 | x = neg_zero; |
| 66 | y = T(1.0); |
| 67 | EXPECT_FP_EQ(func(x, y, "ient), neg_zero); |
| 68 | |
| 69 | x = T(1.125); |
| 70 | y = inf; |
| 71 | EXPECT_FP_EQ(func(x, y, "ient), x); |
| 72 | EXPECT_EQ(quotient, 0); |
| 73 | } |
| 74 | |
| 75 | void testEqualNumeratorAndDenominator(RemQuoFunc func) { |
| 76 | T x = T(1.125), y = T(1.125); |
| 77 | int q; |
| 78 | |
| 79 | // When the remainder is zero, the standard requires it to |
| 80 | // have the same sign as x. |
| 81 | |
| 82 | EXPECT_FP_EQ(func(x, y, &q), zero); |
| 83 | EXPECT_EQ(q, 1); |
| 84 | |
| 85 | EXPECT_FP_EQ(func(x, -y, &q), zero); |
| 86 | EXPECT_EQ(q, -1); |
| 87 | |
| 88 | EXPECT_FP_EQ(func(-x, y, &q), neg_zero); |
| 89 | EXPECT_EQ(q, -1); |
| 90 | |
| 91 | EXPECT_FP_EQ(func(-x, -y, &q), neg_zero); |
| 92 | EXPECT_EQ(q, 1); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | #define LIST_REMQUO_TESTS(T, func) \ |
| 97 | using LlvmLibcRemQuoTest = RemQuoTestTemplate<T>; \ |
| 98 | TEST_F(LlvmLibcRemQuoTest, SpecialNumbers) { testSpecialNumbers(&func); } \ |
| 99 | TEST_F(LlvmLibcRemQuoTest, EqualNumeratorAndDenominator) { \ |
| 100 | testEqualNumeratorAndDenominator(&func); \ |
| 101 | } |
| 102 | |
| 103 | #endif // LLVM_LIBC_TEST_SRC_MATH_REMQUOTEST_H |
| 104 | |