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