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