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
15#include <errno.h>
16#include <stdint.h>
17
18using LlvmLibcPowfTest = LIBC_NAMESPACE::testing::FPTest<float>;
19using LIBC_NAMESPACE::fputil::testing::ForceRoundingMode;
20using LIBC_NAMESPACE::fputil::testing::RoundingMode;
21
22TEST_F(LlvmLibcPowfTest, SpecialNumbers) {
23 constexpr float neg_odd_integer = -3.0f;
24 constexpr float neg_even_integer = -6.0f;
25 constexpr float neg_non_integer = -1.1f;
26 constexpr float pos_odd_integer = 5.0f;
27 constexpr float pos_even_integer = 8.0f;
28 constexpr float pos_non_integer = 1.1f;
29
30 for (int i = 0; i < N_ROUNDING_MODES; ++i) {
31 ForceRoundingMode __r(ROUNDING_MODES[i]);
32 if (!__r.success)
33 continue;
34
35 // pow( 0.0f, exponent )
36 EXPECT_FP_EQ_WITH_EXCEPTION(
37 inf, LIBC_NAMESPACE::powf(zero, neg_odd_integer), FE_DIVBYZERO);
38 EXPECT_FP_EQ_WITH_EXCEPTION(
39 inf, LIBC_NAMESPACE::powf(zero, neg_even_integer), FE_DIVBYZERO);
40 EXPECT_FP_EQ_WITH_EXCEPTION(
41 inf, LIBC_NAMESPACE::powf(zero, neg_non_integer), FE_DIVBYZERO);
42 EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf(zero, pos_odd_integer));
43 EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf(zero, pos_even_integer));
44 EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf(zero, pos_non_integer));
45 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(zero, zero));
46 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(zero, neg_zero));
47 EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::powf(zero, inf));
48 EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::powf(zero, neg_inf),
49 FE_DIVBYZERO);
50 EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf(zero, aNaN));
51
52 // pow( -0.0f, exponent )
53 EXPECT_FP_EQ_WITH_EXCEPTION(
54 neg_inf, LIBC_NAMESPACE::powf(neg_zero, neg_odd_integer), FE_DIVBYZERO);
55 EXPECT_FP_EQ_WITH_EXCEPTION(
56 inf, LIBC_NAMESPACE::powf(neg_zero, neg_even_integer), FE_DIVBYZERO);
57 EXPECT_FP_EQ_WITH_EXCEPTION(
58 inf, LIBC_NAMESPACE::powf(neg_zero, neg_non_integer), FE_DIVBYZERO);
59 EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::powf(neg_zero, pos_odd_integer));
60 EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf(neg_zero, pos_even_integer));
61 EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf(neg_zero, pos_non_integer));
62 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(neg_zero, zero));
63 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(neg_zero, neg_zero));
64 EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::powf(neg_zero, inf));
65 EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::powf(neg_zero, neg_inf),
66 FE_DIVBYZERO);
67 EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf(neg_zero, aNaN));
68
69 // pow( 1.0f, exponent )
70 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(1.0f, zero));
71 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(1.0f, neg_zero));
72 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(1.0f, 1.0f));
73 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(1.0f, -1.0f));
74 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(1.0f, neg_odd_integer));
75 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(1.0f, neg_even_integer));
76 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(1.0f, neg_non_integer));
77 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(1.0f, pos_odd_integer));
78 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(1.0f, pos_even_integer));
79 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(1.0f, pos_non_integer));
80 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(1.0f, inf));
81 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(1.0f, neg_inf));
82 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(1.0f, aNaN));
83
84 // pow( 1.0f, exponent )
85 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(-1.0f, zero));
86 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(-1.0f, neg_zero));
87 EXPECT_FP_EQ(-1.0f, LIBC_NAMESPACE::powf(-1.0f, 1.0f));
88 EXPECT_FP_EQ(-1.0f, LIBC_NAMESPACE::powf(-1.0f, -1.0f));
89 EXPECT_FP_EQ(-1.0f, LIBC_NAMESPACE::powf(-1.0f, neg_odd_integer));
90 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(-1.0f, neg_even_integer));
91 EXPECT_FP_IS_NAN_WITH_EXCEPTION(
92 LIBC_NAMESPACE::powf(-1.0f, neg_non_integer), FE_INVALID);
93 EXPECT_FP_EQ(-1.0f, LIBC_NAMESPACE::powf(-1.0f, pos_odd_integer));
94 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(-1.0f, pos_even_integer));
95 EXPECT_FP_IS_NAN_WITH_EXCEPTION(
96 LIBC_NAMESPACE::powf(-1.0f, pos_non_integer), FE_INVALID);
97 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(-1.0f, inf));
98 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(-1.0f, neg_inf));
99 EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf(-1.0f, aNaN));
100
101 // pow( inf, exponent )
102 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(inf, zero));
103 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(inf, neg_zero));
104 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf(inf, 1.0f));
105 EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf(inf, -1.0f));
106 EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf(inf, neg_odd_integer));
107 EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf(inf, neg_even_integer));
108 EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf(inf, neg_non_integer));
109 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf(inf, pos_odd_integer));
110 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf(inf, pos_even_integer));
111 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf(inf, pos_non_integer));
112 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf(inf, inf));
113 EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf(inf, neg_inf));
114 EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf(inf, aNaN));
115
116 // pow( -inf, exponent )
117 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(neg_inf, zero));
118 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(neg_inf, neg_zero));
119 EXPECT_FP_EQ(neg_inf, LIBC_NAMESPACE::powf(neg_inf, 1.0f));
120 EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::powf(neg_inf, -1.0f));
121 EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::powf(neg_inf, neg_odd_integer));
122 EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf(neg_inf, neg_even_integer));
123 EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf(neg_inf, neg_non_integer));
124 EXPECT_FP_EQ(neg_inf, LIBC_NAMESPACE::powf(neg_inf, pos_odd_integer));
125 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf(neg_inf, pos_even_integer));
126 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf(neg_inf, pos_non_integer));
127 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf(neg_inf, inf));
128 EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf(neg_inf, neg_inf));
129 EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf(neg_inf, aNaN));
130
131 // pow ( aNaN, exponent )
132 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(aNaN, zero));
133 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(aNaN, neg_zero));
134 EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf(aNaN, 1.0f));
135 EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf(aNaN, -1.0f));
136 EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf(aNaN, neg_odd_integer));
137 EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf(aNaN, neg_even_integer));
138 EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf(aNaN, neg_non_integer));
139 EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf(aNaN, pos_odd_integer));
140 EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf(aNaN, pos_even_integer));
141 EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf(aNaN, pos_non_integer));
142 EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf(aNaN, inf));
143 EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf(aNaN, neg_inf));
144 EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf(aNaN, aNaN));
145
146 // pow ( base, inf )
147 EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf(0.1f, inf));
148 EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf(-0.1f, inf));
149 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf(1.1f, inf));
150 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf(-1.1f, inf));
151
152 // pow ( base, -inf )
153 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf(0.1f, neg_inf));
154 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf(-0.1f, neg_inf));
155 EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf(1.1f, neg_inf));
156 EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf(-1.1f, neg_inf));
157
158 // Exact powers of 2:
159 EXPECT_FP_EQ(0x1.0p15f, LIBC_NAMESPACE::powf(2.0f, 15.0f));
160 EXPECT_FP_EQ(0x1.0p126f, LIBC_NAMESPACE::powf(2.0f, 126.0f));
161 EXPECT_FP_EQ(0x1.0p-45f, LIBC_NAMESPACE::powf(2.0f, -45.0f));
162 EXPECT_FP_EQ(0x1.0p-126f, LIBC_NAMESPACE::powf(2.0f, -126.0f));
163 EXPECT_FP_EQ(0x1.0p-149f, LIBC_NAMESPACE::powf(2.0f, -149.0f));
164
165 // Exact powers of 10:
166 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::powf(10.0f, 0.0f));
167 EXPECT_FP_EQ(10.0f, LIBC_NAMESPACE::powf(10.0f, 1.0f));
168 EXPECT_FP_EQ(100.0f, LIBC_NAMESPACE::powf(10.0f, 2.0f));
169 EXPECT_FP_EQ(1000.0f, LIBC_NAMESPACE::powf(10.0f, 3.0f));
170 EXPECT_FP_EQ(10000.0f, LIBC_NAMESPACE::powf(10.0f, 4.0f));
171 EXPECT_FP_EQ(100000.0f, LIBC_NAMESPACE::powf(10.0f, 5.0f));
172 EXPECT_FP_EQ(1000000.0f, LIBC_NAMESPACE::powf(10.0f, 6.0f));
173 EXPECT_FP_EQ(10000000.0f, LIBC_NAMESPACE::powf(10.0f, 7.0f));
174 EXPECT_FP_EQ(100000000.0f, LIBC_NAMESPACE::powf(10.0f, 8.0f));
175 EXPECT_FP_EQ(1000000000.0f, LIBC_NAMESPACE::powf(10.0f, 9.0f));
176 EXPECT_FP_EQ(10000000000.0f, LIBC_NAMESPACE::powf(10.0f, 10.0f));
177
178 // Overflow / Underflow:
179 if (ROUNDING_MODES[i] != RoundingMode::Downward &&
180 ROUNDING_MODES[i] != RoundingMode::TowardZero) {
181 EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::powf(3.1f, 201.0f),
182 FE_OVERFLOW);
183 }
184 if (ROUNDING_MODES[i] != RoundingMode::Upward) {
185 EXPECT_FP_EQ_WITH_EXCEPTION(0.0f, LIBC_NAMESPACE::powf(3.1f, -201.0f),
186 FE_UNDERFLOW);
187 }
188 }
189}
190

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