1 //===-- Unittests for pow -------------------------------------------------===//
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 "src/math/pow.h"
10 #include "test/UnitTest/FPMatcher.h"
11 #include "test/UnitTest/Test.h"
12 #include "utils/MPFRWrapper/MPFRUtils.h"
13
14 using LlvmLibcPowTest = LIBC_NAMESPACE::testing::FPTest<double>;
15 using LIBC_NAMESPACE::testing::tlog;
16
17 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
18
TEST_F(LlvmLibcPowTest,TrickyInputs)19 TEST_F(LlvmLibcPowTest, TrickyInputs) {
20 constexpr mpfr::BinaryInput<double> INPUTS[] = {
21 {0x1.0853408534085p-2, 0x1.0d148e03bcba8p-1},
22 {0x1.65fbd65fbd657p-1, 0x1.f10d148e03bb6p+1},
23 {0x1.c046a084d2e12p-1, 0x1.1f9p+12},
24 {0x1.ae37ed1670326p-1, 0x1.f967df66a202p-1},
25 {0x1.ffffffffffffcp-1, 0x1.fffffffffffffp-2},
26 {0x1.f558a88a8aadep-1, 0x1.88ap+12},
27 {0x1.e84d32731e593p-1, 0x1.2cb8p+13},
28 {0x1.ffffffffffffcp-1, 0x1.fffffffffffffp-2},
29 };
30
31 for (auto input : INPUTS) {
32 double x = input.x;
33 double y = input.y;
34 EXPECT_MPFR_MATCH(mpfr::Operation::Pow, input, LIBC_NAMESPACE::pow(x, y),
35 1.5);
36 }
37 }
38
TEST_F(LlvmLibcPowTest,InFloatRange)39 TEST_F(LlvmLibcPowTest, InFloatRange) {
40 constexpr uint64_t X_COUNT = 123;
41 constexpr uint64_t X_START = FPBits(0.25).uintval();
42 constexpr uint64_t X_STOP = FPBits(4.0).uintval();
43 constexpr uint64_t X_STEP = (X_STOP - X_START) / X_COUNT;
44
45 constexpr uint64_t Y_COUNT = 137;
46 constexpr uint64_t Y_START = FPBits(0.25).uintval();
47 constexpr uint64_t Y_STOP = FPBits(4.0).uintval();
48 constexpr uint64_t Y_STEP = (Y_STOP - Y_START) / Y_COUNT;
49
50 auto test = [&](mpfr::RoundingMode rounding_mode) {
51 mpfr::ForceRoundingMode __r(rounding_mode);
52 if (!__r.success)
53 return;
54
55 uint64_t fails = 0;
56 uint64_t count = 0;
57 uint64_t cc = 0;
58 double mx = 0.0, my = 0.0, mr = 0.0;
59 double tol = 1.5;
60
61 for (uint64_t i = 0, v = X_START; i <= X_COUNT; ++i, v += X_STEP) {
62 double x = FPBits(v).get_val();
63 if (FPBits(x).is_inf_or_nan() || x < 0.0)
64 continue;
65
66 for (uint64_t j = 0, w = Y_START; j <= Y_COUNT; ++j, w += Y_STEP) {
67 double y = FPBits(w).get_val();
68 if (FPBits(y).is_inf_or_nan())
69 continue;
70
71 double result = LIBC_NAMESPACE::pow(x, y);
72 ++cc;
73 if (FPBits(result).is_inf_or_nan())
74 continue;
75
76 ++count;
77 mpfr::BinaryInput<double> inputs{x, y};
78
79 if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Pow, inputs,
80 result, 1.5, rounding_mode)) {
81 ++fails;
82 while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(
83 mpfr::Operation::Pow, inputs, result, tol, rounding_mode)) {
84 mx = x;
85 my = y;
86 mr = result;
87
88 if (tol > 1000.0)
89 break;
90
91 tol *= 2.0;
92 }
93 }
94 }
95 }
96 if (fails || (count < cc)) {
97 tlog << " Pow failed: " << fails << "/" << count << "/" << cc
98 << " tests.\n"
99 << " Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";
100 }
101 if (fails) {
102 mpfr::BinaryInput<double> inputs{mx, my};
103 EXPECT_MPFR_MATCH(mpfr::Operation::Pow, inputs, mr, 1.5, rounding_mode);
104 }
105 };
106
107 tlog << " Test Rounding To Nearest...\n";
108 test(mpfr::RoundingMode::Nearest);
109
110 tlog << " Test Rounding Downward...\n";
111 test(mpfr::RoundingMode::Downward);
112
113 tlog << " Test Rounding Upward...\n";
114 test(mpfr::RoundingMode::Upward);
115
116 tlog << " Test Rounding Toward Zero...\n";
117 test(mpfr::RoundingMode::TowardZero);
118 }
119