1 //===-- Exhaustive test for expm1f ----------------------------------------===//
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 #include "exhaustive_test.h"
10 #include "src/__support/FPUtil/FPBits.h"
11 #include "src/math/expm1f.h"
12 #include "utils/MPFRWrapper/MPFRUtils.h"
13 #include "utils/UnitTest/FPMatcher.h"
14 
15 #include <thread>
16 
17 using FPBits = __llvm_libc::fputil::FPBits<float>;
18 
19 namespace mpfr = __llvm_libc::testing::mpfr;
20 
21 struct LlvmLibcExpm1fExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
checkLlvmLibcExpm1fExhaustiveTest22   bool check(uint32_t start, uint32_t stop,
23              mpfr::RoundingMode rounding) override {
24     mpfr::ForceRoundingMode r(rounding);
25     uint32_t bits = stop;
26     bool result = true;
27     do {
28       FPBits xbits(bits);
29       float x = float(xbits);
30       result &= EXPECT_MPFR_MATCH(mpfr::Operation::Expm1, x,
31                                   __llvm_libc::expm1f(x), 0.5, rounding);
32     } while (bits-- > start);
33     return result;
34   }
35 };
36 
37 // Range: [0, 89];
38 static constexpr uint32_t POS_START = 0x0000'0000U;
39 static constexpr uint32_t POS_STOP = 0x42b2'0000U;
40 
TEST_F(LlvmLibcExpm1fExhaustiveTest,PostiveRangeRoundNearestTieToEven)41 TEST_F(LlvmLibcExpm1fExhaustiveTest, PostiveRangeRoundNearestTieToEven) {
42   test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::Nearest);
43 }
44 
TEST_F(LlvmLibcExpm1fExhaustiveTest,PostiveRangeRoundUp)45 TEST_F(LlvmLibcExpm1fExhaustiveTest, PostiveRangeRoundUp) {
46   test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::Upward);
47 }
48 
TEST_F(LlvmLibcExpm1fExhaustiveTest,PostiveRangeRoundDown)49 TEST_F(LlvmLibcExpm1fExhaustiveTest, PostiveRangeRoundDown) {
50   test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::Downward);
51 }
52 
TEST_F(LlvmLibcExpm1fExhaustiveTest,PostiveRangeRoundTowardZero)53 TEST_F(LlvmLibcExpm1fExhaustiveTest, PostiveRangeRoundTowardZero) {
54   test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::TowardZero);
55 }
56 
57 // Range: [-104, 0];
58 static constexpr uint32_t NEG_START = 0x8000'0000U;
59 static constexpr uint32_t NEG_STOP = 0xc2d0'0000U;
60 
TEST_F(LlvmLibcExpm1fExhaustiveTest,NegativeRangeRoundNearestTieToEven)61 TEST_F(LlvmLibcExpm1fExhaustiveTest, NegativeRangeRoundNearestTieToEven) {
62   test_full_range(NEG_START, NEG_STOP, mpfr::RoundingMode::Nearest);
63 }
64 
TEST_F(LlvmLibcExpm1fExhaustiveTest,NegativeRangeRoundUp)65 TEST_F(LlvmLibcExpm1fExhaustiveTest, NegativeRangeRoundUp) {
66   test_full_range(NEG_START, NEG_STOP, mpfr::RoundingMode::Upward);
67 }
68 
TEST_F(LlvmLibcExpm1fExhaustiveTest,NegativeRangeRoundDown)69 TEST_F(LlvmLibcExpm1fExhaustiveTest, NegativeRangeRoundDown) {
70   test_full_range(NEG_START, NEG_STOP, mpfr::RoundingMode::Downward);
71 }
72 
TEST_F(LlvmLibcExpm1fExhaustiveTest,NegativeRangeRoundTowardZero)73 TEST_F(LlvmLibcExpm1fExhaustiveTest, NegativeRangeRoundTowardZero) {
74   test_full_range(NEG_START, NEG_STOP, mpfr::RoundingMode::TowardZero);
75 }
76