|
| 1 | +//===-- Utility class to test fmul[f|l] -------------------------*- 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_FMULTEST_H |
| 10 | +#define LLVM_LIBC_TEST_SRC_MATH_FMULTEST_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 | +#include "utils/MPFRWrapper/MPFRUtils.h" |
| 17 | + |
| 18 | +namespace mpfr = LIBC_NAMESPACE::testing::mpfr; |
| 19 | + |
| 20 | +template <typename OutType, typename InType> |
| 21 | +class FmulMPFRTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
| 22 | + |
| 23 | + DECLARE_SPECIAL_CONSTANTS(InType) |
| 24 | + |
| 25 | +public: |
| 26 | + typedef OutType (*FMulFunc)(InType, InType); |
| 27 | + |
| 28 | + void testFMulMPFR(FMulFunc func) { |
| 29 | + constexpr int N = 10; |
| 30 | + mpfr::BinaryInput<InType> INPUTS[N] = { |
| 31 | + {3.0, 5.0}, |
| 32 | + {0x1.0p1, 0x1.0p-131}, |
| 33 | + {0x1.0p2, 0x1.0p-129}, |
| 34 | + {1.0, 1.0}, |
| 35 | + {-0.0, -0.0}, |
| 36 | + {-0.0, 0.0}, |
| 37 | + {0.0, -0.0}, |
| 38 | + {0x1.0p100, 0x1.0p100}, |
| 39 | + {1.0, 1.0 + 0x1.0p-128 + 0x1.0p-149 + 0x1.0p-150}, |
| 40 | + {1.0, 0x1.0p-128 + 0x1.0p-149 + 0x1.0p-150}}; |
| 41 | + |
| 42 | + for (int i = 0; i < N; ++i) { |
| 43 | + InType x = INPUTS[i].x; |
| 44 | + InType y = INPUTS[i].y; |
| 45 | + ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Fmul, INPUTS[i], |
| 46 | + func(x, y), 0.5); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + void testSpecialInputsMPFR(FMulFunc func) { |
| 51 | + constexpr int N = 27; |
| 52 | + mpfr::BinaryInput<InType> INPUTS[N] = {{inf, 0x1.0p-129}, |
| 53 | + {0x1.0p-129, inf}, |
| 54 | + {inf, 2.0}, |
| 55 | + {3.0, inf}, |
| 56 | + {0.0, 0.0}, |
| 57 | + {neg_inf, aNaN}, |
| 58 | + {aNaN, neg_inf}, |
| 59 | + {neg_inf, neg_inf}, |
| 60 | + {0.0, neg_inf}, |
| 61 | + {neg_inf, 0.0}, |
| 62 | + {neg_inf, 1.0}, |
| 63 | + {1.0, neg_inf}, |
| 64 | + {neg_inf, 0x1.0p-129}, |
| 65 | + {0x1.0p-129, neg_inf}, |
| 66 | + {0.0, 0x1.0p-129}, |
| 67 | + {inf, 0.0}, |
| 68 | + {0.0, inf}, |
| 69 | + {0.0, aNaN}, |
| 70 | + {2.0, aNaN}, |
| 71 | + {0x1.0p-129, aNaN}, |
| 72 | + {inf, aNaN}, |
| 73 | + {aNaN, aNaN}, |
| 74 | + {0.0, sNaN}, |
| 75 | + {2.0, sNaN}, |
| 76 | + {0x1.0p-129, sNaN}, |
| 77 | + {inf, sNaN}, |
| 78 | + {sNaN, sNaN}}; |
| 79 | + |
| 80 | + for (int i = 0; i < N; ++i) { |
| 81 | + InType x = INPUTS[i].x; |
| 82 | + InType y = INPUTS[i].y; |
| 83 | + ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Fmul, INPUTS[i], |
| 84 | + func(x, y), 0.5); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + void testNormalRange(FMulFunc func) { |
| 89 | + using FPBits = LIBC_NAMESPACE::fputil::FPBits<InType>; |
| 90 | + using StorageType = typename FPBits::StorageType; |
| 91 | + static constexpr StorageType MAX_NORMAL = FPBits::max_normal().uintval(); |
| 92 | + static constexpr StorageType MIN_NORMAL = FPBits::min_normal().uintval(); |
| 93 | + |
| 94 | + constexpr StorageType COUNT = 10'001; |
| 95 | + constexpr StorageType STEP = (MAX_NORMAL - MIN_NORMAL) / COUNT; |
| 96 | + for (int signs = 0; signs < 4; ++signs) { |
| 97 | + for (StorageType v = MIN_NORMAL, w = MAX_NORMAL; |
| 98 | + v <= MAX_NORMAL && w >= MIN_NORMAL; v += STEP, w -= STEP) { |
| 99 | + InType x = FPBits(v).get_val(), y = FPBits(w).get_val(); |
| 100 | + if (signs % 2 == 1) { |
| 101 | + x = -x; |
| 102 | + } |
| 103 | + if (signs >= 2) { |
| 104 | + y = -y; |
| 105 | + } |
| 106 | + |
| 107 | + mpfr::BinaryInput<InType> input{x, y}; |
| 108 | + ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Fmul, input, func(x, y), |
| 109 | + 0.5); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | +#define LIST_FMUL_MPFR_TESTS(OutType, InType, func) \ |
| 116 | + using LlvmLibcFmulTest = FmulMPFRTest<OutType, InType>; \ |
| 117 | + TEST_F(LlvmLibcFmulTest, MulMpfr) { testFMulMPFR(&func); } \ |
| 118 | + TEST_F(LlvmLibcFmulTest, NanInfMpfr) { testSpecialInputsMPFR(&func); } \ |
| 119 | + TEST_F(LlvmLibcFmulTest, NormalRange) { testNormalRange(&func); } |
| 120 | + |
| 121 | +#endif // LLVM_LIBC_TEST_SRC_MATH_FMULTEST_H |
0 commit comments