1 //===-- Exhaustive test for log2f -----------------------------------------===//
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/log2f.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 LlvmLibcLog2fExhaustiveTest : 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::Log2, x, __llvm_libc::log2f(x), 0.5,
28                         rounding);
29     } while (bits++ < stop);
30   }
31 };
32 
33 TEST_F(LlvmLibcLog2fExhaustiveTest, RoundNearestTieToEven) {
34   test_full_range(/*start=*/0U, /*stop=*/0x7f80'0000U, /*nthreads=*/16,
35                   mpfr::RoundingMode::Nearest);
36 }
37 
38 TEST_F(LlvmLibcLog2fExhaustiveTest, RoundUp) {
39   test_full_range(/*start=*/0U, /*stop=*/0x7f80'0000U, /*nthreads=*/16,
40                   mpfr::RoundingMode::Upward);
41 }
42 
43 TEST_F(LlvmLibcLog2fExhaustiveTest, RoundDown) {
44   test_full_range(/*start=*/0U, /*stop=*/0x7f80'0000U, /*nthreads=*/16,
45                   mpfr::RoundingMode::Downward);
46 }
47 
48 TEST_F(LlvmLibcLog2fExhaustiveTest, RoundTowardZero) {
49   test_full_range(/*start=*/0U, /*stop=*/0x7f80'0000U, /*nthreads=*/16,
50                   mpfr::RoundingMode::TowardZero);
51 }
52