1//===----------------------------------------------------------------------===//
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/// \file
10/// Common utilities and tables for double-precision pow(x, y).
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_POW_UTILS_H
15#define LLVM_LIBC_SRC___SUPPORT_MATH_POW_UTILS_H
16
17#include "hdr/errno_macros.h"
18#include "hdr/fenv_macros.h"
19#include "src/__support/CPP/bit.h"
20#include "src/__support/CPP/optional.h"
21#include "src/__support/FPUtil/FEnvImpl.h"
22#include "src/__support/FPUtil/FPBits.h"
23#include "src/__support/FPUtil/double_double.h"
24#include "src/__support/FPUtil/except_value_utils.h"
25#include "src/__support/FPUtil/is_integer.h"
26#include "src/__support/FPUtil/sqrt.h"
27#include "src/__support/common.h"
28#include "src/__support/macros/attributes.h"
29#include "src/__support/macros/config.h"
30#include "src/__support/macros/optimization.h"
31#include "src/__support/math/exp10.h"
32#include "src/__support/math/exp2.h"
33#include "src/__support/sign.h"
34
35namespace LIBC_NAMESPACE_DECL {
36namespace math {
37namespace pow_internal {
38
39using fputil::DoubleDouble;
40
41// 17-entry 6-bit reciprocals R_1[i] = RD_SMALL_K[i] * 2^-6 for i = 0..16,
42// used for 2-step integer range reduction in log2_f128/log2_f256 and for
43// small-table fast-path range reduction when LIBC_MATH_HAS_SMALL_TABLES is
44// enabled.
45LIBC_INLINE_VAR constexpr uint8_t RD_SMALL_K[17] = {
46 64, 60, 57, 54, 51, 49, 46, 44, 43, 41, 39, 38, 37, 35, 34, 33, 32};
47
48// Constants for log2(x) range reduction, generated by Sollya with:
49// > for i from 0 to 127 do {
50// r = 2^-8 * ceil( 2^8 * (1 - 2^(-8)) / (1 + i*2^-7) );
51// b = nearestint(log2(r) * 2^41) * 2^-41;
52// c = round(log2(r) - b, D, RN);
53// print("{ ", -c, ", ", -b, " }, ");
54// };
55// This is the same as -log2(RD[i]), with the least significant bits of the
56// high part set to be 2^-41, so that the sum of high parts + e_x is exact in
57// double precision.
58// We also replace the first and the last ones to be 0.
59LIBC_INLINE_VAR constexpr DoubleDouble LOG2_R_DD[128] = {
60 {.lo: 0.0, .hi: 0.0},
61 {.lo: -0x1.19b14945cf6bap-44, .hi: 0x1.72c7ba21p-7},
62 {.lo: -0x1.95539356f93dcp-43, .hi: 0x1.743ee862p-6},
63 {.lo: 0x1.abe0a48f83604p-43, .hi: 0x1.184b8e4c5p-5},
64 {.lo: 0x1.635577970e04p-43, .hi: 0x1.77394c9d9p-5},
65 {.lo: -0x1.401fbaaa67e3cp-45, .hi: 0x1.d6ebd1f2p-5},
66 {.lo: -0x1.5b1799ceaeb51p-43, .hi: 0x1.1bb32a6008p-4},
67 {.lo: 0x1.7c407050799bfp-43, .hi: 0x1.4c560fe688p-4},
68 {.lo: 0x1.da6339da288fcp-43, .hi: 0x1.7d60496cf8p-4},
69 {.lo: 0x1.be4f6f22dbbadp-43, .hi: 0x1.960caf9ab8p-4},
70 {.lo: -0x1.c760bc9b188c4p-45, .hi: 0x1.c7b528b71p-4},
71 {.lo: 0x1.164e932b2d51cp-44, .hi: 0x1.f9c95dc1dp-4},
72 {.lo: 0x1.924ae921f7ecap-45, .hi: 0x1.097e38ce6p-3},
73 {.lo: -0x1.6d25a5b8a19b2p-44, .hi: 0x1.22dadc2ab4p-3},
74 {.lo: 0x1.e50a1644ac794p-43, .hi: 0x1.3c6fb650ccp-3},
75 {.lo: 0x1.f34baa74a7942p-43, .hi: 0x1.494f863b8cp-3},
76 {.lo: -0x1.8f7aac147fdc1p-46, .hi: 0x1.633a8bf438p-3},
77 {.lo: 0x1.f84be19cb9578p-43, .hi: 0x1.7046031c78p-3},
78 {.lo: -0x1.66cccab240e9p-46, .hi: 0x1.8a8980abfcp-3},
79 {.lo: -0x1.3f7a55cd2af4cp-47, .hi: 0x1.97c1cb13c8p-3},
80 {.lo: 0x1.3458cde69308cp-43, .hi: 0x1.b2602497d4p-3},
81 {.lo: -0x1.667f21fa8423fp-44, .hi: 0x1.bfc67a8p-3},
82 {.lo: 0x1.d2fe4574e09b9p-47, .hi: 0x1.dac22d3e44p-3},
83 {.lo: 0x1.367bde40c5e6dp-43, .hi: 0x1.e857d3d36p-3},
84 {.lo: 0x1.d45da26510033p-46, .hi: 0x1.01d9bbcfa6p-2},
85 {.lo: -0x1.7204f55bbf90dp-44, .hi: 0x1.08bce0d96p-2},
86 {.lo: -0x1.d4f1b95e0ff45p-43, .hi: 0x1.169c05364p-2},
87 {.lo: 0x1.c20d74c0211bfp-44, .hi: 0x1.1d982c9d52p-2},
88 {.lo: 0x1.ad89a083e072ap-43, .hi: 0x1.249cd2b13cp-2},
89 {.lo: 0x1.cd0cb4492f1bcp-43, .hi: 0x1.32bfee370ep-2},
90 {.lo: -0x1.2101a9685c779p-47, .hi: 0x1.39de8e155ap-2},
91 {.lo: 0x1.9451cd394fe8dp-43, .hi: 0x1.4106017c3ep-2},
92 {.lo: 0x1.661e393a16b95p-44, .hi: 0x1.4f6fbb2cecp-2},
93 {.lo: -0x1.c6d8d86531d56p-44, .hi: 0x1.56b22e6b58p-2},
94 {.lo: 0x1.c1c885adb21d3p-43, .hi: 0x1.5dfdcf1eeap-2},
95 {.lo: 0x1.3bb5921006679p-45, .hi: 0x1.6552b49986p-2},
96 {.lo: 0x1.1d406db502403p-43, .hi: 0x1.6cb0f6865cp-2},
97 {.lo: 0x1.55a63e278bad5p-43, .hi: 0x1.7b89f02cf2p-2},
98 {.lo: -0x1.66ae2a7ada553p-49, .hi: 0x1.8304d90c12p-2},
99 {.lo: -0x1.66cccab240e9p-45, .hi: 0x1.8a8980abfcp-2},
100 {.lo: -0x1.62404772a151dp-45, .hi: 0x1.921800924ep-2},
101 {.lo: 0x1.ac9bca36fd02ep-44, .hi: 0x1.99b072a96cp-2},
102 {.lo: 0x1.4bc302ffa76fbp-43, .hi: 0x1.a8ff97181p-2},
103 {.lo: 0x1.01fea1ec47c71p-43, .hi: 0x1.b0b67f4f46p-2},
104 {.lo: -0x1.f20203b3186a6p-43, .hi: 0x1.b877c57b1cp-2},
105 {.lo: -0x1.2642415d47384p-45, .hi: 0x1.c043859e3p-2},
106 {.lo: -0x1.bc76a2753b99bp-50, .hi: 0x1.c819dc2d46p-2},
107 {.lo: -0x1.da93ae3a5f451p-43, .hi: 0x1.cffae611aep-2},
108 {.lo: -0x1.50e785694a8c6p-43, .hi: 0x1.d7e6c0abc4p-2},
109 {.lo: 0x1.c56138c894641p-43, .hi: 0x1.dfdd89d586p-2},
110 {.lo: 0x1.5669df6a2b592p-43, .hi: 0x1.e7df5fe538p-2},
111 {.lo: -0x1.ea92d9e0e8ac2p-48, .hi: 0x1.efec61b012p-2},
112 {.lo: 0x1.a0331af2e6feap-43, .hi: 0x1.f804ae8d0cp-2},
113 {.lo: 0x1.9518ce032f41dp-48, .hi: 0x1.0014332bep-1},
114 {.lo: -0x1.b3b3864c60011p-44, .hi: 0x1.042bd4b9a8p-1},
115 {.lo: -0x1.103e8f00d41c8p-45, .hi: 0x1.08494c66b9p-1},
116 {.lo: 0x1.65be75cc3da17p-43, .hi: 0x1.0c6caaf0c5p-1},
117 {.lo: 0x1.3676289cd3dd4p-43, .hi: 0x1.1096015deep-1},
118 {.lo: -0x1.41dfc7d7c3321p-43, .hi: 0x1.14c560fe69p-1},
119 {.lo: 0x1.e0cda8bd74461p-44, .hi: 0x1.18fadb6e2dp-1},
120 {.lo: 0x1.2a606046ad444p-44, .hi: 0x1.1d368296b5p-1},
121 {.lo: 0x1.f9ea977a639cp-43, .hi: 0x1.217868b0c3p-1},
122 {.lo: -0x1.50520a377c7ecp-45, .hi: 0x1.25c0a0463cp-1},
123 {.lo: 0x1.6e3cb71b554e7p-47, .hi: 0x1.2a0f3c3407p-1},
124 {.lo: -0x1.4275f1035e5e8p-48, .hi: 0x1.2e644fac05p-1},
125 {.lo: -0x1.4275f1035e5e8p-48, .hi: 0x1.2e644fac05p-1},
126 {.lo: -0x1.979a5db68721dp-45, .hi: 0x1.32bfee370fp-1},
127 {.lo: 0x1.1ee969a95f529p-43, .hi: 0x1.37222bb707p-1},
128 {.lo: 0x1.bb4b69336b66ep-43, .hi: 0x1.3b8b1c68fap-1},
129 {.lo: 0x1.d5e6a8a4fb059p-45, .hi: 0x1.3ffad4e74fp-1},
130 {.lo: 0x1.3106e404cabb7p-44, .hi: 0x1.44716a2c08p-1},
131 {.lo: 0x1.3106e404cabb7p-44, .hi: 0x1.44716a2c08p-1},
132 {.lo: -0x1.9bcaf1aa4168ap-43, .hi: 0x1.48eef19318p-1},
133 {.lo: 0x1.1646b761c48dep-44, .hi: 0x1.4d7380dcc4p-1},
134 {.lo: 0x1.2f0c0bfe9dbecp-43, .hi: 0x1.51ff2e3021p-1},
135 {.lo: 0x1.29904613e33cp-43, .hi: 0x1.5692101d9bp-1},
136 {.lo: 0x1.1d406db502403p-44, .hi: 0x1.5b2c3da197p-1},
137 {.lo: 0x1.1d406db502403p-44, .hi: 0x1.5b2c3da197p-1},
138 {.lo: -0x1.125d6cbcd1095p-44, .hi: 0x1.5fcdce2728p-1},
139 {.lo: -0x1.bd9b32266d92cp-43, .hi: 0x1.6476d98adap-1},
140 {.lo: 0x1.54243b21709cep-44, .hi: 0x1.6927781d93p-1},
141 {.lo: 0x1.54243b21709cep-44, .hi: 0x1.6927781d93p-1},
142 {.lo: -0x1.ce60916e52e91p-44, .hi: 0x1.6ddfc2a79p-1},
143 {.lo: 0x1.f1f5ae718f241p-43, .hi: 0x1.729fd26b7p-1},
144 {.lo: -0x1.6eb9612e0b4f3p-43, .hi: 0x1.7767c12968p-1},
145 {.lo: -0x1.6eb9612e0b4f3p-43, .hi: 0x1.7767c12968p-1},
146 {.lo: 0x1.fed21f9cb2cc5p-43, .hi: 0x1.7c37a9227ep-1},
147 {.lo: 0x1.7f5dc57266758p-43, .hi: 0x1.810fa51bf6p-1},
148 {.lo: 0x1.7f5dc57266758p-43, .hi: 0x1.810fa51bf6p-1},
149 {.lo: 0x1.5b338360c2ae2p-43, .hi: 0x1.85efd062c6p-1},
150 {.lo: -0x1.96fc8f4b56502p-43, .hi: 0x1.8ad846cf37p-1},
151 {.lo: -0x1.96fc8f4b56502p-43, .hi: 0x1.8ad846cf37p-1},
152 {.lo: -0x1.bdc81c4db3134p-44, .hi: 0x1.8fc924c89bp-1},
153 {.lo: 0x1.36c101ee1344p-43, .hi: 0x1.94c287492cp-1},
154 {.lo: 0x1.36c101ee1344p-43, .hi: 0x1.94c287492cp-1},
155 {.lo: 0x1.e41fa0a62e6aep-44, .hi: 0x1.99c48be206p-1},
156 {.lo: -0x1.d97ee9124773bp-46, .hi: 0x1.9ecf50bf44p-1},
157 {.lo: -0x1.d97ee9124773bp-46, .hi: 0x1.9ecf50bf44p-1},
158 {.lo: -0x1.3f94e00e7d6bcp-46, .hi: 0x1.a3e2f4ac44p-1},
159 {.lo: -0x1.6879fa00b120ap-43, .hi: 0x1.a8ff971811p-1},
160 {.lo: -0x1.6879fa00b120ap-43, .hi: 0x1.a8ff971811p-1},
161 {.lo: 0x1.1659d8e2d7d38p-44, .hi: 0x1.ae255819fp-1},
162 {.lo: 0x1.1e5e0ae0d3f8ap-43, .hi: 0x1.b35458761dp-1},
163 {.lo: 0x1.1e5e0ae0d3f8ap-43, .hi: 0x1.b35458761dp-1},
164 {.lo: 0x1.484a15babcf88p-43, .hi: 0x1.b88cb9a2abp-1},
165 {.lo: 0x1.484a15babcf88p-43, .hi: 0x1.b88cb9a2abp-1},
166 {.lo: 0x1.871a7610e40bdp-45, .hi: 0x1.bdce9dcc96p-1},
167 {.lo: -0x1.2d90e5edaeceep-43, .hi: 0x1.c31a27dd01p-1},
168 {.lo: -0x1.2d90e5edaeceep-43, .hi: 0x1.c31a27dd01p-1},
169 {.lo: -0x1.5dd31d962d373p-43, .hi: 0x1.c86f7b7ea5p-1},
170 {.lo: -0x1.5dd31d962d373p-43, .hi: 0x1.c86f7b7ea5p-1},
171 {.lo: -0x1.9ad57391924a7p-43, .hi: 0x1.cdcebd2374p-1},
172 {.lo: -0x1.3167ccc538261p-44, .hi: 0x1.d338120a6ep-1},
173 {.lo: -0x1.3167ccc538261p-44, .hi: 0x1.d338120a6ep-1},
174 {.lo: 0x1.c7a4ff65ddbc9p-45, .hi: 0x1.d8aba045bp-1},
175 {.lo: 0x1.c7a4ff65ddbc9p-45, .hi: 0x1.d8aba045bp-1},
176 {.lo: -0x1.f9ab3cf74babap-44, .hi: 0x1.de298ec0bbp-1},
177 {.lo: -0x1.f9ab3cf74babap-44, .hi: 0x1.de298ec0bbp-1},
178 {.lo: 0x1.52842c1c1e586p-43, .hi: 0x1.e3b20546f5p-1},
179 {.lo: 0x1.52842c1c1e586p-43, .hi: 0x1.e3b20546f5p-1},
180 {.lo: 0x1.3c6764fc87b4ap-48, .hi: 0x1.e9452c8a71p-1},
181 {.lo: 0x1.3c6764fc87b4ap-48, .hi: 0x1.e9452c8a71p-1},
182 {.lo: -0x1.a0976c0a2827dp-44, .hi: 0x1.eee32e2aedp-1},
183 {.lo: -0x1.a0976c0a2827dp-44, .hi: 0x1.eee32e2aedp-1},
184 {.lo: -0x1.a45314dc4fc42p-43, .hi: 0x1.f48c34bd1fp-1},
185 {.lo: -0x1.a45314dc4fc42p-43, .hi: 0x1.f48c34bd1fp-1},
186 {.lo: 0x1.ef5d00e390ap-44, .hi: 0x1.fa406bd244p-1},
187 {.lo: 0.0, .hi: 1.0},
188};
189
190// Rounding tests for 2^hi * (mid + lo) when the output might be denormal. We
191// assume further that
192// 1 <= mid < 2,
193// mid + lo < 2,
194// and |lo| << mid.
195//
196// Notice that, if 0 < x < 2^-1022:
197// double( 2^-1022 + x ) - 2^-1022 = double(x),
198// and if -2^-1022 < x < 0:
199// double(-2^-1022 + x ) - -2^-1022 = double(x).
200//
201// So if we scale |x| up by 2^1022, we can use the "+1 trick" (or "-1 trick"
202// when x < 0):
203// double( 1.0 + 2^1022 * x) - 1.0, for x > 0, and
204// double(-1.0 + 2^1022 * x) - -1.0, for x < 0,
205// to test how x is rounded in the denormal range. By adding +-1.0, the
206// results will have similar rounding points as denormal outputs, aligning the
207// least significant bit of the mantissa with 2^-52 * 2^-1022 = 2^-1074.
208//
209// Finally, subtracting 1023 in the exponent field converts the rounded normal
210// value into the subnormal value.
211template <bool SKIP_ZIV_TEST = false>
212LIBC_INLINE constexpr cpp::optional<double>
213ziv_test_denorm(int hi, double mid, double lo, double err, bool is_neg) {
214 using FPBits = typename fputil::FPBits<double>;
215
216 uint64_t scale_u = static_cast<uint64_t>(hi + 2045) << FPBits::FRACTION_LEN;
217 double exp_scale = cpp::bit_cast<double>(from: scale_u);
218
219 double mid_hi = mid * exp_scale;
220 double lo_scaled = lo * exp_scale;
221
222 double extra_factor = 0.0;
223 uint64_t scale_down = 0x3FE0'0000'0000'0000ULL; // 1022 in the exponent field.
224
225 // Result is denormal if |mid_hi + lo_scaled| < 1.0.
226 // In that case, add +-1.0 to mimic denormal rounding points.
227 if (is_neg) {
228 if ((-1.0 - mid_hi) < lo_scaled) {
229 // Extra rounding step is needed, which adds more rounding errors.
230 extra_factor = -1.0;
231 scale_down = 0x3FF0'0000'0000'0000ULL; // 1023 in the exponent field.
232 err += 0x1.0p-52;
233 }
234 } else {
235 if ((1.0 - mid_hi) > lo_scaled) {
236 // Extra rounding step is needed, which adds more rounding errors.
237 extra_factor = 1.0;
238 scale_down = 0x3FF0'0000'0000'0000ULL; // 1023 in the exponent field.
239 err += 0x1.0p-52;
240 }
241 }
242
243 // By adding +-1.0, the hardware adder performs rounding at the exact same
244 // bit position as denormal rounding.
245 if constexpr (SKIP_ZIV_TEST) {
246 double r = extra_factor + (mid_hi + lo_scaled);
247 return cpp::bit_cast<double>(from: cpp::bit_cast<uint64_t>(from: r) - scale_down);
248 } else {
249 double err_scaled = err * exp_scale;
250
251 double lo_u = lo_scaled + err_scaled;
252 double lo_l = lo_scaled - err_scaled;
253
254 double upper = extra_factor + (mid_hi + lo_u);
255 double lower = extra_factor + (mid_hi + lo_l);
256
257 if (LIBC_LIKELY(upper == lower)) {
258 return cpp::bit_cast<double>(from: cpp::bit_cast<uint64_t>(from: upper) - scale_down);
259 }
260
261 return cpp::nullopt;
262 }
263}
264
265// Upper bound for y = 1075 / |log2(1 - 2^(-53)|, generated by Sollya:
266// > y = round(-1075 / log2(1 - 2^(-53)), D, RU);
267// > y;
268// 0x1.74910d52d3052p62
269// > printdouble(y);
270// 0x43d74910d52d3052
271constexpr uint64_t Y_UPPER_BOUND = 0x43d7'4910'd52d'3052;
272// Lower bound for y = 2^(-54) / 1074), generated by Sollya:
273// > y = round(2^(-54) / 1074), D, RD);
274// > y;
275// 0x1.e829f39aef509p-65
276// > printdouble(y);
277// 0x3bee829f39ae'f509
278constexpr uint64_t Y_LOWER_BOUND = 0x3bee'829f'39ae'f509;
279
280// Fast checks for special inputs:
281// x = 0, +-1, 2^k, 10, +- inf
282// y = 0, +-1, 2, 0.5
283LIBC_ALWAYS_INLINE cpp::optional<double> check_special_inputs(double x,
284 double y) {
285 using FPBits = fputil::FPBits<double>;
286 FPBits xbits(x), ybits(y);
287
288 bool x_sign = xbits.sign() == Sign::NEG;
289 bool y_sign = ybits.sign() == Sign::NEG;
290
291 FPBits x_abs = xbits.abs();
292 FPBits y_abs = ybits.abs();
293
294 uint64_t x_u = xbits.uintval();
295 uint64_t y_u = ybits.uintval();
296 uint64_t y_a = y_abs.uintval();
297
298 if (LIBC_UNLIKELY((x_u & 0x0003'FFFF'FFFF'FFFF) == 0) ||
299 ((y_u & 0x000F'FFFF'FFFF'FFFF) == 0)) {
300 // If x or y is signaling NaN
301 if (x_abs.is_signaling_nan() || y_abs.is_signaling_nan()) {
302 fputil::raise_except_if_required(FE_INVALID);
303 return FPBits::quiet_nan().get_val();
304 }
305
306 if (x == 1.0 || y == 0.0)
307 return 1.0;
308
309 if (x == 0.0) {
310 if (y_abs.is_nan())
311 return y;
312 if (y_abs.is_inf())
313 return y_sign ? FPBits::inf().get_val() : 0.0;
314 bool out_is_neg = x_sign && fputil::is_odd_integer(x: y);
315 if (y_sign) {
316 // pow(0, negative number) = inf
317 fputil::set_errno_if_required(EDOM);
318 fputil::raise_except_if_required(FE_DIVBYZERO);
319 return FPBits::inf(sign: out_is_neg ? Sign::NEG : Sign::POS).get_val();
320 }
321 // pow(0, positive number) = 0
322 return out_is_neg ? -0.0 : 0.0;
323 }
324
325 if (y == 1.0)
326 return x;
327
328 if (y == 2.0)
329 return x * x;
330
331 if (y == 0.5 && !x_sign)
332 return fputil::sqrt<double>(x);
333
334 // TODO: Add special case y = -0.5 when rsqrt is available.
335
336 if (x == 2.0)
337 return math::exp2(x: y);
338
339 if (x == 10.0)
340 return math::exp10(x: y);
341
342 // For x = 2^(+- 2^n):
343 // x^y = 2^(+- 2^n * y).
344 if (!x_sign && x_abs.is_normal() && xbits.get_mantissa() == 0 &&
345 y_a > Y_LOWER_BOUND && y_a < Y_UPPER_BOUND) {
346 int e_x = xbits.get_exponent();
347 uint32_t abs_e = static_cast<uint32_t>(e_x > 0 ? e_x : -e_x);
348 if (cpp::has_single_bit(value: abs_e)) {
349 double hi = static_cast<double>(e_x) * y;
350 if (hi >= 1024.0)
351 return fputil::set_overflow<double>();
352 if (hi < -1075.0)
353 return fputil::set_underflow<double>();
354 if (hi >= -1022.0)
355 return math::exp2(x: hi);
356 }
357 }
358 }
359
360 return cpp::nullopt;
361}
362
363// Filters out extreme input ranges, infinities, NaNs, normalizes denormal
364// inputs, and handles negative bases. Returns a value if the result is
365// determined, or nullopt if regular evaluation should proceed (in which case x,
366// y, e_x, x_mant, is_neg, sign_d may be updated).
367LIBC_ALWAYS_INLINE cpp::optional<double>
368check_exceptional_cases(double &x, double &y, double &e_x, uint64_t &x_mant,
369 bool &is_neg, double &sign_d) {
370 using FPBits = fputil::FPBits<double>;
371
372 FPBits xbits(x), ybits(y);
373 bool x_sign = xbits.sign() == Sign::NEG;
374 bool y_sign = ybits.sign() == Sign::NEG;
375
376 FPBits x_abs = xbits.abs();
377 FPBits y_abs = ybits.abs();
378
379 uint64_t y_mant = ybits.get_mantissa();
380 uint64_t x_a = x_abs.uintval();
381 uint64_t y_a = y_abs.uintval();
382
383 // If x or y is signaling NaN
384 if (x_abs.is_signaling_nan() || y_abs.is_signaling_nan()) {
385 fputil::raise_except_if_required(FE_INVALID);
386 return FPBits::quiet_nan().get_val();
387 }
388
389#ifdef LIBC_MATH_HAS_SMALL_TABLES
390 if (x == 1.0 || y == 0.0)
391 return 1.0;
392#endif // LIBC_MATH_HAS_SMALL_TABLES
393
394 // |y| > |1075 / log2(1 - 2^-53)|.
395 if (y_a > Y_UPPER_BOUND) {
396 if (y_a >= FPBits::inf().uintval()) {
397 // y is inf or nan
398 if (y_mant != 0) {
399 // y is NaN
400 // pow(1, NaN) = 1 is already dealt with.
401 // pow(x, NaN) = NaN
402 return y;
403 }
404
405 // Now y is +-Inf
406 if (x_abs.is_nan()) {
407 // pow(NaN, +-Inf) = NaN
408 return x;
409 }
410
411 if (x_a == FPBits::one().uintval()) {
412 // pow(+-1, +-Inf) = 1.0
413 return 1.0;
414 }
415
416 // pow (|x| < 1, -inf) = +inf
417 // pow (|x| < 1, +inf) = 0.0
418 // pow (|x| > 1, -inf) = 0.0
419 // pow (|x| > 1, +inf) = +inf
420 return ((x_a < FPBits::one().uintval()) == y_sign)
421 ? FPBits::inf().get_val()
422 : 0.0;
423 }
424 // x^y will overflow / underflow in double precision. Set y to a
425 // large enough exponent but not too large, so that the computations
426 // won't overflow in double precision.
427 y = y_sign ? -0x1.0p100 : 0x1.0p100;
428 }
429
430 // y is finite and non-zero.
431 if (x_a > FPBits::inf().uintval()) {
432 // x is NaN.
433 // pow (aNaN, 0) is already taken care above.
434 return x;
435 }
436
437 if (x_sign) {
438 if (fputil::is_integer(x: y)) {
439 x = -x;
440 if (fputil::is_odd_integer(x: y)) {
441 is_neg = true;
442 sign_d = -1.0;
443 }
444 } else if (x != 0.0 && x_a < FPBits::inf().uintval()) {
445 // pow( negative, non-integer ) = NaN
446 fputil::set_errno_if_required(EDOM);
447 fputil::raise_except_if_required(FE_INVALID);
448 return FPBits::quiet_nan().get_val();
449 }
450 }
451
452#ifdef LIBC_MATH_HAS_SMALL_TABLES
453 if (x == 0.0) {
454 if (y_sign) {
455 // pow(0, negative number) = inf
456 fputil::set_errno_if_required(EDOM);
457 fputil::raise_except_if_required(FE_DIVBYZERO);
458 return is_neg ? -FPBits::inf().get_val() : FPBits::inf().get_val();
459 }
460 // pow(0, positive number) = 0
461 return is_neg ? -0.0 : 0.0;
462 }
463#endif // LIBC_MATH_HAS_SMALL_TABLES
464
465 if (x_a == FPBits::inf().uintval()) {
466 if (y_sign)
467 return is_neg ? -0.0 : 0.0;
468 return is_neg ? -FPBits::inf().get_val() : FPBits::inf().get_val();
469 }
470
471 if (y_a <= Y_LOWER_BOUND) {
472#if defined(LIBC_MATH_HAS_NO_EXCEPT) && \
473 defined(LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY)
474 return 1.0;
475#else
476 volatile double one = 1.0;
477 volatile double eps =
478 ((x_a < FPBits::one().uintval()) == !y_sign) ? -0x1.0p-100 : 0x1.0p-100;
479 return one + eps;
480#endif // LIBC_MATH_HAS_NO_EXCEPT && LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
481 }
482
483 // Normalize denormal inputs.
484 if (x_a < FPBits::min_normal().uintval()) {
485 FPBits x_norm(x * 0x1.0p64);
486 e_x = static_cast<double>(x_norm.get_exponent()) - 64.0;
487 x_mant = x_norm.get_mantissa();
488 }
489
490 return cpp::nullopt;
491}
492
493// Check if x^y is an exact rounding boundary case (a 54-bit dyadic float,
494// which is either an exact 53-bit float or an exact halfway midpoint).
495//
496// Reference:
497// Lauter, C. and Lefevre, V., "Rounding Boundary Cases for Values of the
498// Exponential and Power Functions," IEEE Trans. Comput. 58(8):1063-1074.
499//
500// For a normalized 128-bit dyadic float with MSB at bit 127, the top 54 bits
501// occupy bits [127:74]. For any 54-bit number, bits [73:0] are zero.
502//
503// If x^y is not in F_54:
504// |x^y - o_54(x^y)| / x^y >= 2^-114.
505// With an evaluation error < 2^-115, the distance from bits [73:0] to the
506// nearest 54-bit boundary must satisfy:
507// dist >= (2^-114 - 2^-115) * 2^127 = 2^12.
508// We use a threshold of 2^-114 (dist < 2^13) to capture all boundary
509// candidates:
510//
511// 1. x = 2^e: x^y = 2^(e * y) is in F_54 iff e * y is an integer.
512// 2. x = m * 2^e, y = n * 2^f with m, n odd integers:
513// x^y in F_54 implies:
514// - 0 <= y <= 35, n <= 35, and f >= -5.
515// - e * y is an integer.
516// - When f < 0 (i.e. y = n / 2^(-f)): m^(2^f) is an integer, verified
517// by taking repeated square roots -f times.
518// - The resulting significand (m^(2^f))^n <= 2^54.
519//
520// Returns true and sets exact_m and exact_exp if |x|^y is an exact boundary,
521// where |x| = (1 + x_mant * 2^-52) * 2^x_e.
522LIBC_COLD LIBC_NOINLINE LIBC_INLINE bool
523is_exact_rounding_boundary(int x_e, uint64_t x_mant, double y,
524 uint64_t &exact_m, int &exact_exp) {
525 using FPBits = fputil::FPBits<double>;
526
527 // Case 1: |x| = 2^x_e.
528 if (x_mant == 0) {
529 double ey = static_cast<double>(x_e) * y;
530 if (fputil::is_integer(x: ey)) {
531 exact_m = 1;
532 exact_exp = static_cast<int>(ey);
533 return true;
534 }
535 return false;
536 }
537
538 // Case 2: |x| is not a power of 2 (requires y = n * 2^f with n >= 1 odd and
539 // f >= -5, so y in [2^-5, 35]).
540 if (y < 0x1.0p-5 || y > 35.0)
541 return false;
542
543 // Decompose y = n * 2^f with n odd.
544 FPBits ybits(y);
545 uint64_t y_mant = ybits.get_mantissa() | (1ULL << FPBits::FRACTION_LEN);
546 int tz_y = cpp::countr_zero(value: y_mant);
547 uint64_t n = y_mant >> tz_y;
548 int f = ybits.get_exponent() - static_cast<int>(FPBits::FRACTION_LEN) + tz_y;
549 if (n > 35 || f < -5)
550 return false;
551
552 // Decompose |x| = cur * 2^e with cur odd >= 3.
553 uint64_t full_x_mant = x_mant | (1ULL << FPBits::FRACTION_LEN);
554 int tz_x = cpp::countr_zero(value: full_x_mant);
555 uint64_t cur = full_x_mant >> tz_x;
556 int e = x_e - static_cast<int>(FPBits::FRACTION_LEN) + tz_x;
557
558 int exp_int = 0;
559 int cand_exp = 0;
560 if (f < 0) {
561 // Non-integer exponent: y = n / 2^(-f) with shift = -f in {1, ..., 5}.
562 // Since n is odd, e * y is an integer iff 2^shift divides e.
563 int shift = -f;
564 if ((static_cast<uint32_t>(e) & ((1U << shift) - 1U)) != 0U)
565 return false;
566
567 // Check if cur^(2^f) is an integer by taking repeated square roots.
568 LIBC_LOOP_NOUNROLL
569 for (int i = 0; i < shift; ++i) {
570 uint64_t s =
571 static_cast<uint64_t>(fputil::sqrt<double>(x: static_cast<double>(cur)));
572 if (s * s != cur)
573 return false;
574 cur = s;
575 }
576 exp_int = static_cast<int>(n);
577 cand_exp = (e >> shift) * exp_int;
578 } else {
579 exp_int = static_cast<int>(n) << f;
580 cand_exp = e * exp_int;
581 }
582
583 // Since cur >= 3 is odd, cur > 2^(63 - clz(cur)), so cur^exp_int > 2^54
584 // whenever (63 - clz(cur)) * exp_int >= 54. This precheck also guarantees
585 // res * cur never overflows uint64_t inside the loop below.
586 if ((63 - cpp::countl_zero(value: cur)) * exp_int >= 54)
587 return false;
588
589 uint64_t res = 1;
590 LIBC_LOOP_NOUNROLL
591 for (int i = 0; i < exp_int; ++i) {
592 res *= cur;
593 if (res > (1ULL << 54))
594 return false;
595 }
596 exact_m = res;
597 exact_exp = cand_exp;
598 return true;
599}
600
601} // namespace pow_internal
602} // namespace math
603} // namespace LIBC_NAMESPACE_DECL
604
605#endif // LLVM_LIBC_SRC___SUPPORT_MATH_POW_UTILS_H
606