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   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::Exp2, x,
29                                   __llvm_libc::exp2f(x), 0.5, rounding);
30     } while (bits++ < stop);
31     return result;
32   }
33 };
34 
35 static constexpr int NUM_THREADS = 16;
36 
37 // Range: [0, 128];
38 static constexpr uint32_t POS_START = 0x0000'0000U;
39 static constexpr uint32_t POS_STOP = 0x4300'0000U;
40 
41 TEST_F(LlvmLibcExp2fExhaustiveTest, PostiveRangeRoundNearestTieToEven) {
42   test_full_range(POS_START, POS_STOP, NUM_THREADS,
43                   mpfr::RoundingMode::Nearest);
44 }
45 
46 TEST_F(LlvmLibcExp2fExhaustiveTest, PostiveRangeRoundUp) {
47   test_full_range(POS_START, POS_STOP, NUM_THREADS, mpfr::RoundingMode::Upward);
48 }
49 
50 TEST_F(LlvmLibcExp2fExhaustiveTest, PostiveRangeRoundDown) {
51   test_full_range(POS_START, POS_STOP, NUM_THREADS,
52                   mpfr::RoundingMode::Downward);
53 }
54 
55 TEST_F(LlvmLibcExp2fExhaustiveTest, PostiveRangeRoundTowardZero) {
56   test_full_range(POS_START, POS_STOP, NUM_THREADS,
57                   mpfr::RoundingMode::TowardZero);
58 }
59 
60 // Range: [-150, 0];
61 static constexpr uint32_t NEG_START = 0x8000'0000U;
62 static constexpr uint32_t NEG_STOP = 0xc316'0000U;
63 
64 TEST_F(LlvmLibcExp2fExhaustiveTest, NegativeRangeRoundNearestTieToEven) {
65   test_full_range(NEG_START, NEG_STOP, NUM_THREADS,
66                   mpfr::RoundingMode::Nearest);
67 }
68 
69 TEST_F(LlvmLibcExp2fExhaustiveTest, NegativeRangeRoundUp) {
70   test_full_range(NEG_START, NEG_STOP, NUM_THREADS, mpfr::RoundingMode::Upward);
71 }
72 
73 TEST_F(LlvmLibcExp2fExhaustiveTest, NegativeRangeRoundDown) {
74   test_full_range(NEG_START, NEG_STOP, NUM_THREADS,
75                   mpfr::RoundingMode::Downward);
76 }
77 
78 TEST_F(LlvmLibcExp2fExhaustiveTest, NegativeRangeRoundTowardZero) {
79   test_full_range(NEG_START, NEG_STOP, NUM_THREADS,
80                   mpfr::RoundingMode::TowardZero);
81 }
82