382 lines · c
1/*2 * Double-precision x^y function.3 *4 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5 * See https://llvm.org/LICENSE.txt for license information.6 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7 */8 9#include <float.h>10#include <math.h>11#include <stdint.h>12#include "math_config.h"13 14/*15Worst-case error: 0.54 ULP (~= ulperr_exp + 1024*Ln2*relerr_log*2^53)16relerr_log: 1.3 * 2^-68 (Relative error of log, 1.5 * 2^-68 without fma)17ulperr_exp: 0.509 ULP (ULP error of exp, 0.511 ULP without fma)18*/19 20#define T __pow_log_data.tab21#define A __pow_log_data.poly22#define Ln2hi __pow_log_data.ln2hi23#define Ln2lo __pow_log_data.ln2lo24#define N (1 << POW_LOG_TABLE_BITS)25#define OFF 0x3fe695550000000026 27/* Top 12 bits of a double (sign and exponent bits). */28static inline uint32_t29top12 (double x)30{31 return asuint64 (x) >> 52;32}33 34/* Compute y+TAIL = log(x) where the rounded result is y and TAIL has about35 additional 15 bits precision. IX is the bit representation of x, but36 normalized in the subnormal range using the sign bit for the exponent. */37static inline double_t38log_inline (uint64_t ix, double_t *tail)39{40 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */41 double_t z, r, y, invc, logc, logctail, kd, hi, t1, t2, lo, lo1, lo2, p;42 uint64_t iz, tmp;43 int k, i;44 45 /* x = 2^k z; where z is in range [OFF,2*OFF) and exact.46 The range is split into N subintervals.47 The ith subinterval contains z and c is near its center. */48 tmp = ix - OFF;49 i = (tmp >> (52 - POW_LOG_TABLE_BITS)) % N;50 k = (int64_t) tmp >> 52; /* arithmetic shift */51 iz = ix - (tmp & 0xfffULL << 52);52 z = asdouble (iz);53 kd = (double_t) k;54 55 /* log(x) = k*Ln2 + log(c) + log1p(z/c-1). */56 invc = T[i].invc;57 logc = T[i].logc;58 logctail = T[i].logctail;59 60 /* Note: 1/c is j/N or j/N/2 where j is an integer in [N,2N) and61 |z/c - 1| < 1/N, so r = z/c - 1 is exactly representable. */62#if HAVE_FAST_FMA63 r = fma (z, invc, -1.0);64#else65 /* Split z such that rhi, rlo and rhi*rhi are exact and |rlo| <= |r|. */66 double_t zhi = asdouble ((iz + (1ULL << 31)) & (-1ULL << 32));67 double_t zlo = z - zhi;68 double_t rhi = zhi * invc - 1.0;69 double_t rlo = zlo * invc;70 r = rhi + rlo;71#endif72 73 /* k*Ln2 + log(c) + r. */74 t1 = kd * Ln2hi + logc;75 t2 = t1 + r;76 lo1 = kd * Ln2lo + logctail;77 lo2 = t1 - t2 + r;78 79 /* Evaluation is optimized assuming superscalar pipelined execution. */80 double_t ar, ar2, ar3, lo3, lo4;81 ar = A[0] * r; /* A[0] = -0.5. */82 ar2 = r * ar;83 ar3 = r * ar2;84 /* k*Ln2 + log(c) + r + A[0]*r*r. */85#if HAVE_FAST_FMA86 hi = t2 + ar2;87 lo3 = fma (ar, r, -ar2);88 lo4 = t2 - hi + ar2;89#else90 double_t arhi = A[0] * rhi;91 double_t arhi2 = rhi * arhi;92 hi = t2 + arhi2;93 lo3 = rlo * (ar + arhi);94 lo4 = t2 - hi + arhi2;95#endif96 /* p = log1p(r) - r - A[0]*r*r. */97#if POW_LOG_POLY_ORDER == 898 p = (ar399 * (A[1] + r * A[2] + ar2 * (A[3] + r * A[4] + ar2 * (A[5] + r * A[6]))));100#endif101 lo = lo1 + lo2 + lo3 + lo4 + p;102 y = hi + lo;103 *tail = hi - y + lo;104 return y;105}106 107#undef N108#undef T109#define N (1 << EXP_TABLE_BITS)110#define InvLn2N __exp_data.invln2N111#define NegLn2hiN __exp_data.negln2hiN112#define NegLn2loN __exp_data.negln2loN113#define Shift __exp_data.shift114#define T __exp_data.tab115#define C2 __exp_data.poly[5 - EXP_POLY_ORDER]116#define C3 __exp_data.poly[6 - EXP_POLY_ORDER]117#define C4 __exp_data.poly[7 - EXP_POLY_ORDER]118#define C5 __exp_data.poly[8 - EXP_POLY_ORDER]119#define C6 __exp_data.poly[9 - EXP_POLY_ORDER]120 121/* Handle cases that may overflow or underflow when computing the result that122 is scale*(1+TMP) without intermediate rounding. The bit representation of123 scale is in SBITS, however it has a computed exponent that may have124 overflown into the sign bit so that needs to be adjusted before using it as125 a double. (int32_t)KI is the k used in the argument reduction and exponent126 adjustment of scale, positive k here means the result may overflow and127 negative k means the result may underflow. */128static inline double129specialcase (double_t tmp, uint64_t sbits, uint64_t ki)130{131 double_t scale, y;132 133 if ((ki & 0x80000000) == 0)134 {135 /* k > 0, the exponent of scale might have overflowed by <= 460. */136 sbits -= 1009ull << 52;137 scale = asdouble (sbits);138 y = 0x1p1009 * (scale + scale * tmp);139 return check_oflow (eval_as_double (y));140 }141 /* k < 0, need special care in the subnormal range. */142 sbits += 1022ull << 52;143 /* Note: sbits is signed scale. */144 scale = asdouble (sbits);145 y = scale + scale * tmp;146 if (fabs (y) < 1.0)147 {148 /* Round y to the right precision before scaling it into the subnormal149 range to avoid double rounding that can cause 0.5+E/2 ulp error where150 E is the worst-case ulp error outside the subnormal range. So this151 is only useful if the goal is better than 1 ulp worst-case error. */152 double_t hi, lo, one = 1.0;153 if (y < 0.0)154 one = -1.0;155 lo = scale - y + scale * tmp;156 hi = one + y;157 lo = one - hi + y + lo;158 y = eval_as_double (hi + lo) - one;159 /* Fix the sign of 0. */160 if (y == 0.0)161 y = asdouble (sbits & 0x8000000000000000);162 /* The underflow exception needs to be signaled explicitly. */163 force_eval_double (opt_barrier_double (0x1p-1022) * 0x1p-1022);164 }165 y = 0x1p-1022 * y;166 return check_uflow (eval_as_double (y));167}168 169#define SIGN_BIAS (0x800 << EXP_TABLE_BITS)170 171/* Computes sign*exp(x+xtail) where |xtail| < 2^-8/N and |xtail| <= |x|.172 The sign_bias argument is SIGN_BIAS or 0 and sets the sign to -1 or 1. */173static inline double174exp_inline (double_t x, double_t xtail, uint32_t sign_bias)175{176 uint32_t abstop;177 uint64_t ki, idx, top, sbits;178 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */179 double_t kd, z, r, r2, scale, tail, tmp;180 181 abstop = top12 (x) & 0x7ff;182 if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54)))183 {184 if (abstop - top12 (0x1p-54) >= 0x80000000)185 {186 /* Avoid spurious underflow for tiny x. */187 /* Note: 0 is common input. */188 double_t one = WANT_ROUNDING ? 1.0 + x : 1.0;189 return sign_bias ? -one : one;190 }191 if (abstop >= top12 (1024.0))192 {193 /* Note: inf and nan are already handled. */194 if (asuint64 (x) >> 63)195 return __math_uflow (sign_bias);196 else197 return __math_oflow (sign_bias);198 }199 /* Large x is special cased below. */200 abstop = 0;201 }202 203 /* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)]. */204 /* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N]. */205 z = InvLn2N * x;206#if TOINT_INTRINSICS207 kd = roundtoint (z);208 ki = converttoint (z);209#elif EXP_USE_TOINT_NARROW210 /* z - kd is in [-0.5-2^-16, 0.5] in all rounding modes. */211 kd = eval_as_double (z + Shift);212 ki = asuint64 (kd) >> 16;213 kd = (double_t) (int32_t) ki;214#else215 /* z - kd is in [-1, 1] in non-nearest rounding modes. */216 kd = eval_as_double (z + Shift);217 ki = asuint64 (kd);218 kd -= Shift;219#endif220 r = x + kd * NegLn2hiN + kd * NegLn2loN;221 /* The code assumes 2^-200 < |xtail| < 2^-8/N. */222 r += xtail;223 /* 2^(k/N) ~= scale * (1 + tail). */224 idx = 2 * (ki % N);225 top = (ki + sign_bias) << (52 - EXP_TABLE_BITS);226 tail = asdouble (T[idx]);227 /* This is only a valid scale when -1023*N < k < 1024*N. */228 sbits = T[idx + 1] + top;229 /* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1). */230 /* Evaluation is optimized assuming superscalar pipelined execution. */231 r2 = r * r;232 /* Without fma the worst case error is 0.25/N ulp larger. */233 /* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp. */234#if EXP_POLY_ORDER == 4235 tmp = tail + r + r2 * C2 + r * r2 * (C3 + r * C4);236#elif EXP_POLY_ORDER == 5237 tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);238#elif EXP_POLY_ORDER == 6239 tmp = tail + r + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6);240#endif241 if (unlikely (abstop == 0))242 return specialcase (tmp, sbits, ki);243 scale = asdouble (sbits);244 /* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there245 is no spurious underflow here even without fma. */246 return eval_as_double (scale + scale * tmp);247}248 249/* Returns 0 if not int, 1 if odd int, 2 if even int. The argument is250 the bit representation of a non-zero finite floating-point value. */251static inline int252checkint (uint64_t iy)253{254 int e = iy >> 52 & 0x7ff;255 if (e < 0x3ff)256 return 0;257 if (e > 0x3ff + 52)258 return 2;259 if (iy & ((1ULL << (0x3ff + 52 - e)) - 1))260 return 0;261 if (iy & (1ULL << (0x3ff + 52 - e)))262 return 1;263 return 2;264}265 266/* Returns 1 if input is the bit representation of 0, infinity or nan. */267static inline int268zeroinfnan (uint64_t i)269{270 return 2 * i - 1 >= 2 * asuint64 (INFINITY) - 1;271}272 273double274pow (double x, double y)275{276 uint32_t sign_bias = 0;277 uint64_t ix, iy;278 uint32_t topx, topy;279 280 ix = asuint64 (x);281 iy = asuint64 (y);282 topx = top12 (x);283 topy = top12 (y);284 if (unlikely (topx - 0x001 >= 0x7ff - 0x001285 || (topy & 0x7ff) - 0x3be >= 0x43e - 0x3be))286 {287 /* Note: if |y| > 1075 * ln2 * 2^53 ~= 0x1.749p62 then pow(x,y) = inf/0288 and if |y| < 2^-54 / 1075 ~= 0x1.e7b6p-65 then pow(x,y) = +-1. */289 /* Special cases: (x < 0x1p-126 or inf or nan) or290 (|y| < 0x1p-65 or |y| >= 0x1p63 or nan). */291 if (unlikely (zeroinfnan (iy)))292 {293 if (2 * iy == 0)294 return issignaling_inline (x) ? x + y : 1.0;295 if (ix == asuint64 (1.0))296 return issignaling_inline (y) ? x + y : 1.0;297 if (2 * ix > 2 * asuint64 (INFINITY)298 || 2 * iy > 2 * asuint64 (INFINITY))299 return x + y;300 if (2 * ix == 2 * asuint64 (1.0))301 return 1.0;302 if ((2 * ix < 2 * asuint64 (1.0)) == !(iy >> 63))303 return 0.0; /* |x|<1 && y==inf or |x|>1 && y==-inf. */304 return y * y;305 }306 if (unlikely (zeroinfnan (ix)))307 {308 double_t x2 = x * x;309 if (ix >> 63 && checkint (iy) == 1)310 {311 x2 = -x2;312 sign_bias = 1;313 }314 if (WANT_ERRNO && 2 * ix == 0 && iy >> 63)315 return __math_divzero (sign_bias);316 /* Without the barrier some versions of clang hoist the 1/x2 and317 thus division by zero exception can be signaled spuriously. */318 return iy >> 63 ? opt_barrier_double (1 / x2) : x2;319 }320 /* Here x and y are non-zero finite. */321 if (ix >> 63)322 {323 /* Finite x < 0. */324 int yint = checkint (iy);325 if (yint == 0)326 return __math_invalid (x);327 if (yint == 1)328 sign_bias = SIGN_BIAS;329 ix &= 0x7fffffffffffffff;330 topx &= 0x7ff;331 }332 if ((topy & 0x7ff) - 0x3be >= 0x43e - 0x3be)333 {334 /* Note: sign_bias == 0 here because y is not odd. */335 if (ix == asuint64 (1.0))336 return 1.0;337 if ((topy & 0x7ff) < 0x3be)338 {339 /* |y| < 2^-65, x^y ~= 1 + y*log(x). */340 if (WANT_ROUNDING)341 return ix > asuint64 (1.0) ? 1.0 + y : 1.0 - y;342 else343 return 1.0;344 }345 return (ix > asuint64 (1.0)) == (topy < 0x800) ? __math_oflow (0)346 : __math_uflow (0);347 }348 if (topx == 0)349 {350 /* Normalize subnormal x so exponent becomes negative. */351 /* Without the barrier some versions of clang evaluate the mul352 unconditionally causing spurious overflow exceptions. */353 ix = asuint64 (opt_barrier_double (x) * 0x1p52);354 ix &= 0x7fffffffffffffff;355 ix -= 52ULL << 52;356 }357 }358 359 double_t lo;360 double_t hi = log_inline (ix, &lo);361 double_t ehi, elo;362#if HAVE_FAST_FMA363 ehi = y * hi;364 elo = y * lo + fma (y, hi, -ehi);365#else366 double_t yhi = asdouble (iy & -1ULL << 27);367 double_t ylo = y - yhi;368 double_t lhi = asdouble (asuint64 (hi) & -1ULL << 27);369 double_t llo = hi - lhi + lo;370 ehi = yhi * lhi;371 elo = ylo * lhi + y * llo; /* |elo| < |ehi| * 2^-25. */372#endif373 return exp_inline (ehi, elo, sign_bias);374}375#if USE_GLIBC_ABI376strong_alias (pow, __pow_finite)377hidden_alias (pow, __ieee754_pow)378# if LDBL_MANT_DIG == 53379long double powl (long double x, long double y) { return pow (x, y); }380# endif381#endif382