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-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "hdr/math_macros.h"
10#include "src/__support/FPUtil/FPBits.h"
11#include "src/errno/libc_errno.h"
12#include "src/math/log1p.h"
13#include "test/UnitTest/FPMatcher.h"
14#include "test/UnitTest/Test.h"
15#include "utils/MPFRWrapper/MPFRUtils.h"
16
17#include <errno.h>
18#include <stdint.h>
19
20using LlvmLibcLog1pTest = LIBC_NAMESPACE::testing::FPTest<double>;
21
22namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
23using LIBC_NAMESPACE::testing::tlog;
24
25TEST_F(LlvmLibcLog1pTest, SpecialNumbers) {
26 EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::log1p(aNaN));
27 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::log1p(inf));
28 EXPECT_FP_IS_NAN_WITH_EXCEPTION(LIBC_NAMESPACE::log1p(neg_inf), FE_INVALID);
29 EXPECT_FP_IS_NAN_WITH_EXCEPTION(LIBC_NAMESPACE::log1p(-2.0), FE_INVALID);
30 EXPECT_FP_EQ(zero, LIBC_NAMESPACE::log1p(0.0));
31 EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::log1p(-0.0));
32 EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, LIBC_NAMESPACE::log1p(-1.0),
33 FE_DIVBYZERO);
34}
35
36TEST_F(LlvmLibcLog1pTest, TrickyInputs) {
37 constexpr int N = 42;
38 constexpr uint64_t INPUTS[N] = {
39 0x3ff0000000000000, // x = 1.0
40 0x4024000000000000, // x = 10.0
41 0x4059000000000000, // x = 10^2
42 0x408f400000000000, // x = 10^3
43 0x40c3880000000000, // x = 10^4
44 0x40f86a0000000000, // x = 10^5
45 0x412e848000000000, // x = 10^6
46 0x416312d000000000, // x = 10^7
47 0x4197d78400000000, // x = 10^8
48 0x41cdcd6500000000, // x = 10^9
49 0x4202a05f20000000, // x = 10^10
50 0x42374876e8000000, // x = 10^11
51 0x426d1a94a2000000, // x = 10^12
52 0x42a2309ce5400000, // x = 10^13
53 0x42d6bcc41e900000, // x = 10^14
54 0x430c6bf526340000, // x = 10^15
55 0x4341c37937e08000, // x = 10^16
56 0x4376345785d8a000, // x = 10^17
57 0x43abc16d674ec800, // x = 10^18
58 0x43e158e460913d00, // x = 10^19
59 0x4415af1d78b58c40, // x = 10^20
60 0x444b1ae4d6e2ef50, // x = 10^21
61 0x4480f0cf064dd592, // x = 10^22
62 0x3fefffffffef06ad, 0x3fefde0f22c7d0eb, 0x225e7812faadb32f,
63 0x3fee1076964c2903, 0x3fdfe93fff7fceb0, 0x3ff012631ad8df10,
64 0x3fefbfdaa448ed98, 0x3fd00a8cefe9a5f8, 0x3fd0b4d870eb22f8,
65 0x3c90c40cef04efb5, 0x449d2ccad399848e, 0x4aa12ccdffd9d2ec,
66 0x5656f070b92d36ce, 0x6db06dcb74f76bcc, 0x7f1954e72ffd4596,
67 0x5671e2f1628093e4, 0x73dac56e2bf1a951, 0x8001bc6879ea14c5,
68 0x45ca5f497ec291df, // x = 0x1.a5f497ec291dfp+93
69 };
70 for (int i = 0; i < N; ++i) {
71 double x = FPBits(INPUTS[i]).get_val();
72 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log1p, x,
73 LIBC_NAMESPACE::log1p(x), 0.5);
74 }
75}
76
77TEST_F(LlvmLibcLog1pTest, AllExponents) {
78 double x = 0x1.0p-1074;
79 for (int i = -1074; i < 1024; ++i, x *= 2.0) {
80 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log1p, x,
81 LIBC_NAMESPACE::log1p(x), 0.5);
82 }
83}
84
85TEST_F(LlvmLibcLog1pTest, InDoubleRange) {
86 constexpr uint64_t COUNT = 4501;
87
88 auto test = [&](uint64_t start, uint64_t stop,
89 mpfr::RoundingMode rounding_mode) {
90 mpfr::ForceRoundingMode __r(rounding_mode);
91 if (!__r.success)
92 return;
93
94 uint64_t fails = 0;
95 uint64_t count = 0;
96 uint64_t cc = 0;
97 double mx, mr = 0.0;
98 double tol = 0.5;
99
100 uint64_t step = (stop - start) / COUNT;
101
102 for (uint64_t i = 0, v = start; i <= COUNT; ++i, v += step) {
103 double x = FPBits(v).get_val();
104 if (isnan(x: x) || isinf(x: x) || x < 0.0)
105 continue;
106 LIBC_NAMESPACE::libc_errno = 0;
107 double result = LIBC_NAMESPACE::log1p(x);
108 ++cc;
109 if (isnan(x: result) || isinf(x: result))
110 continue;
111
112 ++count;
113 // ASSERT_MPFR_MATCH(mpfr::Operation::Log1p, x, result, 0.5);
114 if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Log1p, x, result,
115 0.5, rounding_mode)) {
116 ++fails;
117 while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Log1p, x,
118 result, tol, rounding_mode)) {
119 mx = x;
120 mr = result;
121 tol *= 2.0;
122 }
123 }
124 }
125 tlog << " Log1p failed: " << fails << "/" << count << "/" << cc
126 << " tests.\n";
127 tlog << " Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";
128 if (fails) {
129 EXPECT_MPFR_MATCH(mpfr::Operation::Log1p, mx, mr, 0.5, rounding_mode);
130 }
131 };
132
133 auto test_all_rounding = [&](uint64_t start, uint64_t stop,
134 const char *start_str, const char *stop_str) {
135 tlog << "\n=== Test in range [" << start_str << ", " << stop_str
136 << "] ===\n";
137
138 tlog << "\n Test Rounding To Nearest...\n";
139 test(start, stop, mpfr::RoundingMode::Nearest);
140
141 tlog << "\n Test Rounding Downward...\n";
142 test(start, stop, mpfr::RoundingMode::Downward);
143
144 tlog << "\n Test Rounding Upward...\n";
145 test(start, stop, mpfr::RoundingMode::Upward);
146
147 tlog << "\n Test Rounding Toward Zero...\n";
148 test(start, stop, mpfr::RoundingMode::TowardZero);
149 };
150
151 test_all_rounding(0x0000'0000'0000'0001ULL, 0x0010'0000'0000'0000ULL,
152 "2^-1074", "2^-1022");
153
154 test_all_rounding(0x39B0'0000'0000'0000ULL, 0x3A50'0000'0000'0000ULL,
155 "2^-100", "2^-90");
156
157 test_all_rounding(0x3CD0'0000'0000'0000ULL, 0x3D20'0000'0000'0000ULL, "2^-50",
158 "2^-45");
159
160 test_all_rounding(0x3E10'0000'0000'0000ULL, 0x3E40'0000'0000'0000ULL, "2^-30",
161 "2^-27");
162
163 test_all_rounding(0x3FD0'0000'0000'0000ULL, 0x4010'0000'0000'0000ULL, "0.25",
164 "4.0");
165
166 test_all_rounding(0x4630'0000'0000'0000ULL, 0x4670'0000'0000'0000ULL, "2^100",
167 "2^104");
168
169 test_all_rounding(0x7FD0'0000'0000'0000ULL, 0x7FF0'0000'0000'0000ULL,
170 "2^1022", "2^1024");
171}
172

source code of libc/test/src/math/log1p_test.cpp