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