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

source code of libc/test/src/math/powf_test.cpp