GLIBC-SA-2024-0004: add commit for 2.31 branch
[glibc.git] / sysdeps / ieee754 / dbl-64 / e_pow.c
blob8f9384662eb662f75287bbfc5c8aa1aab2f1cd75
1 /* Double-precision x^y function.
2 Copyright (C) 2018-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <math.h>
20 #include <stdint.h>
21 #include <math-barriers.h>
22 #include <math-narrow-eval.h>
23 #include <math-svid-compat.h>
24 #include <libm-alias-finite.h>
25 #include <libm-alias-double.h>
26 #include "math_config.h"
29 Worst-case error: 0.54 ULP (~= ulperr_exp + 1024*Ln2*relerr_log*2^53)
30 relerr_log: 1.3 * 2^-68 (Relative error of log, 1.5 * 2^-68 without fma)
31 ulperr_exp: 0.509 ULP (ULP error of exp, 0.511 ULP without fma)
34 #define T __pow_log_data.tab
35 #define A __pow_log_data.poly
36 #define Ln2hi __pow_log_data.ln2hi
37 #define Ln2lo __pow_log_data.ln2lo
38 #define N (1 << POW_LOG_TABLE_BITS)
39 #define OFF 0x3fe6955500000000
41 /* Top 12 bits of a double (sign and exponent bits). */
42 static inline uint32_t
43 top12 (double x)
45 return asuint64 (x) >> 52;
48 /* Compute y+TAIL = log(x) where the rounded result is y and TAIL has about
49 additional 15 bits precision. IX is the bit representation of x, but
50 normalized in the subnormal range using the sign bit for the exponent. */
51 static inline double_t
52 log_inline (uint64_t ix, double_t *tail)
54 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
55 double_t z, r, y, invc, logc, logctail, kd, hi, t1, t2, lo, lo1, lo2, p;
56 uint64_t iz, tmp;
57 int k, i;
59 /* x = 2^k z; where z is in range [OFF,2*OFF) and exact.
60 The range is split into N subintervals.
61 The ith subinterval contains z and c is near its center. */
62 tmp = ix - OFF;
63 i = (tmp >> (52 - POW_LOG_TABLE_BITS)) % N;
64 k = (int64_t) tmp >> 52; /* arithmetic shift */
65 iz = ix - (tmp & 0xfffULL << 52);
66 z = asdouble (iz);
67 kd = (double_t) k;
69 /* log(x) = k*Ln2 + log(c) + log1p(z/c-1). */
70 invc = T[i].invc;
71 logc = T[i].logc;
72 logctail = T[i].logctail;
74 /* Note: 1/c is j/N or j/N/2 where j is an integer in [N,2N) and
75 |z/c - 1| < 1/N, so r = z/c - 1 is exactly representible. */
76 #ifdef __FP_FAST_FMA
77 r = __builtin_fma (z, invc, -1.0);
78 #else
79 /* Split z such that rhi, rlo and rhi*rhi are exact and |rlo| <= |r|. */
80 double_t zhi = asdouble ((iz + (1ULL << 31)) & (-1ULL << 32));
81 double_t zlo = z - zhi;
82 double_t rhi = zhi * invc - 1.0;
83 double_t rlo = zlo * invc;
84 r = rhi + rlo;
85 #endif
87 /* k*Ln2 + log(c) + r. */
88 t1 = kd * Ln2hi + logc;
89 t2 = t1 + r;
90 lo1 = kd * Ln2lo + logctail;
91 lo2 = t1 - t2 + r;
93 /* Evaluation is optimized assuming superscalar pipelined execution. */
94 double_t ar, ar2, ar3, lo3, lo4;
95 ar = A[0] * r; /* A[0] = -0.5. */
96 ar2 = r * ar;
97 ar3 = r * ar2;
98 /* k*Ln2 + log(c) + r + A[0]*r*r. */
99 #ifdef __FP_FAST_FMA
100 hi = t2 + ar2;
101 lo3 = __builtin_fma (ar, r, -ar2);
102 lo4 = t2 - hi + ar2;
103 #else
104 double_t arhi = A[0] * rhi;
105 double_t arhi2 = rhi * arhi;
106 hi = t2 + arhi2;
107 lo3 = rlo * (ar + arhi);
108 lo4 = t2 - hi + arhi2;
109 #endif
110 /* p = log1p(r) - r - A[0]*r*r. */
111 p = (ar3
112 * (A[1] + r * A[2] + ar2 * (A[3] + r * A[4] + ar2 * (A[5] + r * A[6]))));
113 lo = lo1 + lo2 + lo3 + lo4 + p;
114 y = hi + lo;
115 *tail = hi - y + lo;
116 return y;
119 #undef N
120 #undef T
121 #define N (1 << EXP_TABLE_BITS)
122 #define InvLn2N __exp_data.invln2N
123 #define NegLn2hiN __exp_data.negln2hiN
124 #define NegLn2loN __exp_data.negln2loN
125 #define Shift __exp_data.shift
126 #define T __exp_data.tab
127 #define C2 __exp_data.poly[5 - EXP_POLY_ORDER]
128 #define C3 __exp_data.poly[6 - EXP_POLY_ORDER]
129 #define C4 __exp_data.poly[7 - EXP_POLY_ORDER]
130 #define C5 __exp_data.poly[8 - EXP_POLY_ORDER]
131 #define C6 __exp_data.poly[9 - EXP_POLY_ORDER]
133 /* Handle cases that may overflow or underflow when computing the result that
134 is scale*(1+TMP) without intermediate rounding. The bit representation of
135 scale is in SBITS, however it has a computed exponent that may have
136 overflown into the sign bit so that needs to be adjusted before using it as
137 a double. (int32_t)KI is the k used in the argument reduction and exponent
138 adjustment of scale, positive k here means the result may overflow and
139 negative k means the result may underflow. */
140 static inline double
141 specialcase (double_t tmp, uint64_t sbits, uint64_t ki)
143 double_t scale, y;
145 if ((ki & 0x80000000) == 0)
147 /* k > 0, the exponent of scale might have overflowed by <= 460. */
148 sbits -= 1009ull << 52;
149 scale = asdouble (sbits);
150 y = 0x1p1009 * (scale + scale * tmp);
151 return check_oflow (y);
153 /* k < 0, need special care in the subnormal range. */
154 sbits += 1022ull << 52;
155 /* Note: sbits is signed scale. */
156 scale = asdouble (sbits);
157 y = scale + scale * tmp;
158 if (fabs (y) < 1.0)
160 /* Round y to the right precision before scaling it into the subnormal
161 range to avoid double rounding that can cause 0.5+E/2 ulp error where
162 E is the worst-case ulp error outside the subnormal range. So this
163 is only useful if the goal is better than 1 ulp worst-case error. */
164 double_t hi, lo, one = 1.0;
165 if (y < 0.0)
166 one = -1.0;
167 lo = scale - y + scale * tmp;
168 hi = one + y;
169 lo = one - hi + y + lo;
170 y = math_narrow_eval (hi + lo) - one;
171 /* Fix the sign of 0. */
172 if (y == 0.0)
173 y = asdouble (sbits & 0x8000000000000000);
174 /* The underflow exception needs to be signaled explicitly. */
175 math_force_eval (math_opt_barrier (0x1p-1022) * 0x1p-1022);
177 y = 0x1p-1022 * y;
178 return check_uflow (y);
181 #define SIGN_BIAS (0x800 << EXP_TABLE_BITS)
183 /* Computes sign*exp(x+xtail) where |xtail| < 2^-8/N and |xtail| <= |x|.
184 The sign_bias argument is SIGN_BIAS or 0 and sets the sign to -1 or 1. */
185 static inline double
186 exp_inline (double x, double xtail, uint32_t sign_bias)
188 uint32_t abstop;
189 uint64_t ki, idx, top, sbits;
190 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
191 double_t kd, z, r, r2, scale, tail, tmp;
193 abstop = top12 (x) & 0x7ff;
194 if (__glibc_unlikely (abstop - top12 (0x1p-54)
195 >= top12 (512.0) - top12 (0x1p-54)))
197 if (abstop - top12 (0x1p-54) >= 0x80000000)
199 /* Avoid spurious underflow for tiny x. */
200 /* Note: 0 is common input. */
201 double_t one = WANT_ROUNDING ? 1.0 + x : 1.0;
202 return sign_bias ? -one : one;
204 if (abstop >= top12 (1024.0))
206 /* Note: inf and nan are already handled. */
207 if (asuint64 (x) >> 63)
208 return __math_uflow (sign_bias);
209 else
210 return __math_oflow (sign_bias);
212 /* Large x is special cased below. */
213 abstop = 0;
216 /* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)]. */
217 /* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N]. */
218 z = InvLn2N * x;
219 #if TOINT_INTRINSICS
220 /* z - kd is in [-0.5, 0.5] in all rounding modes. */
221 kd = roundtoint (z);
222 ki = converttoint (z);
223 #else
224 /* z - kd is in [-1, 1] in non-nearest rounding modes. */
225 kd = math_narrow_eval (z + Shift);
226 ki = asuint64 (kd);
227 kd -= Shift;
228 #endif
229 r = x + kd * NegLn2hiN + kd * NegLn2loN;
230 /* The code assumes 2^-200 < |xtail| < 2^-8/N. */
231 r += xtail;
232 /* 2^(k/N) ~= scale * (1 + tail). */
233 idx = 2 * (ki % N);
234 top = (ki + sign_bias) << (52 - EXP_TABLE_BITS);
235 tail = asdouble (T[idx]);
236 /* This is only a valid scale when -1023*N < k < 1024*N. */
237 sbits = T[idx + 1] + top;
238 /* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1). */
239 /* Evaluation is optimized assuming superscalar pipelined execution. */
240 r2 = r * r;
241 /* Without fma the worst case error is 0.25/N ulp larger. */
242 /* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp. */
243 tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
244 if (__glibc_unlikely (abstop == 0))
245 return specialcase (tmp, sbits, ki);
246 scale = asdouble (sbits);
247 /* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there
248 is no spurious underflow here even without fma. */
249 return scale + scale * tmp;
252 /* Returns 0 if not int, 1 if odd int, 2 if even int. The argument is
253 the bit representation of a non-zero finite floating-point value. */
254 static inline int
255 checkint (uint64_t iy)
257 int e = iy >> 52 & 0x7ff;
258 if (e < 0x3ff)
259 return 0;
260 if (e > 0x3ff + 52)
261 return 2;
262 if (iy & ((1ULL << (0x3ff + 52 - e)) - 1))
263 return 0;
264 if (iy & (1ULL << (0x3ff + 52 - e)))
265 return 1;
266 return 2;
269 /* Returns 1 if input is the bit representation of 0, infinity or nan. */
270 static inline int
271 zeroinfnan (uint64_t i)
273 return 2 * i - 1 >= 2 * asuint64 (INFINITY) - 1;
276 #ifndef SECTION
277 # define SECTION
278 #endif
280 double
281 SECTION
282 __pow (double x, double y)
284 uint32_t sign_bias = 0;
285 uint64_t ix, iy;
286 uint32_t topx, topy;
288 ix = asuint64 (x);
289 iy = asuint64 (y);
290 topx = top12 (x);
291 topy = top12 (y);
292 if (__glibc_unlikely (topx - 0x001 >= 0x7ff - 0x001
293 || (topy & 0x7ff) - 0x3be >= 0x43e - 0x3be))
295 /* Note: if |y| > 1075 * ln2 * 2^53 ~= 0x1.749p62 then pow(x,y) = inf/0
296 and if |y| < 2^-54 / 1075 ~= 0x1.e7b6p-65 then pow(x,y) = +-1. */
297 /* Special cases: (x < 0x1p-126 or inf or nan) or
298 (|y| < 0x1p-65 or |y| >= 0x1p63 or nan). */
299 if (__glibc_unlikely (zeroinfnan (iy)))
301 if (2 * iy == 0)
302 return issignaling_inline (x) ? x + y : 1.0;
303 if (ix == asuint64 (1.0))
304 return issignaling_inline (y) ? x + y : 1.0;
305 if (2 * ix > 2 * asuint64 (INFINITY)
306 || 2 * iy > 2 * asuint64 (INFINITY))
307 return x + y;
308 if (2 * ix == 2 * asuint64 (1.0))
309 return 1.0;
310 if ((2 * ix < 2 * asuint64 (1.0)) == !(iy >> 63))
311 return 0.0; /* |x|<1 && y==inf or |x|>1 && y==-inf. */
312 return y * y;
314 if (__glibc_unlikely (zeroinfnan (ix)))
316 double_t x2 = x * x;
317 if (ix >> 63 && checkint (iy) == 1)
319 x2 = -x2;
320 sign_bias = 1;
322 if (WANT_ERRNO && 2 * ix == 0 && iy >> 63)
323 return __math_divzero (sign_bias);
324 /* Without the barrier some versions of clang hoist the 1/x2 and
325 thus division by zero exception can be signaled spuriously. */
326 return iy >> 63 ? math_opt_barrier (1 / x2) : x2;
328 /* Here x and y are non-zero finite. */
329 if (ix >> 63)
331 /* Finite x < 0. */
332 int yint = checkint (iy);
333 if (yint == 0)
334 return __math_invalid (x);
335 if (yint == 1)
336 sign_bias = SIGN_BIAS;
337 ix &= 0x7fffffffffffffff;
338 topx &= 0x7ff;
340 if ((topy & 0x7ff) - 0x3be >= 0x43e - 0x3be)
342 /* Note: sign_bias == 0 here because y is not odd. */
343 if (ix == asuint64 (1.0))
344 return 1.0;
345 if ((topy & 0x7ff) < 0x3be)
347 /* |y| < 2^-65, x^y ~= 1 + y*log(x). */
348 if (WANT_ROUNDING)
349 return ix > asuint64 (1.0) ? 1.0 + y : 1.0 - y;
350 else
351 return 1.0;
353 return (ix > asuint64 (1.0)) == (topy < 0x800) ? __math_oflow (0)
354 : __math_uflow (0);
356 if (topx == 0)
358 /* Normalize subnormal x so exponent becomes negative. */
359 ix = asuint64 (x * 0x1p52);
360 ix &= 0x7fffffffffffffff;
361 ix -= 52ULL << 52;
365 double_t lo;
366 double_t hi = log_inline (ix, &lo);
367 double_t ehi, elo;
368 #ifdef __FP_FAST_FMA
369 ehi = y * hi;
370 elo = y * lo + __builtin_fma (y, hi, -ehi);
371 #else
372 double_t yhi = asdouble (iy & -1ULL << 27);
373 double_t ylo = y - yhi;
374 double_t lhi = asdouble (asuint64 (hi) & -1ULL << 27);
375 double_t llo = hi - lhi + lo;
376 ehi = yhi * lhi;
377 elo = ylo * lhi + y * llo; /* |elo| < |ehi| * 2^-25. */
378 #endif
379 return exp_inline (ehi, elo, sign_bias);
381 #ifndef __pow
382 strong_alias (__pow, __ieee754_pow)
383 libm_alias_finite (__ieee754_pow, __pow)
384 # if LIBM_SVID_COMPAT
385 versioned_symbol (libm, __pow, pow, GLIBC_2_29);
386 libm_alias_double_other (__pow, pow)
387 # else
388 libm_alias_double (__pow, pow)
389 # endif
390 #endif