145 lines · cpp
1//===-- Unittests for log -------------------------------------------------===//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 "hdr/stdint_proxy.h"11#include "src/__support/FPUtil/FPBits.h"12#include "src/__support/macros/optimization.h"13#include "src/math/log.h"14#include "test/UnitTest/FPMatcher.h"15#include "test/UnitTest/Test.h"16#include "utils/MPFRWrapper/MPFRUtils.h"17 18#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS19#define TOLERANCE 120#else21#define TOLERANCE 022#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS23 24using LlvmLibcLogTest = LIBC_NAMESPACE::testing::FPTest<double>;25 26namespace mpfr = LIBC_NAMESPACE::testing::mpfr;27using LIBC_NAMESPACE::testing::tlog;28 29TEST_F(LlvmLibcLogTest, SpecialNumbers) {30 EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::log(aNaN));31 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::log(inf));32 EXPECT_FP_IS_NAN_WITH_EXCEPTION(LIBC_NAMESPACE::log(neg_inf), FE_INVALID);33 EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, LIBC_NAMESPACE::log(0.0), FE_DIVBYZERO);34 EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, LIBC_NAMESPACE::log(-0.0), FE_DIVBYZERO);35 EXPECT_FP_IS_NAN_WITH_EXCEPTION(LIBC_NAMESPACE::log(-1.0), FE_INVALID);36 EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::log(1.0));37}38 39TEST_F(LlvmLibcLogTest, TrickyInputs) {40 constexpr int N = 30;41 constexpr uint64_t INPUTS[N] = {42 0x3ff0000000000000, // x = 1.043 0x4024000000000000, // x = 10.044 0x4059000000000000, // x = 10^245 0x408f400000000000, // x = 10^346 0x40c3880000000000, // x = 10^447 0x40f86a0000000000, // x = 10^548 0x412e848000000000, // x = 10^649 0x416312d000000000, // x = 10^750 0x4197d78400000000, // x = 10^851 0x41cdcd6500000000, // x = 10^952 0x4202a05f20000000, // x = 10^1053 0x42374876e8000000, // x = 10^1154 0x426d1a94a2000000, // x = 10^1255 0x42a2309ce5400000, // x = 10^1356 0x42d6bcc41e900000, // x = 10^1457 0x430c6bf526340000, // x = 10^1558 0x4341c37937e08000, // x = 10^1659 0x4376345785d8a000, // x = 10^1760 0x43abc16d674ec800, // x = 10^1861 0x43e158e460913d00, // x = 10^1962 0x4415af1d78b58c40, // x = 10^2063 0x444b1ae4d6e2ef50, // x = 10^2164 0x4480f0cf064dd592, // x = 10^2265 0x3fefffffffef06ad, 0x3fefde0f22c7d0eb, 0x225e7812faadb32f,66 0x3fee1076964c2903, 0x3fdfe93fff7fceb0, 0x3ff012631ad8df10,67 0x3fefbfdaa448ed98,68 };69 for (int i = 0; i < N; ++i) {70 double x = FPBits(INPUTS[i]).get_val();71 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log, x,72 LIBC_NAMESPACE::log(x), 0.5);73 }74}75 76TEST_F(LlvmLibcLogTest, AllExponents) {77 double x = 0x1.0p-1074;78 for (int i = -1074; i < 1024; ++i, x *= 2.0) {79 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log, x,80 LIBC_NAMESPACE::log(x), 0.5);81 }82}83 84TEST_F(LlvmLibcLogTest, InDoubleRange) {85 constexpr uint64_t COUNT = 234561;86 constexpr uint64_t START = 0x3FD0'0000'0000'0000ULL; // 0.2587 constexpr uint64_t STOP = 0x4010'0000'0000'0000ULL; // 4.088 // constexpr uint64_t START = 0x3FF0'0000'0000'0000ULL; // 1.089 // constexpr uint64_t STOP = 0x4000'0000'0000'0000ULL; // 2.090 constexpr uint64_t STEP = (STOP - START) / COUNT;91 92 auto test = [&](mpfr::RoundingMode rounding_mode) {93 mpfr::ForceRoundingMode __r(rounding_mode);94 if (!__r.success)95 return;96 97 uint64_t fails = 0;98 uint64_t count = 0;99 uint64_t cc = 0;100 double mx, mr = 0.0;101 double tol = 0.5;102 103 for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {104 double x = FPBits(v).get_val();105 if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 0.0)106 continue;107 double result = LIBC_NAMESPACE::log(x);108 ++cc;109 if (FPBits(result).is_nan() || FPBits(result).is_inf())110 continue;111 112 ++count;113 // ASSERT_MPFR_MATCH(mpfr::Operation::Log, x, result, 0.5);114 if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Log, x, result,115 TOLERANCE + 0.5, rounding_mode)) {116 ++fails;117 while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Log, x,118 result, tol, rounding_mode)) {119 mx = x;120 mr = result;121 tol *= 2.0;122 }123 }124 }125 if (fails) {126 tlog << " Log failed: " << fails << "/" << count << "/" << cc127 << " tests.\n";128 tlog << " Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";129 EXPECT_MPFR_MATCH(mpfr::Operation::Log, mx, mr, 0.5, rounding_mode);130 }131 };132 133 tlog << " Test Rounding To Nearest...\n";134 test(mpfr::RoundingMode::Nearest);135 136 tlog << " Test Rounding Downward...\n";137 test(mpfr::RoundingMode::Downward);138 139 tlog << " Test Rounding Upward...\n";140 test(mpfr::RoundingMode::Upward);141 142 tlog << " Test Rounding Toward Zero...\n";143 test(mpfr::RoundingMode::TowardZero);144}145