1 | //===-- Unittests for 10^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/exp10.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 | |
20 | using LlvmLibcExp10Test = LIBC_NAMESPACE::testing::FPTest<double>; |
21 | |
22 | namespace mpfr = LIBC_NAMESPACE::testing::mpfr; |
23 | using LIBC_NAMESPACE::testing::tlog; |
24 | |
25 | TEST_F(LlvmLibcExp10Test, SpecialNumbers) { |
26 | EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::exp10(aNaN)); |
27 | EXPECT_FP_EQ(inf, LIBC_NAMESPACE::exp10(inf)); |
28 | EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::exp10(neg_inf)); |
29 | EXPECT_FP_EQ_WITH_EXCEPTION(zero, LIBC_NAMESPACE::exp10(-0x1.0p20), |
30 | FE_UNDERFLOW); |
31 | EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp10(0x1.0p20), |
32 | FE_OVERFLOW); |
33 | EXPECT_FP_EQ_ALL_ROUNDING(1.0, LIBC_NAMESPACE::exp10(0.0)); |
34 | EXPECT_FP_EQ_ALL_ROUNDING(1.0, LIBC_NAMESPACE::exp10(-0.0)); |
35 | } |
36 | |
37 | TEST_F(LlvmLibcExp10Test, TrickyInputs) { |
38 | constexpr int N = 41; |
39 | constexpr uint64_t INPUTS[N] = { |
40 | 0x40033093317082F8, 0x3FD79289C6E6A5C0, |
41 | 0x3FD05DE80A173EA0, // 0x1.05de80a173eap-2 |
42 | 0xbf1eb7a4cb841fcc, // -0x1.eb7a4cb841fccp-14 |
43 | 0xbf19a61fb925970d, |
44 | 0x3fda7b764e2cf47a, // 0x1.a7b764e2cf47ap-2 |
45 | 0xc04757852a4b93aa, // -0x1.757852a4b93aap+5 |
46 | 0x4044c19e5712e377, // x=0x1.4c19e5712e377p+5 |
47 | 0xbf19a61fb925970d, // x=-0x1.9a61fb925970dp-14 |
48 | 0xc039a74cdab36c28, // x=-0x1.9a74cdab36c28p+4 |
49 | 0xc085b3e4e2e3bba9, // x=-0x1.5b3e4e2e3bba9p+9 |
50 | 0xc086960d591aec34, // x=-0x1.6960d591aec34p+9 |
51 | 0xc086232c09d58d91, // x=-0x1.6232c09d58d91p+9 |
52 | 0xc0874910d52d3051, // x=-0x1.74910d52d3051p9 |
53 | 0xc0867a172ceb0990, // x=-0x1.67a172ceb099p+9 |
54 | 0xc08ff80000000000, // x=-0x1.ff8p+9 |
55 | 0xbc971547652b82fe, // x=-0x1.71547652b82fep-54 |
56 | 0x0000000000000000, // x = 0 |
57 | 0x3ff0000000000000, // x = 1 |
58 | 0x4000000000000000, // x = 2 |
59 | 0x4008000000000000, // x = 3 |
60 | 0x4010000000000000, // x = 4 |
61 | 0x4014000000000000, // x = 5 |
62 | 0x4018000000000000, // x = 6 |
63 | 0x401c000000000000, // x = 7 |
64 | 0x4020000000000000, // x = 8 |
65 | 0x4022000000000000, // x = 9 |
66 | 0x4024000000000000, // x = 10 |
67 | 0x4026000000000000, // x = 11 |
68 | 0x4028000000000000, // x = 12 |
69 | 0x402a000000000000, // x = 13 |
70 | 0x402c000000000000, // x = 14 |
71 | 0x402e000000000000, // x = 15 |
72 | 0x4030000000000000, // x = 16 |
73 | 0x4031000000000000, // x = 17 |
74 | 0x4032000000000000, // x = 18 |
75 | 0x4033000000000000, // x = 19 |
76 | 0x4034000000000000, // x = 20 |
77 | 0x4035000000000000, // x = 21 |
78 | 0x4036000000000000, // x = 22 |
79 | 0x4037000000000000, // x = 23 |
80 | }; |
81 | for (int i = 0; i < N; ++i) { |
82 | double x = FPBits(INPUTS[i]).get_val(); |
83 | EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp10, x, |
84 | LIBC_NAMESPACE::exp10(x), 0.5); |
85 | } |
86 | } |
87 | |
88 | TEST_F(LlvmLibcExp10Test, InDoubleRange) { |
89 | constexpr uint64_t COUNT = 1'231; |
90 | uint64_t START = LIBC_NAMESPACE::fputil::FPBits<double>(0.25).uintval(); |
91 | uint64_t STOP = LIBC_NAMESPACE::fputil::FPBits<double>(4.0).uintval(); |
92 | uint64_t STEP = (STOP - START) / COUNT; |
93 | |
94 | auto test = [&](mpfr::RoundingMode rounding_mode) { |
95 | mpfr::ForceRoundingMode __r(rounding_mode); |
96 | if (!__r.success) |
97 | return; |
98 | |
99 | uint64_t fails = 0; |
100 | uint64_t count = 0; |
101 | uint64_t cc = 0; |
102 | double mx, mr = 0.0; |
103 | double tol = 0.5; |
104 | |
105 | for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) { |
106 | double x = FPBits(v).get_val(); |
107 | if (isnan(x: x) || isinf(x: x) || x < 0.0) |
108 | continue; |
109 | LIBC_NAMESPACE::libc_errno = 0; |
110 | double result = LIBC_NAMESPACE::exp10(x); |
111 | ++cc; |
112 | if (isnan(x: result) || isinf(x: result)) |
113 | continue; |
114 | |
115 | ++count; |
116 | |
117 | if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Exp10, x, result, |
118 | 0.5, rounding_mode)) { |
119 | ++fails; |
120 | while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Exp10, x, |
121 | result, tol, rounding_mode)) { |
122 | mx = x; |
123 | mr = result; |
124 | |
125 | if (tol > 1000.0) |
126 | break; |
127 | |
128 | tol *= 2.0; |
129 | } |
130 | } |
131 | } |
132 | tlog << " Exp10 failed: " << fails << "/" << count << "/" << cc |
133 | << " tests.\n" ; |
134 | tlog << " Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n" ; |
135 | if (fails) { |
136 | EXPECT_MPFR_MATCH(mpfr::Operation::Exp10, mx, mr, 0.5, rounding_mode); |
137 | } |
138 | }; |
139 | |
140 | tlog << " Test Rounding To Nearest...\n" ; |
141 | test(mpfr::RoundingMode::Nearest); |
142 | |
143 | tlog << " Test Rounding Downward...\n" ; |
144 | test(mpfr::RoundingMode::Downward); |
145 | |
146 | tlog << " Test Rounding Upward...\n" ; |
147 | test(mpfr::RoundingMode::Upward); |
148 | |
149 | tlog << " Test Rounding Toward Zero...\n" ; |
150 | test(mpfr::RoundingMode::TowardZero); |
151 | } |
152 | |