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> {
checkLlvmLibcExp2fExhaustiveTest20   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 // Range: [0, 128];
36 static constexpr uint32_t POS_START = 0x0000'0000U;
37 static constexpr uint32_t POS_STOP = 0x4300'0000U;
38 
TEST_F(LlvmLibcExp2fExhaustiveTest,PostiveRangeRoundNearestTieToEven)39 TEST_F(LlvmLibcExp2fExhaustiveTest, PostiveRangeRoundNearestTieToEven) {
40   test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::Nearest);
41 }
42 
TEST_F(LlvmLibcExp2fExhaustiveTest,PostiveRangeRoundUp)43 TEST_F(LlvmLibcExp2fExhaustiveTest, PostiveRangeRoundUp) {
44   test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::Upward);
45 }
46 
TEST_F(LlvmLibcExp2fExhaustiveTest,PostiveRangeRoundDown)47 TEST_F(LlvmLibcExp2fExhaustiveTest, PostiveRangeRoundDown) {
48   test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::Downward);
49 }
50 
TEST_F(LlvmLibcExp2fExhaustiveTest,PostiveRangeRoundTowardZero)51 TEST_F(LlvmLibcExp2fExhaustiveTest, PostiveRangeRoundTowardZero) {
52   test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::TowardZero);
53 }
54 
55 // Range: [-150, 0];
56 static constexpr uint32_t NEG_START = 0x8000'0000U;
57 static constexpr uint32_t NEG_STOP = 0xc316'0000U;
58 
TEST_F(LlvmLibcExp2fExhaustiveTest,NegativeRangeRoundNearestTieToEven)59 TEST_F(LlvmLibcExp2fExhaustiveTest, NegativeRangeRoundNearestTieToEven) {
60   test_full_range(NEG_START, NEG_STOP, mpfr::RoundingMode::Nearest);
61 }
62 
TEST_F(LlvmLibcExp2fExhaustiveTest,NegativeRangeRoundUp)63 TEST_F(LlvmLibcExp2fExhaustiveTest, NegativeRangeRoundUp) {
64   test_full_range(NEG_START, NEG_STOP, mpfr::RoundingMode::Upward);
65 }
66 
TEST_F(LlvmLibcExp2fExhaustiveTest,NegativeRangeRoundDown)67 TEST_F(LlvmLibcExp2fExhaustiveTest, NegativeRangeRoundDown) {
68   test_full_range(NEG_START, NEG_STOP, mpfr::RoundingMode::Downward);
69 }
70 
TEST_F(LlvmLibcExp2fExhaustiveTest,NegativeRangeRoundTowardZero)71 TEST_F(LlvmLibcExp2fExhaustiveTest, NegativeRangeRoundTowardZero) {
72   test_full_range(NEG_START, NEG_STOP, mpfr::RoundingMode::TowardZero);
73 }
74