1 //===-- Unittests for e^x - 1 ---------------------------------------------===//
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/expm1.h"
13 #include "test/UnitTest/FPMatcher.h"
14 #include "test/UnitTest/Test.h"
15 #include "utils/MPFRWrapper/MPFRUtils.h"
16
17 #include <stdint.h>
18
19 using LlvmLibcExpm1Test = LIBC_NAMESPACE::testing::FPTest<double>;
20
21 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
22 using LIBC_NAMESPACE::testing::tlog;
23
TEST_F(LlvmLibcExpm1Test,TrickyInputs)24 TEST_F(LlvmLibcExpm1Test, TrickyInputs) {
25 constexpr double INPUTS[] = {
26 0x1.71547652b82fep-54, 0x1.465655f122ff6p-49, 0x1.bc8ee6b28659ap-46,
27 0x1.8442b169f672dp-14, 0x1.9a61fb925970dp-14, 0x1.eb7a4cb841fccp-14,
28 0x1.05de80a173eap-2, 0x1.79289c6e6a5cp-2, 0x1.a7b764e2cf47ap-2,
29 0x1.b4f0cfb15ca0fp+3, 0x1.9a74cdab36c28p+4, 0x1.2b708872320ddp+5,
30 0x1.4c19e5712e377p+5, 0x1.757852a4b93aap+5, 0x1.77f74111e0894p+6,
31 0x1.a6c3780bbf824p+6, 0x1.e3d57e4c557f6p+6, 0x1.f07560077985ap+6,
32 0x1.1f0da93354198p+7, 0x1.71018579c0758p+7, 0x1.204684c1167e9p+8,
33 0x1.5b3e4e2e3bba9p+9, 0x1.6232c09d58d91p+9, 0x1.67a172ceb099p+9,
34 0x1.6960d591aec34p+9, 0x1.74910d52d3051p+9, 0x1.ff8p+9,
35 };
36 constexpr int N = sizeof(INPUTS) / sizeof(INPUTS[0]);
37 for (int i = 0; i < N; ++i) {
38 double x = INPUTS[i];
39 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Expm1, x,
40 LIBC_NAMESPACE::expm1(x), 0.5);
41 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Expm1, -x,
42 LIBC_NAMESPACE::expm1(-x), 0.5);
43 }
44 }
45
TEST_F(LlvmLibcExpm1Test,InDoubleRange)46 TEST_F(LlvmLibcExpm1Test, InDoubleRange) {
47 constexpr uint64_t COUNT = 1'231;
48 uint64_t START = LIBC_NAMESPACE::fputil::FPBits<double>(0.25).uintval();
49 uint64_t STOP = LIBC_NAMESPACE::fputil::FPBits<double>(4.0).uintval();
50 uint64_t STEP = (STOP - START) / COUNT;
51
52 auto test = [&](mpfr::RoundingMode rounding_mode) {
53 mpfr::ForceRoundingMode __r(rounding_mode);
54 if (!__r.success)
55 return;
56
57 uint64_t fails = 0;
58 uint64_t count = 0;
59 uint64_t cc = 0;
60 double mx, mr = 0.0;
61 double tol = 0.5;
62
63 for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
64 double x = FPBits(v).get_val();
65 if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 0.0)
66 continue;
67 LIBC_NAMESPACE::libc_errno = 0;
68 double result = LIBC_NAMESPACE::expm1(x);
69 ++cc;
70 if (FPBits(result).is_nan() || FPBits(result).is_inf())
71 continue;
72
73 ++count;
74
75 if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Expm1, x, result,
76 0.5, rounding_mode)) {
77 ++fails;
78 while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Expm1, x,
79 result, tol, rounding_mode)) {
80 mx = x;
81 mr = result;
82
83 if (tol > 1000.0)
84 break;
85
86 tol *= 2.0;
87 }
88 }
89 }
90 if (fails) {
91 tlog << " Expm1 failed: " << fails << "/" << count << "/" << cc
92 << " tests.\n";
93 tlog << " Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";
94 EXPECT_MPFR_MATCH(mpfr::Operation::Expm1, mx, mr, 0.5, rounding_mode);
95 }
96 };
97
98 tlog << " Test Rounding To Nearest...\n";
99 test(mpfr::RoundingMode::Nearest);
100
101 tlog << " Test Rounding Downward...\n";
102 test(mpfr::RoundingMode::Downward);
103
104 tlog << " Test Rounding Upward...\n";
105 test(mpfr::RoundingMode::Upward);
106
107 tlog << " Test Rounding Toward Zero...\n";
108 test(mpfr::RoundingMode::TowardZero);
109 }
110