163d2df00STue Ly //===-- Exhaustive test for log2f -----------------------------------------===//
263d2df00STue Ly //
363d2df00STue Ly // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
463d2df00STue Ly // See https://llvm.org/LICENSE.txt for license information.
563d2df00STue Ly // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
663d2df00STue Ly //
763d2df00STue Ly //===----------------------------------------------------------------------===//
863d2df00STue Ly
963d2df00STue Ly #include "exhaustive_test.h"
1063d2df00STue Ly #include "src/__support/FPUtil/FPBits.h"
1163d2df00STue Ly #include "src/math/log2f.h"
1263d2df00STue Ly #include "utils/MPFRWrapper/MPFRUtils.h"
1363d2df00STue Ly #include "utils/UnitTest/FPMatcher.h"
1463d2df00STue Ly
1563d2df00STue Ly using FPBits = __llvm_libc::fputil::FPBits<float>;
1663d2df00STue Ly
1763d2df00STue Ly namespace mpfr = __llvm_libc::testing::mpfr;
1863d2df00STue Ly
1963d2df00STue Ly struct LlvmLibcLog2fExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
checkLlvmLibcLog2fExhaustiveTest204c9bfec6STue Ly bool check(uint32_t start, uint32_t stop,
2163d2df00STue Ly mpfr::RoundingMode rounding) override {
2263d2df00STue Ly mpfr::ForceRoundingMode r(rounding);
2363d2df00STue Ly uint32_t bits = start;
244c9bfec6STue Ly bool result = true;
2563d2df00STue Ly do {
2663d2df00STue Ly FPBits xbits(bits);
2763d2df00STue Ly float x = float(xbits);
284c9bfec6STue Ly result &= EXPECT_MPFR_MATCH(mpfr::Operation::Log2, x,
294c9bfec6STue Ly __llvm_libc::log2f(x), 0.5, rounding);
3063d2df00STue Ly } while (bits++ < stop);
314c9bfec6STue Ly return result;
3263d2df00STue Ly }
3363d2df00STue Ly };
3463d2df00STue Ly
TEST_F(LlvmLibcLog2fExhaustiveTest,RoundNearestTieToEven)3563d2df00STue Ly TEST_F(LlvmLibcLog2fExhaustiveTest, RoundNearestTieToEven) {
36*fdf1fda5SKirill Okhotnikov test_full_range(/*start=*/0U, /*stop=*/0x7f80'0000U,
3763d2df00STue Ly mpfr::RoundingMode::Nearest);
3863d2df00STue Ly }
3963d2df00STue Ly
TEST_F(LlvmLibcLog2fExhaustiveTest,RoundUp)4063d2df00STue Ly TEST_F(LlvmLibcLog2fExhaustiveTest, RoundUp) {
41*fdf1fda5SKirill Okhotnikov test_full_range(/*start=*/0U, /*stop=*/0x7f80'0000U,
4263d2df00STue Ly mpfr::RoundingMode::Upward);
4363d2df00STue Ly }
4463d2df00STue Ly
TEST_F(LlvmLibcLog2fExhaustiveTest,RoundDown)4563d2df00STue Ly TEST_F(LlvmLibcLog2fExhaustiveTest, RoundDown) {
46*fdf1fda5SKirill Okhotnikov test_full_range(/*start=*/0U, /*stop=*/0x7f80'0000U,
4763d2df00STue Ly mpfr::RoundingMode::Downward);
4863d2df00STue Ly }
4963d2df00STue Ly
TEST_F(LlvmLibcLog2fExhaustiveTest,RoundTowardZero)5063d2df00STue Ly TEST_F(LlvmLibcLog2fExhaustiveTest, RoundTowardZero) {
51*fdf1fda5SKirill Okhotnikov test_full_range(/*start=*/0U, /*stop=*/0x7f80'0000U,
5263d2df00STue Ly mpfr::RoundingMode::TowardZero);
5363d2df00STue Ly }
54