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