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/// Implementation header for double-precision pow(x, y).
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_POW_H
15#define LLVM_LIBC_SRC___SUPPORT_MATH_POW_H
16
17#include "hdr/errno_macros.h"
18#include "src/__support/FPUtil/FEnvImpl.h"
19#include "src/__support/FPUtil/FPBits.h"
20#include "src/__support/FPUtil/double_double.h"
21#include "src/__support/FPUtil/except_value_utils.h"
22#include "src/__support/FPUtil/is_integer.h"
23#include "src/__support/FPUtil/multiply_add.h"
24#include "src/__support/FPUtil/nearest_integer.h"
25#include "src/__support/common.h"
26#include "src/__support/macros/attributes.h"
27#include "src/__support/macros/config.h"
28#include "src/__support/macros/optimization.h"
29#include "src/__support/macros/properties/cpu_features.h"
30#include "src/__support/math/common_constants.h"
31#include "src/__support/math/exp_constants.h"
32#include "src/__support/math/pow_utils.h"
33#include "src/__support/sign.h"
34
35#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
36#include "src/__support/math/pow_fast.h"
37#else
38
39#include "src/__support/math/pow_accurate_128.h"
40
41namespace LIBC_NAMESPACE_DECL {
42
43namespace math {
44
45// Overview of the main part of pow(x, y) = x^y computations.
46//
47// Let x = 2^(e_x) * m_x > 0. Then:
48// x^y = 2^( y * log2(x) )
49// = 2^( y * ( e_x + log2(m_x) ) )
50// = 2^( e_h + e_l )
51// = 2^(e_h) * 2^(e_l)
52// where:
53// e_h = round(y * log2(x)),
54// e_l = {y * log2(x)} = y * log2(x) - e_h.
55//
56// In particular, e_h is an integer, and |e_l| <= 0.5.
57//
58// For the final result to be fit in double precision, the exponent field can be
59// bounded by:
60// -1075 <= e_h <= 1024,
61// and anything outside that can be passed to quick overflow/underflow logic.
62//
63// Since |e_l| <= 0.5:
64// 0.5 < 2^(e_l) < 2,
65// the relative error of x^y = 2^(e_h) * 2^(e_l) is about
66// ~ absolute error of e_l
67// ~ absolute error of y * log2(x)
68// ~ relative error(log2(x)) * |y|
69// ~ relative error(log2(x)) * |e_h|
70// < relative error(log2(x)) * 2^11.
71//
72// Roughly speaking, to compute x^y with relative error < 2^(-n), we will need
73// to compute log2(x) with relative error < 2^(-n - 11), approximately.
74//
75// To compute log2(x), we will perform range reduction for log2(m_x):
76// dx = r * m_x - 1.
77// Then m_x = (1 + dx) / r, and
78// log2(m_x) = log2( (1 + dx) / r )
79// = log2(1 + dx) - log2(r),
80// where -log2(r) is obtained from look up tables.
81// The computation of dx = r * m_x - 1 is exact, and we choose the size of the
82// look up table for r's such that:
83// -2^-8 <= dx < 2^-7.
84// Combining them together, we have that:
85// log2(x) = e_x - log2(r) + log2(1 + dx)
86// = e_x - log2(r) + log2(e) * (dx - dx^2/2 + dx^3/3 - ...)
87// So in the worst case, where e_x = log2(r) = 0, the relative error of log2(x)
88// computation will be:
89// ~ absolute_error(log2(1 + dx)) / |dx|.
90// So if we compute log2(1 + dx) accurately up to dx^n term, and use a
91// polynomial approximation for the dx^(n + 1) and higher terms:
92// log2(1 + dx) ~ log2(e) * (dx - dx^2/2 + ... + (-1)^(n + 1) dx^n / n) +
93// dx^(n + 1) * P(dx)
94// such that the absolute error of P(dx) is smaller than double precision ulp,
95// then the overall relative error of our log2(1 + dx) approximation is:
96// ~ ulp(dx^(n + 1)) / |dx| ~ 2^(-52) * |dx^n|
97//
98// Let's consider 3 cases:
99// - For a fast path of a correctly rounded implementation, we will want the
100// relative error of x^y bounded above by:
101// ~ 2^(-precision - 10) = 2^(-53 - 10) = 2^(-63)
102// so that the Ziv accuracy test passes with probability > 1 - 2^-10 ~ 99.99%.
103// And from the above argument, we need to compute log2(1 + dx) with relative
104// error ~ 2^(-63 - 11) = 2^(-74).
105// So we will need to compute log2(1 + dx) accurately up to dx^n such that:
106// 2^(-52) * |dx^n| ~ 2^(-74).
107// Or equivalently:
108// |dx^n| < 2^(-22).
109// With our bounds from range reduction |dx| < 2^(-7), n = 3 will be quite
110// close to our desired precision needed.
111//
112// In summary, for a fast path of correctly rounded implementation, we will
113// compute log2(e) * (dx - dx^2/2 + dx^3/3) accurately, and approximate higher
114// terms with dx^4 * P(dx), where:
115// |(log2(1 + dx) - log2(e) * (dx - dx^2/2 + dx^3/3))/dx^4 - P(dx)| < 2^-53.
116//
117// - For a strictly < 1 ULP error fast version across the full exponent range
118// (as implemented in pow_fast), we will want the relative error of x^y
119// bounded above by:
120// ~ 2^(-precision) = 2^(-53).
121// So the relative error needed to approximate log2(1 + dx) is:
122// ~ 2^(-53 - 11) = 2^(-64),
123// and we will need to compute log2(1 + dx) accurately up to dx^n such that:
124// 2^(-52) * |dx^n| ~ 2^(-64),
125// or equivalently:
126// |dx^n| < 2^-12.
127// With |dx| < 2^(-7), n = 2 satisfies |dx^2| < 2^(-14) < 2^(-12).
128// Hence, we compute log2(e) * (dx - dx^2/2) accurately in DoubleDouble, and
129// approximate higher terms with dx^3 * P(dx), where:
130// |(log2(1 + dx) - log2(e) * (dx - dx^2/2))/dx^3 - P(dx)| < 2^-53.
131// This guarantees error < 1 ULP everywhere (empirically bounded by 0.75 ULP).
132//
133// - If we were to choose n = 1 (computing only log2(e) * dx accurately, and
134// approximating higher terms with dx^2 * P(dx)), the relative error of
135// log2(1 + dx) is only bounded by:
136// ~ 2^(-52) * |dx| < 2^(-52) * 2^(-7) = 2^(-59).
137// The resulting relative error of x^y is about:
138// ~ 2^(-59) * |e_h|.
139// While this achieves < 1 ULP for typical inputs with |e_h| <= 64, near
140// extreme exponent boundaries (|e_h| ~ 2^10 and |dx| ~ 2^-7) the error can
141// drift up to ~7.8 ULPs (bounded theoretically by ~16 ULPs).
142
143LIBC_INLINE double pow(double x, double y) {
144 using namespace common_constants_internal;
145 using namespace pow_internal;
146 using FPBits = fputil::FPBits<double>;
147
148 FPBits xbits(x), ybits(y);
149 uint64_t x_u = xbits.uintval();
150 uint64_t y_u = ybits.uintval();
151 uint64_t y_a = ybits.abs().uintval();
152
153 if (LIBC_UNLIKELY((x_u & 0x0003'FFFF'FFFF'FFFF) == 0 ||
154 (y_u & 0x000F'FFFF'FFFF'FFFF) == 0)) {
155 if (auto r = check_special_inputs(x, y); LIBC_UNLIKELY(r.has_value()))
156 return r.value();
157 }
158
159 double e_x = static_cast<double>(xbits.get_exponent());
160 uint64_t x_mant = xbits.get_mantissa();
161 bool is_neg = false;
162 double sign_d = 1.0;
163
164 if (LIBC_UNLIKELY(y_a <= Y_LOWER_BOUND || y_a >= Y_UPPER_BOUND ||
165 x_u >= FPBits::inf().uintval() ||
166 x_u < FPBits::min_normal().uintval())) {
167 if (auto r = check_exceptional_cases(x, y, e_x, x_mant, is_neg, sign_d);
168 LIBC_UNLIKELY(r.has_value()))
169 return r.value();
170 }
171
172 // x^y = 2^( y * log2(x) )
173 // = 2^( y * ( e_x + log2(m_x) ) )
174 // First we compute log2(x) = e_x + log2(m_x)
175
176 // Extract exponent field of x.
177
178 // Use the highest 7 fractional bits of m_x as the index for look up tables.
179 unsigned idx_x = static_cast<unsigned>(x_mant >> (FPBits::FRACTION_LEN - 7));
180 // Add the hidden bit to the mantissa.
181 // 1 <= m_x < 2
182 FPBits m_x = FPBits(x_mant | 0x3ff0'0000'0000'0000);
183
184 // Reduced argument for log2(m_x):
185 // dx = r * m_x - 1.
186 // The computation is exact, and -2^-8 <= dx < 2^-7.
187 // Then m_x = (1 + dx) / r, and
188 // log2(m_x) = log2( (1 + dx) / r )
189 // = log2(1 + dx) - log2(r).
190
191 // As analyzed in the overview comment above, we evaluate the cubic part
192 // (dx * C1 + dx^2 * C2 + dx^3 * C3) accurately using DoubleDouble (n = 3)
193 // and approximate higher terms with dx^4 * P(dx) to ensure relative error
194 // < 2^-63 for this fast path.
195
196 // Degree-5 polynomial approximation for:
197 // P(dx) ~ (log2(1 + dx) - (dx - dx^2/2 + dx^3/3)/log(2)) / dx^4
198 // Generated by Sollya with:
199 // > P = fpminimax((log2(1 + x) - (x - x^2/2 + x^3/3)/log(2))/x^4, 5,
200 // [|D...|], [-2^-8, 2^-7]);
201 // > dirtyinfnorm((log2(1 + x) - (x - x^2/2 + x^3/3)/log(2))/x - x^3*P,
202 // [-2^-8, 2^-7]);
203 // 0x1.a643c...p-74
204 // > dirtyinfnorm((log2(1 + x) - (x - x^2/2 + x^3/3)/log(2))/x^4 - P,
205 // [-2^-8, 2^-7]);
206 // 0x1.b81c5...p-53
207 constexpr double COEFFS[] = {-0x1.71547652b82fdp-2, 0x1.2776c50ef8f9bp-2,
208 -0x1.ec709dc4f0fedp-3, 0x1.a617677f716dep-3,
209 -0x1.715423d54d5p-3, 0x1.44e6355fc4d03p-3};
210
211 // Constants for C_k = (-1)^(k-1) / (k * log(2)) in DoubleDouble:
212 // C1 = 1 / log(2)
213 constexpr DoubleDouble C1 = {.lo: 0x1.777d0ffda0d24p-56, .hi: 0x1.71547652b82fep0};
214 // C2 = -1 / (2 * log(2))
215 constexpr DoubleDouble C2 = {.lo: -0x1.777d0ffda0d24p-57, .hi: -0x1.71547652b82fep-1};
216 // C3 = 1 / (3 * log(2))
217 constexpr DoubleDouble C3 = {.lo: 0x1.b749fc15522bcp-50, .hi: 0x1.ec709dc3a03e2p-2};
218
219 // Perform exact range reduction.
220#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
221 double dx = fputil::multiply_sub(RD[idx_x], m_x.get_val(), 1.0); // Exact
222#else
223 double c = FPBits(m_x.uintval() & 0x3fff'e000'0000'0000).get_val();
224 double dx =
225 fputil::multiply_add(x: RD[idx_x], y: m_x.get_val() - c, z: CD[idx_x]); // Exact
226#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
227
228 // Evaluate the cubic part (dx * C1 + dx^2 * C2 + dx^3 * C3) and polynomial
229 // tail using a parallel Double-Double Estrin scheme.
230#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
231 // Error-free transformation for r = C1.hi + dx * C2.hi:
232 double r_hi = fputil::multiply_add(dx, C2.hi, C1.hi);
233 double r_lo = fputil::multiply_add(dx, C2.hi, C1.hi - r_hi); // Exact error
234#else
235 DoubleDouble dx_c2 = fputil::exact_mult(a: dx, b: C2.hi);
236 DoubleDouble r_sum = fputil::exact_add(a: C1.hi, b: dx_c2.hi);
237 double r_hi = r_sum.hi;
238 double r_lo = r_sum.lo + dx_c2.lo;
239#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
240
241 // Low parts polynomial evaluated in parallel:
242 // C1.lo + dx * (C2.lo + dx * C3.lo) + r_lo
243 double lo_tail = fputil::multiply_add(x: dx, y: C3.lo, z: C2.lo);
244 double lo_poly = fputil::multiply_add(x: dx, y: lo_tail, z: C1.lo + r_lo);
245
246 // Evaluate polynomial tail P(dx) using Estrin's scheme:
247 double dx2 = dx * dx;
248 double c0 = fputil::multiply_add(x: dx, y: COEFFS[1], z: COEFFS[0]);
249 double c1 = fputil::multiply_add(x: dx, y: COEFFS[3], z: COEFFS[2]);
250 double c2 = fputil::multiply_add(x: dx, y: COEFFS[5], z: COEFFS[4]);
251
252 double dx4 = dx2 * dx2;
253 double d0 = fputil::multiply_add(x: dx2, y: c1, z: c0);
254 double p = fputil::multiply_add(x: dx4, y: c2, z: d0);
255
256 // High part of cubic term: C3.hi + dx * P(dx)
257 double q = fputil::multiply_add(x: dx, y: p, z: C3.hi);
258
259 // Combine dx^2 * q into lo_poly:
260 lo_poly = fputil::multiply_add(x: dx2, y: q, z: lo_poly);
261
262 // Multiply by dx to get log2(1 + dx) in DoubleDouble:
263 DoubleDouble log2_1p = fputil::exact_mult(a: dx, b: r_hi);
264 log2_1p.lo = fputil::multiply_add(x: dx, y: lo_poly, z: log2_1p.lo);
265
266 // Combine with e_x - log2(r):
267 DoubleDouble log2_x_hi =
268 fputil::exact_add(a: e_x + LOG2_R_DD[idx_x].hi, b: log2_1p.hi);
269 double log2_x_lo = log2_1p.lo + LOG2_R_DD[idx_x].lo;
270 DoubleDouble log2_x = fputil::exact_add(a: log2_x_hi.hi, b: log2_x_lo);
271 log2_x.lo += log2_x_hi.lo;
272
273 // To compute 2^(y * log2(x)), we break the exponent into 3 parts:
274 // y * log2(x) = hi + mid + lo, where
275 // hi is an integer
276 // mid * 2^6 is an integer
277 // |lo| <= 2^-7
278 // Then:
279 // x^y = 2^(y * log2(x)) = 2^hi * 2^mid * 2^lo,
280 // In which 2^mid is obtained from a look-up table of size 2^6 = 64 elements,
281 // and 2^lo ~ 1 + lo * P(lo).
282 // Thus, we have:
283 // hi + mid = 2^-6 * round( 2^6 * y * log2(x) )
284 // If we restrict the output such that |hi| < 512, (hi + mid) uses (9 + 6)
285 // bits, hence, if we use double precision to perform
286 // round( 2^6 * y * log2(x))
287 // the lo part is bounded by 2^-7 + 2^(-(52 - 15)) = 2^-7 + 2^-37
288
289 // In the following computations:
290 // y6 = 2^6 * y
291 // hm = 2^6 * (hi + mid) = round(2^6 * y * log2(x)) ~ round(y6 * s)
292 // lo6 = 2^6 * lo = 2^6 * (y - (hi + mid)) = y6 * log2(x) - hm.
293 constexpr double SCALE = 0x1.0p6;
294 double y6 = y * SCALE; // Exact.
295
296 DoubleDouble y6_log2_x = fputil::exact_mult(a: y6, b: log2_x.hi);
297 y6_log2_x.lo = fputil::multiply_add(x: y6, y: log2_x.lo, z: y6_log2_x.lo);
298
299 // Check overflow/underflow.
300 double scale = 1.0;
301 bool is_denorm = false;
302
303 // |2^(hi + mid) - exp2_hi_mid| <= ulp(exp2_hi_mid) / 2
304
305 // The fast computation for 2^hi below requires that:
306 // |hi| < 512, or equivalently, |hm| < 512 * 2^6.
307 // This guarantees that the biased exponent:
308 // exp_biased = (hm_i >> 6) + EXP_BIAS = hi + 1023
309 // is strictly within the normal double range:
310 // 1023 - 511 <= exp_biased <= 1023 + 511, or 512 <= exp_biased <= 1534.
311 // Hence, 2^hi is always a normal, non-zero, finite power of 2, and the
312 // multiplication upper * exp2_hi is exact and never underflows or overflows.
313 //
314 // From the edge case checks above:
315 // 2^(-54) / 1074 <= |y| <= 1075 * 2^53, and
316 // |log_2(1 - 2^(-53))| <= |log2(x)| <= 1074.
317 // So their product is bounded by:
318 // 2^-117 < |y * log2(x)| < 2^74.
319 //
320 // The meaningful range for y * log2(x) in double precision is:
321 // -1075 <= y * log2(x) <= 1024.
322 // Any value > 1024 overflows, and any value < -1075 underflows.
323 //
324 // When |y * log2(x)| >= 511, we shift the exponent by an offset S:
325 // y * log2(x) = (y * log2(x) - S) + S
326 // and multiply by scale = 2^S at the end.
327 // To ensure the shifted exponent (y * log2(x) - S) stays within [-511, 511]:
328 // - For positive range [511, 1024]:
329 // 511 - S > -511 ==> S < 1022
330 // 1024 - S < 511 ==> S > 513
331 // - For negative range [-1076, -511]:
332 // -511 + S < 511 ==> S <= 1022
333 // -1076 + S > -511 ==> S >= 565
334 // Combined, the shift must satisfy: 565 <= S <= 1022.
335 // We choose S = 600, which yields:
336 // y * log2(x) - 600 in [-89, 424] for the positive range, and
337 // y * log2(x) + 600 in [-476, 89] for the negative range,
338 // both fitting well within [-511, 511].
339 //
340 // For exponents that are completely out of range:
341 // y * log2(x) > 1025 or y * log2(x) < -1076,
342 // the product can be as large as 2^74, so subtracting or adding 600 * 64
343 // would still overflow a 32-bit int when computing hm_i.
344 // We return early with overflow or underflow in these cases.
345 //
346 // Alternatively, for less branching or in SIMD / vector implementations, one
347 // could clamp y6_log2_x.hi to:
348 // - UPPER_EXP_BOUND (511 * 64) for overflow, which when multiplied by
349 // scale = 2^600 yields 2^1111 and correctly overflows.
350 // - -500 * 64 for underflow, which when multiplied by scale = 2^-600 yields
351 // 2^-1100 and correctly underflows.
352 // However, in this correctly rounded version, such clamping can cause the
353 // Ziv accuracy test to fail on the clamped exponent, unnecessarily
354 // triggering the slow accurate paths for overflow or underflow cases.
355
356 constexpr double UPPER_EXP_BOUND = 511.0 * SCALE;
357 if (LIBC_UNLIKELY(FPBits(y6_log2_x.hi).abs().get_val() >= UPPER_EXP_BOUND)) {
358 if (FPBits(y6_log2_x.hi).sign() == Sign::POS) {
359 if (y6_log2_x.hi > 1025.0 * SCALE)
360 return fputil::set_overflow<double>(is_neg ? Sign::NEG : Sign::POS);
361 scale = 0x1.0p600;
362 y6_log2_x.hi -= 600.0 * SCALE;
363 } else {
364 if (y6_log2_x.hi <= -1021.0 * SCALE) {
365 if (y6_log2_x.hi < -1076.0 * SCALE)
366 return fputil::set_underflow<double>(is_neg ? Sign::NEG : Sign::POS);
367 is_denorm = true;
368 } else {
369 scale = 0x1.0p-600;
370 y6_log2_x.hi += 600.0 * SCALE;
371 }
372 }
373 }
374
375 double hm = fputil::nearest_integer(x: y6_log2_x.hi);
376
377 // lo6 = 2^6 * lo.
378 DoubleDouble lo6 = fputil::exact_add(a: y6_log2_x.hi - hm, b: y6_log2_x.lo);
379
380 int hm_i = static_cast<int>(hm);
381 unsigned idx_y = static_cast<unsigned>(hm_i) & 0x3f;
382
383 // 2^hi
384 int hi = hm_i >> 6;
385 double exp2_hi = 1.0;
386 if (LIBC_LIKELY(!is_denorm)) {
387 int64_t exp2_hi_i = static_cast<int64_t>(
388 static_cast<uint64_t>(hi + FPBits::EXP_BIAS) << FPBits::FRACTION_LEN);
389 exp2_hi = FPBits(static_cast<uint64_t>(exp2_hi_i)).get_val();
390 }
391
392 // 2^mid
393 DoubleDouble exp2_mid{.lo: EXP2_MID1[idx_y].mid * sign_d,
394 .hi: EXP2_MID1[idx_y].hi * sign_d};
395
396 // Polynomial expansion for 2^(lo6/64):
397 // 2^(lo6/64) ~ 1 + lo6 * (log(2)/64) + lo6^2 * P(lo6)
398 // The linear term is computed in DoubleDouble, and the degree-3 polynomial
399 // P(lo6) is evaluated with standard double precision.
400 //
401 // hi and lo parts of log(2)/64, generated by Sollya with:
402 // > a = D(log(2)/64);
403 // > b = D(log(2)/64 - a);
404 constexpr DoubleDouble LOG_2_OVER_64 = {.lo: 0x1.abc9e3b39803fp-62,
405 .hi: 0x1.62e42fefa39efp-7};
406
407 // Degree-3 polynomial approximation for (2^(lo6/64) - 1 - lo6*log(2)/64) /
408 // lo6^2: Generated by Sollya with:
409 // > f = (2^(x/64) - 1 - x*log(2)/64) / x^2;
410 // > P = fpminimax(f, 5, [|D...|], [-0.5, 0.5]);
411 // > dirtyinfnorm((f - P) * x^2, [-0.5, 0.5]);
412 // 0x1.1778...p-73
413 constexpr double EXP2_COEFFS[] = {
414 0x1.ebfbdff82c58fp-15, 0x1.c6b08d704a0cp-23, 0x1.3b2ab6fb4d08fp-31,
415 0x1.5d87fe77a735dp-40, 0x1.430d835610044p-49, 0x1.ffe67c38112c3p-59};
416
417 DoubleDouble lo_log_2 = fputil::quick_mult(a: lo6, b: LOG_2_OVER_64);
418 DoubleDouble lo_log_2_p1 = fputil::exact_add(a: 1.0, b: lo_log_2.hi);
419 lo_log_2_p1.lo += lo_log_2.lo;
420
421 double lo6_sq = lo6.hi * lo6.hi;
422 double e0 = fputil::multiply_add(x: lo6.hi, y: EXP2_COEFFS[1], z: EXP2_COEFFS[0]);
423 double e1 = fputil::multiply_add(x: lo6.hi, y: EXP2_COEFFS[3], z: EXP2_COEFFS[2]);
424 double e2 = fputil::multiply_add(x: lo6.hi, y: EXP2_COEFFS[5], z: EXP2_COEFFS[4]);
425
426 double lo6_4 = lo6_sq * lo6_sq;
427 double f0 = fputil::multiply_add(x: lo6_sq, y: e0, z: lo_log_2_p1.lo);
428 double f1 = fputil::multiply_add(x: lo6_sq, y: e2, z: e1);
429
430 lo_log_2_p1.lo = fputil::multiply_add(x: lo6_4, y: f1, z: f0);
431 DoubleDouble r = fputil::quick_mult(a: exp2_mid, b: lo_log_2_p1);
432
433 // Absolute error bound for r:
434 // - Base error from exp2 stage is bounded by 0x1.0p-64.
435 // - Propagated error from log2(x):
436 // |y| * err(log2(x)) * (2^mid * log(2)) <= |y| * 2^-73.5
437 // Dynamic error bound adapting to exponent scale |y|:
438#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
439 double err_r =
440 fputil::multiply_add(FPBits(y).abs().get_val(), 0x1.8p-73, 0x1.0p-64);
441#else // !LIBC_TARGET_CPU_HAS_FMA_DOUBLE
442 // Without FMA, intermediate roundings increase log2(x) and exp2 errors.
443 double err_r =
444 fputil::multiply_add(x: FPBits(y).abs().get_val(), y: 0x1.cp-72, z: 0x1.0p-63);
445#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
446
447 if (LIBC_UNLIKELY(is_denorm)) {
448 if (auto r_denorm = ziv_test_denorm(hi, mid: r.hi, lo: r.lo, err: err_r, is_neg);
449 LIBC_LIKELY(r_denorm.has_value())) {
450 double res = r_denorm.value();
451
452 // Since is_denorm is triggered when y * log2(x) <= -1021 * 2^6, the
453 // rounded result can still be a normal number (>= 2^-1022). In that
454 // case, no underflow has occurred, so return directly.
455 if (LIBC_UNLIKELY(FPBits(res).is_normal()))
456 return res;
457
458 // When the rounded result is denormal or zero, underflow and ERANGE
459 // should only be set if the result is inexact. We check for exact
460 // results:
461 // 1. If x = 2^e_x (x_mant == 0), then x^y = 2^(e_x * y) is an exact
462 // power of 2 iff e_x * y is an integer and >= -1074.0 (the smallest
463 // representable power of 2 in double precision).
464 if (LIBC_UNLIKELY(x_mant == 0)) {
465 double ex_y = e_x * y;
466 if (ex_y >= -1074.0 && fputil::is_integer(x: ex_y))
467 return res;
468 } else if (LIBC_UNLIKELY(y > 0.0 && y <= 35.0)) {
469 // 2. If x is not a power of 2, exact subnormal results in double
470 // precision have odd exact_m and exact_exp >= -1074.
471 uint64_t exact_m = 0;
472 int exact_exp = 0;
473 if (pow_internal::is_exact_rounding_boundary(
474 x_e: static_cast<int>(e_x), x_mant, y, exact_m, exact_exp) &&
475 exact_exp >= -1074)
476 return res;
477 }
478
479 // Otherwise, the denormal or zero result is inexact, so we signal the
480 // underflow exception and set ERANGE if required.
481 fputil::set_errno_if_required(ERANGE);
482 fputil::raise_underflow_except_if_required<double>();
483 return res;
484 }
485 return pow_accurate(y, is_neg, x_e: static_cast<int>(e_x), x_mant);
486 }
487
488 double upper = r.hi + (r.lo + err_r);
489 double lower = r.hi + (r.lo - err_r);
490 if (LIBC_LIKELY(upper == lower)) {
491 double tmp1 = upper * exp2_hi;
492 if (LIBC_UNLIKELY(scale == 0x1.0p600 &&
493 FPBits(tmp1).abs().get_val() >= 0x1.0p424))
494 return fputil::set_overflow<double>(is_neg ? Sign::NEG : Sign::POS);
495 return tmp1 * scale;
496 }
497
498 return pow_accurate(y, is_neg, x_e: static_cast<int>(e_x), x_mant);
499}
500
501} // namespace math
502} // namespace LIBC_NAMESPACE_DECL
503
504#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
505
506#endif // LLVM_LIBC_SRC___SUPPORT_MATH_POW_H
507