140 lines · cpp
1//===-- Unittests for log2 ------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "hdr/math_macros.h"10#include "src/__support/FPUtil/FPBits.h"11#include "src/math/log2.h"12#include "test/UnitTest/FPMatcher.h"13#include "test/UnitTest/Test.h"14#include "utils/MPFRWrapper/MPFRUtils.h"15 16#include "hdr/stdint_proxy.h"17 18using LlvmLibcLog2Test = LIBC_NAMESPACE::testing::FPTest<double>;19 20namespace mpfr = LIBC_NAMESPACE::testing::mpfr;21using LIBC_NAMESPACE::testing::tlog;22 23TEST_F(LlvmLibcLog2Test, SpecialNumbers) {24 EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::log2(aNaN));25 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::log2(inf));26 EXPECT_FP_IS_NAN_WITH_EXCEPTION(LIBC_NAMESPACE::log2(neg_inf), FE_INVALID);27 EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, LIBC_NAMESPACE::log2(0.0), FE_DIVBYZERO);28 EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, LIBC_NAMESPACE::log2(-0.0),29 FE_DIVBYZERO);30 EXPECT_FP_IS_NAN_WITH_EXCEPTION(LIBC_NAMESPACE::log2(-1.0), FE_INVALID);31 EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::log2(1.0));32}33 34TEST_F(LlvmLibcLog2Test, TrickyInputs) {35 constexpr int N = 30;36 constexpr uint64_t INPUTS[N] = {37 0x3ff0000000000000, // x = 1.038 0x4024000000000000, // x = 10.039 0x4059000000000000, // x = 10^240 0x408f400000000000, // x = 10^341 0x40c3880000000000, // x = 10^442 0x40f86a0000000000, // x = 10^543 0x412e848000000000, // x = 10^644 0x416312d000000000, // x = 10^745 0x4197d78400000000, // x = 10^846 0x41cdcd6500000000, // x = 10^947 0x4202a05f20000000, // x = 10^1048 0x42374876e8000000, // x = 10^1149 0x426d1a94a2000000, // x = 10^1250 0x42a2309ce5400000, // x = 10^1351 0x42d6bcc41e900000, // x = 10^1452 0x430c6bf526340000, // x = 10^1553 0x4341c37937e08000, // x = 10^1654 0x4376345785d8a000, // x = 10^1755 0x43abc16d674ec800, // x = 10^1856 0x43e158e460913d00, // x = 10^1957 0x4415af1d78b58c40, // x = 10^2058 0x444b1ae4d6e2ef50, // x = 10^2159 0x4480f0cf064dd592, // x = 10^2260 0x3fefffffffef06ad, 0x3fefde0f22c7d0eb, 0x225e7812faadb32f,61 0x3fee1076964c2903, 0x3fdfe93fff7fceb0, 0x3ff012631ad8df10,62 0x3fefbfdaa448ed98,63 };64 for (int i = 0; i < N; ++i) {65 double x = FPBits(INPUTS[i]).get_val();66 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log2, x,67 LIBC_NAMESPACE::log2(x), 0.5);68 }69}70 71TEST_F(LlvmLibcLog2Test, AllExponents) {72 double x = 0x1.0p-1074;73 for (int i = -1074; i < 1024; ++i, x *= 2.0) {74 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log2, x,75 LIBC_NAMESPACE::log2(x), 0.5);76 }77}78 79TEST_F(LlvmLibcLog2Test, InDoubleRange) {80 constexpr uint64_t COUNT = 1'001;81 constexpr uint64_t START = 0x3FD0'0000'0000'0000ULL; // 0.2582 constexpr uint64_t STOP = 0x4010'0000'0000'0000ULL; // 4.083 // constexpr uint64_t START = 0x3FF0'0000'0000'0000ULL; // 1.084 // constexpr uint64_t STOP = 0x4000'0000'0000'0000ULL; // 2.085 constexpr uint64_t STEP = (STOP - START) / COUNT;86 87 auto test = [&](mpfr::RoundingMode rounding_mode) {88 mpfr::ForceRoundingMode __r(rounding_mode);89 if (!__r.success)90 return;91 92 uint64_t fails = 0;93 uint64_t count = 0;94 uint64_t cc = 0;95 double mx, mr = 0.0;96 double tol = 0.5;97 98 for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {99 double x = FPBits(v).get_val();100 if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 0.0)101 continue;102 double result = LIBC_NAMESPACE::log2(x);103 ++cc;104 if (FPBits(result).is_nan() || FPBits(result).is_inf())105 continue;106 107 ++count;108 // ASSERT_MPFR_MATCH(mpfr::Operation::Log2, x, result, 0.5);109 if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Log2, x, result,110 0.5, rounding_mode)) {111 ++fails;112 while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Log2, x,113 result, tol, rounding_mode)) {114 mx = x;115 mr = result;116 tol *= 2.0;117 }118 }119 }120 if (fails) {121 tlog << " Log2 failed: " << fails << "/" << count << "/" << cc122 << " tests.\n";123 tlog << " Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";124 EXPECT_MPFR_MATCH(mpfr::Operation::Log2, mx, mr, 0.5, rounding_mode);125 }126 };127 128 tlog << " Test Rounding To Nearest...\n";129 test(mpfr::RoundingMode::Nearest);130 131 tlog << " Test Rounding Downward...\n";132 test(mpfr::RoundingMode::Downward);133 134 tlog << " Test Rounding Upward...\n";135 test(mpfr::RoundingMode::Upward);136 137 tlog << " Test Rounding Toward Zero...\n";138 test(mpfr::RoundingMode::TowardZero);139}140