1 //===-- Unittests for powf ------------------------------------------------===//
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/math/powf.h"
12 #include "test/UnitTest/FPMatcher.h"
13 #include "test/UnitTest/Test.h"
14 #include "utils/MPFRWrapper/MPFRUtils.h"
15
16 #include <errno.h>
17 #include <stdint.h>
18
19 using LlvmLibcPowfTest = LIBC_NAMESPACE::testing::FPTest<float>;
20 using LIBC_NAMESPACE::testing::tlog;
21
22 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
23
TEST_F(LlvmLibcPowfTest,TrickyInputs)24 TEST_F(LlvmLibcPowfTest, TrickyInputs) {
25 constexpr int N = 13;
26 constexpr mpfr::BinaryInput<float> INPUTS[N] = {
27 {0x1.290bbp-124f, 0x1.1e6d92p-25f},
28 {0x1.2e9fb6p+5f, -0x1.1b82b6p-18f},
29 {0x1.6877f6p+60f, -0x1.75f1c6p-4f},
30 {0x1.0936acp-63f, -0x1.55200ep-15f},
31 {0x1.d6d72ap+43f, -0x1.749ccap-5f},
32 {0x1.4afb2ap-40f, 0x1.063198p+0f},
33 {0x1.0124dep+0f, -0x1.fdb016p+9f},
34 {0x1.1058p+0f, 0x1.ap+64f},
35 {0x1.1058p+0f, -0x1.ap+64f},
36 {0x1.1058p+0f, 0x1.ap+64f},
37 {0x1.fa32d4p-1f, 0x1.67a62ep+12f},
38 {-0x1.8p-49, 0x1.8p+1},
39 {0x1.8p-48, 0x1.8p+1},
40 };
41
42 for (int i = 0; i < N; ++i) {
43 float x = INPUTS[i].x;
44 float y = INPUTS[i].y;
45 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Pow, INPUTS[i],
46 LIBC_NAMESPACE::powf(x, y), 0.5);
47 }
48 }
49
TEST_F(LlvmLibcPowfTest,InFloatRange)50 TEST_F(LlvmLibcPowfTest, InFloatRange) {
51 constexpr uint32_t X_COUNT = 1'23;
52 constexpr uint32_t X_START = FPBits(0.25f).uintval();
53 constexpr uint32_t X_STOP = FPBits(4.0f).uintval();
54 constexpr uint32_t X_STEP = (X_STOP - X_START) / X_COUNT;
55
56 constexpr uint32_t Y_COUNT = 1'37;
57 constexpr uint32_t Y_START = FPBits(0.25f).uintval();
58 constexpr uint32_t Y_STOP = FPBits(4.0f).uintval();
59 constexpr uint32_t Y_STEP = (Y_STOP - Y_START) / Y_COUNT;
60
61 auto test = [&](mpfr::RoundingMode rounding_mode) {
62 mpfr::ForceRoundingMode __r(rounding_mode);
63 if (!__r.success)
64 return;
65
66 uint64_t fails = 0;
67 uint64_t count = 0;
68 uint64_t cc = 0;
69 float mx, my, mr = 0.0;
70 double tol = 0.5;
71
72 for (uint32_t i = 0, v = X_START; i <= X_COUNT; ++i, v += X_STEP) {
73 float x = FPBits(v).get_val();
74 if (isnan(x) || isinf(x) || x < 0.0)
75 continue;
76
77 for (uint32_t j = 0, w = Y_START; j <= Y_COUNT; ++j, w += Y_STEP) {
78 float y = FPBits(w).get_val();
79 if (isnan(y) || isinf(y))
80 continue;
81
82 LIBC_NAMESPACE::libc_errno = 0;
83 float result = LIBC_NAMESPACE::powf(x, y);
84 ++cc;
85 if (isnan(result) || isinf(result))
86 continue;
87
88 ++count;
89 mpfr::BinaryInput<float> inputs{x, y};
90
91 if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Pow, inputs,
92 result, 0.5, rounding_mode)) {
93 ++fails;
94 while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(
95 mpfr::Operation::Pow, inputs, result, tol, rounding_mode)) {
96 mx = x;
97 my = y;
98 mr = result;
99
100 if (tol > 1000.0)
101 break;
102
103 tol *= 2.0;
104 }
105 }
106 }
107 }
108 if (fails || (count < cc)) {
109 tlog << " Powf failed: " << fails << "/" << count << "/" << cc
110 << " tests.\n"
111 << " Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";
112 }
113 if (fails) {
114 mpfr::BinaryInput<float> inputs{mx, my};
115 EXPECT_MPFR_MATCH(mpfr::Operation::Pow, inputs, mr, 0.5, rounding_mode);
116 }
117 };
118
119 tlog << " Test Rounding To Nearest...\n";
120 test(mpfr::RoundingMode::Nearest);
121
122 tlog << " Test Rounding Downward...\n";
123 test(mpfr::RoundingMode::Downward);
124
125 tlog << " Test Rounding Upward...\n";
126 test(mpfr::RoundingMode::Upward);
127
128 tlog << " Test Rounding Toward Zero...\n";
129 test(mpfr::RoundingMode::TowardZero);
130 }
131