1//===-- Unittests for exp -------------------------------------------------===//
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/exp.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 LlvmLibcExpTest = LIBC_NAMESPACE::testing::FPTest<double>;
21
22namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
23using LIBC_NAMESPACE::testing::tlog;
24
25TEST_F(LlvmLibcExpTest, SpecialNumbers) {
26 EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::exp(aNaN));
27 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::exp(inf));
28 EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::exp(neg_inf));
29 EXPECT_FP_EQ_WITH_EXCEPTION(zero, LIBC_NAMESPACE::exp(-0x1.0p20),
30 FE_UNDERFLOW);
31 EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp(0x1.0p20), FE_OVERFLOW);
32 EXPECT_FP_EQ_ALL_ROUNDING(1.0, LIBC_NAMESPACE::exp(0.0));
33 EXPECT_FP_EQ_ALL_ROUNDING(1.0, LIBC_NAMESPACE::exp(-0.0));
34}
35
36TEST_F(LlvmLibcExpTest, TrickyInputs) {
37 constexpr int N = 14;
38 constexpr uint64_t INPUTS[N] = {
39 0x3FD79289C6E6A5C0,
40 0x3FD05DE80A173EA0, // 0x1.05de80a173eap-2
41 0xbf1eb7a4cb841fcc, // -0x1.eb7a4cb841fccp-14
42 0xbf19a61fb925970d,
43 0x3fda7b764e2cf47a, // 0x1.a7b764e2cf47ap-2
44 0xc04757852a4b93aa, // -0x1.757852a4b93aap+5
45 0x4044c19e5712e377, // x=0x1.4c19e5712e377p+5
46 0xbf19a61fb925970d, // x=-0x1.9a61fb925970dp-14
47 0xc039a74cdab36c28, // x=-0x1.9a74cdab36c28p+4
48 0xc085b3e4e2e3bba9, // x=-0x1.5b3e4e2e3bba9p+9
49 0xc086960d591aec34, // x=-0x1.6960d591aec34p+9
50 0xc086232c09d58d91, // x=-0x1.6232c09d58d91p+9
51 0xc0874910d52d3051, // x=-0x1.74910d52d3051p9
52 0xc0867a172ceb0990, // x=-0x1.67a172ceb099p+9
53 };
54 for (int i = 0; i < N; ++i) {
55 double x = FPBits(INPUTS[i]).get_val();
56 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp, x,
57 LIBC_NAMESPACE::exp(x), 0.5);
58 }
59}
60
61TEST_F(LlvmLibcExpTest, InDoubleRange) {
62 constexpr uint64_t COUNT = 1'231;
63 uint64_t START = LIBC_NAMESPACE::fputil::FPBits<double>(0.25).uintval();
64 uint64_t STOP = LIBC_NAMESPACE::fputil::FPBits<double>(4.0).uintval();
65 uint64_t STEP = (STOP - START) / COUNT;
66
67 auto test = [&](mpfr::RoundingMode rounding_mode) {
68 mpfr::ForceRoundingMode __r(rounding_mode);
69 if (!__r.success)
70 return;
71
72 uint64_t fails = 0;
73 uint64_t count = 0;
74 uint64_t cc = 0;
75 double mx, mr = 0.0;
76 double tol = 0.5;
77
78 for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
79 double x = FPBits(v).get_val();
80 if (isnan(x: x) || isinf(x: x) || x < 0.0)
81 continue;
82 LIBC_NAMESPACE::libc_errno = 0;
83 double result = LIBC_NAMESPACE::exp(x);
84 ++cc;
85 if (isnan(x: result) || isinf(x: result))
86 continue;
87
88 ++count;
89 // ASSERT_MPFR_MATCH(mpfr::Operation::Log, x, result, 0.5);
90 if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Exp, x, result,
91 0.5, rounding_mode)) {
92 ++fails;
93 while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Exp, x,
94 result, tol, rounding_mode)) {
95 mx = x;
96 mr = result;
97
98 if (tol > 1000.0)
99 break;
100
101 tol *= 2.0;
102 }
103 }
104 }
105 tlog << " Exp failed: " << fails << "/" << count << "/" << cc
106 << " tests.\n";
107 tlog << " Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";
108 if (fails) {
109 EXPECT_MPFR_MATCH(mpfr::Operation::Exp, mx, mr, 0.5, rounding_mode);
110 }
111 };
112
113 tlog << " Test Rounding To Nearest...\n";
114 test(mpfr::RoundingMode::Nearest);
115
116 tlog << " Test Rounding Downward...\n";
117 test(mpfr::RoundingMode::Downward);
118
119 tlog << " Test Rounding Upward...\n";
120 test(mpfr::RoundingMode::Upward);
121
122 tlog << " Test Rounding Toward Zero...\n";
123 test(mpfr::RoundingMode::TowardZero);
124}
125

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