164721a33STue Ly //===-- Exhaustive test for exp2f -----------------------------------------===//
264721a33STue Ly //
364721a33STue Ly // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
464721a33STue Ly // See https://llvm.org/LICENSE.txt for license information.
564721a33STue Ly // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
664721a33STue Ly //
764721a33STue Ly //===----------------------------------------------------------------------===//
864721a33STue Ly
964721a33STue Ly #include "exhaustive_test.h"
1064721a33STue Ly #include "src/__support/FPUtil/FPBits.h"
1164721a33STue Ly #include "src/math/exp2f.h"
1264721a33STue Ly #include "utils/MPFRWrapper/MPFRUtils.h"
1364721a33STue Ly #include "utils/UnitTest/FPMatcher.h"
1464721a33STue Ly
1564721a33STue Ly using FPBits = __llvm_libc::fputil::FPBits<float>;
1664721a33STue Ly
1764721a33STue Ly namespace mpfr = __llvm_libc::testing::mpfr;
1864721a33STue Ly
1964721a33STue Ly struct LlvmLibcExp2fExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
checkLlvmLibcExp2fExhaustiveTest204c9bfec6STue Ly bool check(uint32_t start, uint32_t stop,
2164721a33STue Ly mpfr::RoundingMode rounding) override {
2264721a33STue Ly mpfr::ForceRoundingMode r(rounding);
2364721a33STue Ly uint32_t bits = start;
244c9bfec6STue Ly bool result = true;
2564721a33STue Ly do {
2664721a33STue Ly FPBits xbits(bits);
2764721a33STue Ly float x = float(xbits);
284c9bfec6STue Ly result &= EXPECT_MPFR_MATCH(mpfr::Operation::Exp2, x,
294c9bfec6STue Ly __llvm_libc::exp2f(x), 0.5, rounding);
3064721a33STue Ly } while (bits++ < stop);
314c9bfec6STue Ly return result;
3264721a33STue Ly }
3364721a33STue Ly };
3464721a33STue Ly
3564721a33STue Ly // Range: [0, 128];
3664721a33STue Ly static constexpr uint32_t POS_START = 0x0000'0000U;
3764721a33STue Ly static constexpr uint32_t POS_STOP = 0x4300'0000U;
3864721a33STue Ly
TEST_F(LlvmLibcExp2fExhaustiveTest,PostiveRangeRoundNearestTieToEven)3964721a33STue Ly TEST_F(LlvmLibcExp2fExhaustiveTest, PostiveRangeRoundNearestTieToEven) {
40*fdf1fda5SKirill Okhotnikov test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::Nearest);
4164721a33STue Ly }
4264721a33STue Ly
TEST_F(LlvmLibcExp2fExhaustiveTest,PostiveRangeRoundUp)4364721a33STue Ly TEST_F(LlvmLibcExp2fExhaustiveTest, PostiveRangeRoundUp) {
44*fdf1fda5SKirill Okhotnikov test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::Upward);
4564721a33STue Ly }
4664721a33STue Ly
TEST_F(LlvmLibcExp2fExhaustiveTest,PostiveRangeRoundDown)4764721a33STue Ly TEST_F(LlvmLibcExp2fExhaustiveTest, PostiveRangeRoundDown) {
48*fdf1fda5SKirill Okhotnikov test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::Downward);
4964721a33STue Ly }
5064721a33STue Ly
TEST_F(LlvmLibcExp2fExhaustiveTest,PostiveRangeRoundTowardZero)5164721a33STue Ly TEST_F(LlvmLibcExp2fExhaustiveTest, PostiveRangeRoundTowardZero) {
52*fdf1fda5SKirill Okhotnikov test_full_range(POS_START, POS_STOP, mpfr::RoundingMode::TowardZero);
5364721a33STue Ly }
5464721a33STue Ly
5564721a33STue Ly // Range: [-150, 0];
5664721a33STue Ly static constexpr uint32_t NEG_START = 0x8000'0000U;
5764721a33STue Ly static constexpr uint32_t NEG_STOP = 0xc316'0000U;
5864721a33STue Ly
TEST_F(LlvmLibcExp2fExhaustiveTest,NegativeRangeRoundNearestTieToEven)5964721a33STue Ly TEST_F(LlvmLibcExp2fExhaustiveTest, NegativeRangeRoundNearestTieToEven) {
60*fdf1fda5SKirill Okhotnikov test_full_range(NEG_START, NEG_STOP, mpfr::RoundingMode::Nearest);
6164721a33STue Ly }
6264721a33STue Ly
TEST_F(LlvmLibcExp2fExhaustiveTest,NegativeRangeRoundUp)6364721a33STue Ly TEST_F(LlvmLibcExp2fExhaustiveTest, NegativeRangeRoundUp) {
64*fdf1fda5SKirill Okhotnikov test_full_range(NEG_START, NEG_STOP, mpfr::RoundingMode::Upward);
6564721a33STue Ly }
6664721a33STue Ly
TEST_F(LlvmLibcExp2fExhaustiveTest,NegativeRangeRoundDown)6764721a33STue Ly TEST_F(LlvmLibcExp2fExhaustiveTest, NegativeRangeRoundDown) {
68*fdf1fda5SKirill Okhotnikov test_full_range(NEG_START, NEG_STOP, mpfr::RoundingMode::Downward);
6964721a33STue Ly }
7064721a33STue Ly
TEST_F(LlvmLibcExp2fExhaustiveTest,NegativeRangeRoundTowardZero)7164721a33STue Ly TEST_F(LlvmLibcExp2fExhaustiveTest, NegativeRangeRoundTowardZero) {
72*fdf1fda5SKirill Okhotnikov test_full_range(NEG_START, NEG_STOP, mpfr::RoundingMode::TowardZero);
7364721a33STue Ly }
74