174 lines · cpp
1//===-- Unittests for log1p -----------------------------------------------===//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/log1p.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 LlvmLibcLog1pTest = LIBC_NAMESPACE::testing::FPTest<double>;25 26namespace mpfr = LIBC_NAMESPACE::testing::mpfr;27using LIBC_NAMESPACE::testing::tlog;28 29TEST_F(LlvmLibcLog1pTest, SpecialNumbers) {30 EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::log1p(aNaN));31 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::log1p(inf));32 EXPECT_FP_IS_NAN_WITH_EXCEPTION(LIBC_NAMESPACE::log1p(neg_inf), FE_INVALID);33 EXPECT_FP_IS_NAN_WITH_EXCEPTION(LIBC_NAMESPACE::log1p(-2.0), FE_INVALID);34 EXPECT_FP_EQ(zero, LIBC_NAMESPACE::log1p(0.0));35 EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::log1p(-0.0));36 EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, LIBC_NAMESPACE::log1p(-1.0),37 FE_DIVBYZERO);38}39 40TEST_F(LlvmLibcLog1pTest, TrickyInputs) {41 constexpr int N = 42;42 constexpr uint64_t INPUTS[N] = {43 0x3ff0000000000000, // x = 1.044 0x4024000000000000, // x = 10.045 0x4059000000000000, // x = 10^246 0x408f400000000000, // x = 10^347 0x40c3880000000000, // x = 10^448 0x40f86a0000000000, // x = 10^549 0x412e848000000000, // x = 10^650 0x416312d000000000, // x = 10^751 0x4197d78400000000, // x = 10^852 0x41cdcd6500000000, // x = 10^953 0x4202a05f20000000, // x = 10^1054 0x42374876e8000000, // x = 10^1155 0x426d1a94a2000000, // x = 10^1256 0x42a2309ce5400000, // x = 10^1357 0x42d6bcc41e900000, // x = 10^1458 0x430c6bf526340000, // x = 10^1559 0x4341c37937e08000, // x = 10^1660 0x4376345785d8a000, // x = 10^1761 0x43abc16d674ec800, // x = 10^1862 0x43e158e460913d00, // x = 10^1963 0x4415af1d78b58c40, // x = 10^2064 0x444b1ae4d6e2ef50, // x = 10^2165 0x4480f0cf064dd592, // x = 10^2266 0x3fefffffffef06ad, 0x3fefde0f22c7d0eb, 0x225e7812faadb32f,67 0x3fee1076964c2903, 0x3fdfe93fff7fceb0, 0x3ff012631ad8df10,68 0x3fefbfdaa448ed98, 0x3fd00a8cefe9a5f8, 0x3fd0b4d870eb22f8,69 0x3c90c40cef04efb5, 0x449d2ccad399848e, 0x4aa12ccdffd9d2ec,70 0x5656f070b92d36ce, 0x6db06dcb74f76bcc, 0x7f1954e72ffd4596,71 0x5671e2f1628093e4, 0x73dac56e2bf1a951, 0x8001bc6879ea14c5,72 0x45ca5f497ec291df, // x = 0x1.a5f497ec291dfp+9373 };74 for (int i = 0; i < N; ++i) {75 double x = FPBits(INPUTS[i]).get_val();76 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log1p, x,77 LIBC_NAMESPACE::log1p(x), TOLERANCE + 0.5);78 }79}80 81TEST_F(LlvmLibcLog1pTest, AllExponents) {82 double x = 0x1.0p-1074;83 for (int i = -1074; i < 1024; ++i, x *= 2.0) {84 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log1p, x,85 LIBC_NAMESPACE::log1p(x), 0.5);86 }87}88 89TEST_F(LlvmLibcLog1pTest, InDoubleRange) {90 constexpr uint64_t COUNT = 4501;91 92 auto test = [&](uint64_t start, uint64_t stop,93 mpfr::RoundingMode rounding_mode) {94 mpfr::ForceRoundingMode __r(rounding_mode);95 if (!__r.success)96 return;97 98 uint64_t fails = 0;99 uint64_t count = 0;100 uint64_t cc = 0;101 double mx, mr = 0.0;102 double tol = 0.5;103 104 uint64_t step = (stop - start) / COUNT;105 106 for (uint64_t i = 0, v = start; i <= COUNT; ++i, v += step) {107 double x = FPBits(v).get_val();108 if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 0.0)109 continue;110 double result = LIBC_NAMESPACE::log1p(x);111 ++cc;112 if (FPBits(result).is_nan() || FPBits(result).is_inf())113 continue;114 115 ++count;116 if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Log1p, x, result,117 TOLERANCE + 0.5, rounding_mode)) {118 ++fails;119 while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Log1p, x,120 result, tol, rounding_mode)) {121 mx = x;122 mr = result;123 tol *= 2.0;124 }125 }126 }127 if (fails) {128 tlog << " Log1p failed: " << fails << "/" << count << "/" << cc129 << " tests.\n";130 tlog << " Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";131 EXPECT_MPFR_MATCH(mpfr::Operation::Log1p, mx, mr, 0.5, rounding_mode);132 }133 };134 135 auto test_all_rounding = [&](uint64_t start, uint64_t stop,136 const char *start_str, const char *stop_str) {137 tlog << "\n=== Test in range [" << start_str << ", " << stop_str138 << "] ===\n";139 140 tlog << "\n Test Rounding To Nearest...\n";141 test(start, stop, mpfr::RoundingMode::Nearest);142 143 tlog << "\n Test Rounding Downward...\n";144 test(start, stop, mpfr::RoundingMode::Downward);145 146 tlog << "\n Test Rounding Upward...\n";147 test(start, stop, mpfr::RoundingMode::Upward);148 149 tlog << "\n Test Rounding Toward Zero...\n";150 test(start, stop, mpfr::RoundingMode::TowardZero);151 };152 153 test_all_rounding(0x0000'0000'0000'0001ULL, 0x0010'0000'0000'0000ULL,154 "2^-1074", "2^-1022");155 156 test_all_rounding(0x39B0'0000'0000'0000ULL, 0x3A50'0000'0000'0000ULL,157 "2^-100", "2^-90");158 159 test_all_rounding(0x3CD0'0000'0000'0000ULL, 0x3D20'0000'0000'0000ULL, "2^-50",160 "2^-45");161 162 test_all_rounding(0x3E10'0000'0000'0000ULL, 0x3E40'0000'0000'0000ULL, "2^-30",163 "2^-27");164 165 test_all_rounding(0x3FD0'0000'0000'0000ULL, 0x4010'0000'0000'0000ULL, "0.25",166 "4.0");167 168 test_all_rounding(0x4630'0000'0000'0000ULL, 0x4670'0000'0000'0000ULL, "2^100",169 "2^104");170 171 test_all_rounding(0x7FD0'0000'0000'0000ULL, 0x7FF0'0000'0000'0000ULL,172 "2^1022", "2^1024");173}174