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 using FPBits = __llvm_libc::fputil::FPBits<float>;
16 
17 namespace mpfr = __llvm_libc::testing::mpfr;
18 
19 struct LlvmLibcExpfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
20   bool check(uint32_t start, uint32_t stop,
21              mpfr::RoundingMode rounding) override {
22     mpfr::ForceRoundingMode r(rounding);
23     uint32_t bits = start;
24     bool result = true;
25     do {
26       FPBits xbits(bits);
27       float x = float(xbits);
28       result &= EXPECT_MPFR_MATCH(mpfr::Operation::Expm1, x,
29                                   __llvm_libc::expm1f(x), 0.5, rounding);
30     } while (bits++ < stop);
31   }
32 };
33 
34 static constexpr int NUM_THREADS = 16;
35 
36 // Range: [0, 89];
37 static constexpr uint32_t POS_START = 0x0000'0000U;
38 static constexpr uint32_t POS_STOP = 0x42b2'0000U;
39 
40 TEST_F(LlvmLibcExpfExhaustiveTest, PostiveRangeRoundNearestTieToEven) {
41   test_full_range(POS_START, POS_STOP, NUM_THREADS,
42                   mpfr::RoundingMode::Nearest);
43 }
44 
45 TEST_F(LlvmLibcExpfExhaustiveTest, PostiveRangeRoundUp) {
46   test_full_range(POS_START, POS_STOP, NUM_THREADS, mpfr::RoundingMode::Upward);
47 }
48 
49 TEST_F(LlvmLibcExpfExhaustiveTest, PostiveRangeRoundDown) {
50   test_full_range(POS_START, POS_STOP, NUM_THREADS,
51                   mpfr::RoundingMode::Downward);
52 }
53 
54 TEST_F(LlvmLibcExpfExhaustiveTest, PostiveRangeRoundTowardZero) {
55   test_full_range(POS_START, POS_STOP, NUM_THREADS,
56                   mpfr::RoundingMode::TowardZero);
57 }
58 
59 // Range: [-104, 0];
60 static constexpr uint32_t NEG_START = 0x8000'0000U;
61 static constexpr uint32_t NEG_STOP = 0xc2d0'0000U;
62 
63 TEST_F(LlvmLibcExpfExhaustiveTest, NegativeRangeRoundNearestTieToEven) {
64   test_full_range(NEG_START, NEG_STOP, NUM_THREADS,
65                   mpfr::RoundingMode::Nearest);
66 }
67 
68 TEST_F(LlvmLibcExpfExhaustiveTest, NegativeRangeRoundUp) {
69   test_full_range(NEG_START, NEG_STOP, NUM_THREADS, mpfr::RoundingMode::Upward);
70 }
71 
72 TEST_F(LlvmLibcExpfExhaustiveTest, NegativeRangeRoundDown) {
73   test_full_range(NEG_START, NEG_STOP, NUM_THREADS,
74                   mpfr::RoundingMode::Downward);
75 }
76 
77 TEST_F(LlvmLibcExpfExhaustiveTest, NegativeRangeRoundTowardZero) {
78   test_full_range(NEG_START, NEG_STOP, NUM_THREADS,
79                   mpfr::RoundingMode::TowardZero);
80 }
81