Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Double-precision x^y function. |
| 3 | * |
| 4 | * Copyright (c) 2018, Arm Limited. |
Szabolcs Nagy | 11253b0 | 2018-11-12 11:10:57 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: MIT |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Szabolcs Nagy | f934c86 | 2019-08-28 15:15:15 +0100 | [diff] [blame] | 8 | #include <float.h> |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 9 | #include <math.h> |
| 10 | #include <stdint.h> |
| 11 | #include "math_config.h" |
| 12 | |
| 13 | /* |
Szabolcs Nagy | 5049bfa | 2018-06-25 12:33:56 +0100 | [diff] [blame] | 14 | Worst-case error: 0.54 ULP (~= ulperr_exp + 1024*Ln2*relerr_log*2^53) |
| 15 | relerr_log: 1.3 * 2^-68 (Relative error of log, 1.5 * 2^-68 without fma) |
| 16 | ulperr_exp: 0.509 ULP (ULP error of exp, 0.511 ULP without fma) |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #define T __pow_log_data.tab |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 20 | #define A __pow_log_data.poly |
| 21 | #define Ln2hi __pow_log_data.ln2hi |
| 22 | #define Ln2lo __pow_log_data.ln2lo |
| 23 | #define N (1 << POW_LOG_TABLE_BITS) |
Szabolcs Nagy | a623032 | 2018-06-21 17:53:15 +0100 | [diff] [blame] | 24 | #define OFF 0x3fe6955500000000 |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 25 | |
Szabolcs Nagy | 58ce45c | 2018-06-29 11:06:13 +0100 | [diff] [blame] | 26 | /* Top 12 bits of a double (sign and exponent bits). */ |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 27 | static inline uint32_t |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 28 | top12 (double x) |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 29 | { |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 30 | return asuint64 (x) >> 52; |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 31 | } |
| 32 | |
Szabolcs Nagy | ae8bc7d | 2018-07-09 17:42:10 +0100 | [diff] [blame] | 33 | /* Compute y+TAIL = log(x) where the rounded result is y and TAIL has about |
| 34 | additional 15 bits precision. IX is the bit representation of x, but |
| 35 | normalized in the subnormal range using the sign bit for the exponent. */ |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 36 | static inline double_t |
| 37 | log_inline (uint64_t ix, double_t *tail) |
| 38 | { |
| 39 | /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ |
Szabolcs Nagy | a623032 | 2018-06-21 17:53:15 +0100 | [diff] [blame] | 40 | double_t z, r, y, invc, logc, logctail, kd, hi, t1, t2, lo, lo1, lo2, p; |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 41 | uint64_t iz, tmp; |
| 42 | int k, i; |
| 43 | |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 44 | /* x = 2^k z; where z is in range [OFF,2*OFF) and exact. |
| 45 | The range is split into N subintervals. |
| 46 | The ith subinterval contains z and c is near its center. */ |
| 47 | tmp = ix - OFF; |
| 48 | i = (tmp >> (52 - POW_LOG_TABLE_BITS)) % N; |
| 49 | k = (int64_t) tmp >> 52; /* arithmetic shift */ |
| 50 | iz = ix - (tmp & 0xfffULL << 52); |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 51 | z = asdouble (iz); |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 52 | kd = (double_t) k; |
| 53 | |
Szabolcs Nagy | a623032 | 2018-06-21 17:53:15 +0100 | [diff] [blame] | 54 | /* log(x) = k*Ln2 + log(c) + log1p(z/c-1). */ |
| 55 | invc = T[i].invc; |
| 56 | logc = T[i].logc; |
| 57 | logctail = T[i].logctail; |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 58 | |
Szabolcs Nagy | 2105bad | 2018-07-03 11:35:57 +0100 | [diff] [blame] | 59 | /* Note: 1/c is j/N or j/N/2 where j is an integer in [N,2N) and |
| 60 | |z/c - 1| < 1/N, so r = z/c - 1 is exactly representible. */ |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 61 | #if HAVE_FAST_FMA |
| 62 | r = fma (z, invc, -1.0); |
| 63 | #else |
Szabolcs Nagy | 2105bad | 2018-07-03 11:35:57 +0100 | [diff] [blame] | 64 | /* Split z such that rhi, rlo and rhi*rhi are exact and |rlo| <= |r|. */ |
| 65 | double_t zhi = asdouble ((iz + (1ULL << 31)) & (-1ULL << 32)); |
Szabolcs Nagy | a623032 | 2018-06-21 17:53:15 +0100 | [diff] [blame] | 66 | double_t zlo = z - zhi; |
| 67 | double_t rhi = zhi * invc - 1.0; |
| 68 | double_t rlo = zlo * invc; |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 69 | r = rhi + rlo; |
| 70 | #endif |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 71 | |
Szabolcs Nagy | a623032 | 2018-06-21 17:53:15 +0100 | [diff] [blame] | 72 | /* k*Ln2 + log(c) + r. */ |
| 73 | t1 = kd * Ln2hi + logc; |
| 74 | t2 = t1 + r; |
| 75 | lo1 = kd * Ln2lo + logctail; |
| 76 | lo2 = t1 - t2 + r; |
| 77 | |
| 78 | /* Evaluation is optimized assuming superscalar pipelined execution. */ |
| 79 | double_t ar, ar2, ar3, lo3, lo4; |
| 80 | ar = A[0] * r; /* A[0] = -0.5. */ |
| 81 | ar2 = r * ar; |
| 82 | ar3 = r * ar2; |
| 83 | /* k*Ln2 + log(c) + r + A[0]*r*r. */ |
| 84 | #if HAVE_FAST_FMA |
| 85 | hi = t2 + ar2; |
| 86 | lo3 = fma (ar, r, -ar2); |
| 87 | lo4 = t2 - hi + ar2; |
| 88 | #else |
| 89 | double_t arhi = A[0] * rhi; |
| 90 | double_t arhi2 = rhi * arhi; |
| 91 | hi = t2 + arhi2; |
| 92 | lo3 = rlo * (ar + arhi); |
| 93 | lo4 = t2 - hi + arhi2; |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 94 | #endif |
Szabolcs Nagy | a623032 | 2018-06-21 17:53:15 +0100 | [diff] [blame] | 95 | /* p = log1p(r) - r - A[0]*r*r. */ |
| 96 | #if POW_LOG_POLY_ORDER == 8 |
Szabolcs Nagy | bc4b901 | 2018-07-03 18:54:51 +0100 | [diff] [blame] | 97 | p = (ar3 |
| 98 | * (A[1] + r * A[2] + ar2 * (A[3] + r * A[4] + ar2 * (A[5] + r * A[6])))); |
Szabolcs Nagy | a623032 | 2018-06-21 17:53:15 +0100 | [diff] [blame] | 99 | #endif |
| 100 | lo = lo1 + lo2 + lo3 + lo4 + p; |
| 101 | y = hi + lo; |
| 102 | *tail = hi - y + lo; |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 103 | return y; |
| 104 | } |
| 105 | |
| 106 | #undef N |
| 107 | #undef T |
| 108 | #define N (1 << EXP_TABLE_BITS) |
| 109 | #define InvLn2N __exp_data.invln2N |
| 110 | #define NegLn2hiN __exp_data.negln2hiN |
| 111 | #define NegLn2loN __exp_data.negln2loN |
| 112 | #define Shift __exp_data.shift |
| 113 | #define T __exp_data.tab |
| 114 | #define C2 __exp_data.poly[5 - EXP_POLY_ORDER] |
| 115 | #define C3 __exp_data.poly[6 - EXP_POLY_ORDER] |
| 116 | #define C4 __exp_data.poly[7 - EXP_POLY_ORDER] |
| 117 | #define C5 __exp_data.poly[8 - EXP_POLY_ORDER] |
| 118 | #define C6 __exp_data.poly[9 - EXP_POLY_ORDER] |
| 119 | |
Szabolcs Nagy | bc4b901 | 2018-07-03 18:54:51 +0100 | [diff] [blame] | 120 | /* Handle cases that may overflow or underflow when computing the result that |
| 121 | is scale*(1+TMP) without intermediate rounding. The bit representation of |
| 122 | scale is in SBITS, however it has a computed exponent that may have |
| 123 | overflown into the sign bit so that needs to be adjusted before using it as |
| 124 | a double. (int32_t)KI is the k used in the argument reduction and exponent |
| 125 | adjustment of scale, positive k here means the result may overflow and |
| 126 | negative k means the result may underflow. */ |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 127 | static inline double |
| 128 | specialcase (double_t tmp, uint64_t sbits, uint64_t ki) |
| 129 | { |
| 130 | double_t scale, y; |
| 131 | |
| 132 | if ((ki & 0x80000000) == 0) |
| 133 | { |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 134 | /* k > 0, the exponent of scale might have overflowed by <= 460. */ |
| 135 | sbits -= 1009ull << 52; |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 136 | scale = asdouble (sbits); |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 137 | y = 0x1p1009 * (scale + scale * tmp); |
Szabolcs Nagy | 04884bd | 2018-12-07 14:58:51 +0000 | [diff] [blame] | 138 | return check_oflow (eval_as_double (y)); |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 139 | } |
| 140 | /* k < 0, need special care in the subnormal range. */ |
| 141 | sbits += 1022ull << 52; |
| 142 | /* Note: sbits is signed scale. */ |
| 143 | scale = asdouble (sbits); |
| 144 | y = scale + scale * tmp; |
| 145 | if (fabs (y) < 1.0) |
| 146 | { |
| 147 | /* Round y to the right precision before scaling it into the subnormal |
| 148 | range to avoid double rounding that can cause 0.5+E/2 ulp error where |
| 149 | E is the worst-case ulp error outside the subnormal range. So this |
| 150 | is only useful if the goal is better than 1 ulp worst-case error. */ |
| 151 | double_t hi, lo, one = 1.0; |
| 152 | if (y < 0.0) |
| 153 | one = -1.0; |
| 154 | lo = scale - y + scale * tmp; |
| 155 | hi = one + y; |
| 156 | lo = one - hi + y + lo; |
| 157 | y = eval_as_double (hi + lo) - one; |
Szabolcs Nagy | e00696a | 2018-06-19 13:53:40 +0100 | [diff] [blame] | 158 | /* Fix the sign of 0. */ |
| 159 | if (y == 0.0) |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 160 | y = asdouble (sbits & 0x8000000000000000); |
| 161 | /* The underflow exception needs to be signaled explicitly. */ |
Szabolcs Nagy | 5fa69e1 | 2018-06-12 17:18:24 +0100 | [diff] [blame] | 162 | force_eval_double (opt_barrier_double (0x1p-1022) * 0x1p-1022); |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 163 | } |
| 164 | y = 0x1p-1022 * y; |
Szabolcs Nagy | 04884bd | 2018-12-07 14:58:51 +0000 | [diff] [blame] | 165 | return check_uflow (eval_as_double (y)); |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | #define SIGN_BIAS (0x800 << EXP_TABLE_BITS) |
| 169 | |
Szabolcs Nagy | 58ce45c | 2018-06-29 11:06:13 +0100 | [diff] [blame] | 170 | /* Computes sign*exp(x+xtail) where |xtail| < 2^-8/N and |xtail| <= |x|. |
| 171 | The sign_bias argument is SIGN_BIAS or 0 and sets the sign to -1 or 1. */ |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 172 | static inline double |
Szabolcs Nagy | 04884bd | 2018-12-07 14:58:51 +0000 | [diff] [blame] | 173 | exp_inline (double_t x, double_t xtail, uint32_t sign_bias) |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 174 | { |
| 175 | uint32_t abstop; |
| 176 | uint64_t ki, idx, top, sbits; |
| 177 | /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ |
| 178 | double_t kd, z, r, r2, scale, tail, tmp; |
| 179 | |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 180 | abstop = top12 (x) & 0x7ff; |
| 181 | if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54))) |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 182 | { |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 183 | if (abstop - top12 (0x1p-54) >= 0x80000000) |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 184 | { |
| 185 | /* Avoid spurious underflow for tiny x. */ |
| 186 | /* Note: 0 is common input. */ |
| 187 | double_t one = WANT_ROUNDING ? 1.0 + x : 1.0; |
| 188 | return sign_bias ? -one : one; |
| 189 | } |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 190 | if (abstop >= top12 (1024.0)) |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 191 | { |
| 192 | /* Note: inf and nan are already handled. */ |
| 193 | if (asuint64 (x) >> 63) |
| 194 | return __math_uflow (sign_bias); |
| 195 | else |
| 196 | return __math_oflow (sign_bias); |
| 197 | } |
| 198 | /* Large x is special cased below. */ |
| 199 | abstop = 0; |
| 200 | } |
| 201 | |
| 202 | /* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)]. */ |
| 203 | /* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N]. */ |
| 204 | z = InvLn2N * x; |
| 205 | #if TOINT_INTRINSICS |
| 206 | kd = roundtoint (z); |
| 207 | ki = converttoint (z); |
| 208 | #elif EXP_USE_TOINT_NARROW |
| 209 | /* z - kd is in [-0.5-2^-16, 0.5] in all rounding modes. */ |
| 210 | kd = eval_as_double (z + Shift); |
| 211 | ki = asuint64 (kd) >> 16; |
| 212 | kd = (double_t) (int32_t) ki; |
| 213 | #else |
| 214 | /* z - kd is in [-1, 1] in non-nearest rounding modes. */ |
| 215 | kd = eval_as_double (z + Shift); |
| 216 | ki = asuint64 (kd); |
| 217 | kd -= Shift; |
| 218 | #endif |
Szabolcs Nagy | 5e83891 | 2018-06-29 09:56:54 +0100 | [diff] [blame] | 219 | r = x + kd * NegLn2hiN + kd * NegLn2loN; |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 220 | /* The code assumes 2^-200 < |xtail| < 2^-8/N. */ |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 221 | r += xtail; |
| 222 | /* 2^(k/N) ~= scale * (1 + tail). */ |
Szabolcs Nagy | 5e83891 | 2018-06-29 09:56:54 +0100 | [diff] [blame] | 223 | idx = 2 * (ki % N); |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 224 | top = (ki + sign_bias) << (52 - EXP_TABLE_BITS); |
| 225 | tail = asdouble (T[idx]); |
| 226 | /* This is only a valid scale when -1023*N < k < 1024*N. */ |
| 227 | sbits = T[idx + 1] + top; |
| 228 | /* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1). */ |
| 229 | /* Evaluation is optimized assuming superscalar pipelined execution. */ |
Szabolcs Nagy | 5e83891 | 2018-06-29 09:56:54 +0100 | [diff] [blame] | 230 | r2 = r * r; |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 231 | /* Without fma the worst case error is 0.25/N ulp larger. */ |
| 232 | /* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp. */ |
| 233 | #if EXP_POLY_ORDER == 4 |
Szabolcs Nagy | 5e83891 | 2018-06-29 09:56:54 +0100 | [diff] [blame] | 234 | tmp = tail + r + r2 * C2 + r * r2 * (C3 + r * C4); |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 235 | #elif EXP_POLY_ORDER == 5 |
Szabolcs Nagy | 5e83891 | 2018-06-29 09:56:54 +0100 | [diff] [blame] | 236 | tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5); |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 237 | #elif EXP_POLY_ORDER == 6 |
Szabolcs Nagy | 5e83891 | 2018-06-29 09:56:54 +0100 | [diff] [blame] | 238 | tmp = tail + r + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6); |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 239 | #endif |
| 240 | if (unlikely (abstop == 0)) |
| 241 | return specialcase (tmp, sbits, ki); |
| 242 | scale = asdouble (sbits); |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 243 | /* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there |
| 244 | is no spurious underflow here even without fma. */ |
Szabolcs Nagy | 04884bd | 2018-12-07 14:58:51 +0000 | [diff] [blame] | 245 | return eval_as_double (scale + scale * tmp); |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Szabolcs Nagy | e875f40 | 2018-09-18 10:36:04 +0100 | [diff] [blame] | 248 | /* Returns 0 if not int, 1 if odd int, 2 if even int. The argument is |
| 249 | the bit representation of a non-zero finite floating-point value. */ |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 250 | static inline int |
| 251 | checkint (uint64_t iy) |
| 252 | { |
| 253 | int e = iy >> 52 & 0x7ff; |
| 254 | if (e < 0x3ff) |
| 255 | return 0; |
| 256 | if (e > 0x3ff + 52) |
| 257 | return 2; |
| 258 | if (iy & ((1ULL << (0x3ff + 52 - e)) - 1)) |
| 259 | return 0; |
| 260 | if (iy & (1ULL << (0x3ff + 52 - e))) |
| 261 | return 1; |
| 262 | return 2; |
| 263 | } |
| 264 | |
Szabolcs Nagy | 58ce45c | 2018-06-29 11:06:13 +0100 | [diff] [blame] | 265 | /* Returns 1 if input is the bit representation of 0, infinity or nan. */ |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 266 | static inline int |
| 267 | zeroinfnan (uint64_t i) |
| 268 | { |
| 269 | return 2 * i - 1 >= 2 * asuint64 (INFINITY) - 1; |
| 270 | } |
| 271 | |
| 272 | double |
| 273 | pow (double x, double y) |
| 274 | { |
Szabolcs Nagy | db6e4e9 | 2018-06-18 11:03:27 +0100 | [diff] [blame] | 275 | uint32_t sign_bias = 0; |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 276 | uint64_t ix, iy; |
| 277 | uint32_t topx, topy; |
| 278 | |
| 279 | ix = asuint64 (x); |
| 280 | iy = asuint64 (y); |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 281 | topx = top12 (x); |
| 282 | topy = top12 (y); |
Szabolcs Nagy | 76fd080 | 2018-06-22 17:28:45 +0100 | [diff] [blame] | 283 | if (unlikely (topx - 0x001 >= 0x7ff - 0x001 |
| 284 | || (topy & 0x7ff) - 0x3be >= 0x43e - 0x3be)) |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 285 | { |
| 286 | /* Note: if |y| > 1075 * ln2 * 2^53 ~= 0x1.749p62 then pow(x,y) = inf/0 |
| 287 | and if |y| < 2^-54 / 1075 ~= 0x1.e7b6p-65 then pow(x,y) = +-1. */ |
| 288 | /* Special cases: (x < 0x1p-126 or inf or nan) or |
| 289 | (|y| < 0x1p-65 or |y| >= 0x1p63 or nan). */ |
| 290 | if (unlikely (zeroinfnan (iy))) |
| 291 | { |
| 292 | if (2 * iy == 0) |
| 293 | return issignaling_inline (x) ? x + y : 1.0; |
| 294 | if (ix == asuint64 (1.0)) |
| 295 | return issignaling_inline (y) ? x + y : 1.0; |
Szabolcs Nagy | 76fd080 | 2018-06-22 17:28:45 +0100 | [diff] [blame] | 296 | if (2 * ix > 2 * asuint64 (INFINITY) |
| 297 | || 2 * iy > 2 * asuint64 (INFINITY)) |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 298 | return x + y; |
| 299 | if (2 * ix == 2 * asuint64 (1.0)) |
| 300 | return 1.0; |
| 301 | if ((2 * ix < 2 * asuint64 (1.0)) == !(iy >> 63)) |
| 302 | return 0.0; /* |x|<1 && y==inf or |x|>1 && y==-inf. */ |
| 303 | return y * y; |
| 304 | } |
| 305 | if (unlikely (zeroinfnan (ix))) |
| 306 | { |
| 307 | double_t x2 = x * x; |
| 308 | if (ix >> 63 && checkint (iy) == 1) |
| 309 | { |
| 310 | x2 = -x2; |
| 311 | sign_bias = 1; |
| 312 | } |
| 313 | if (WANT_ERRNO && 2 * ix == 0 && iy >> 63) |
| 314 | return __math_divzero (sign_bias); |
Szabolcs Nagy | 5fa69e1 | 2018-06-12 17:18:24 +0100 | [diff] [blame] | 315 | /* Without the barrier some versions of clang hoist the 1/x2 and |
| 316 | thus division by zero exception can be signaled spuriously. */ |
| 317 | return iy >> 63 ? opt_barrier_double (1 / x2) : x2; |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 318 | } |
| 319 | /* Here x and y are non-zero finite. */ |
| 320 | if (ix >> 63) |
| 321 | { |
| 322 | /* Finite x < 0. */ |
| 323 | int yint = checkint (iy); |
| 324 | if (yint == 0) |
| 325 | return __math_invalid (x); |
| 326 | if (yint == 1) |
| 327 | sign_bias = SIGN_BIAS; |
| 328 | ix &= 0x7fffffffffffffff; |
| 329 | topx &= 0x7ff; |
| 330 | } |
| 331 | if ((topy & 0x7ff) - 0x3be >= 0x43e - 0x3be) |
| 332 | { |
| 333 | /* Note: sign_bias == 0 here because y is not odd. */ |
| 334 | if (ix == asuint64 (1.0)) |
| 335 | return 1.0; |
| 336 | if ((topy & 0x7ff) < 0x3be) |
| 337 | { |
| 338 | /* |y| < 2^-65, x^y ~= 1 + y*log(x). */ |
| 339 | if (WANT_ROUNDING) |
| 340 | return ix > asuint64 (1.0) ? 1.0 + y : 1.0 - y; |
| 341 | else |
| 342 | return 1.0; |
| 343 | } |
Szabolcs Nagy | 76fd080 | 2018-06-22 17:28:45 +0100 | [diff] [blame] | 344 | return (ix > asuint64 (1.0)) == (topy < 0x800) ? __math_oflow (0) |
| 345 | : __math_uflow (0); |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 346 | } |
| 347 | if (topx == 0) |
| 348 | { |
| 349 | /* Normalize subnormal x so exponent becomes negative. */ |
Szabolcs Nagy | 2771bc7 | 2020-01-08 12:31:50 +0000 | [diff] [blame] | 350 | /* Without the barrier some versions of clang evalutate the mul |
| 351 | unconditionally causing spurious overflow exceptions. */ |
| 352 | ix = asuint64 (opt_barrier_double (x) * 0x1p52); |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 353 | ix &= 0x7fffffffffffffff; |
| 354 | ix -= 52ULL << 52; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | double_t lo; |
| 359 | double_t hi = log_inline (ix, &lo); |
| 360 | double_t ehi, elo; |
| 361 | #if HAVE_FAST_FMA |
Szabolcs Nagy | 76fd080 | 2018-06-22 17:28:45 +0100 | [diff] [blame] | 362 | ehi = y * hi; |
| 363 | elo = y * lo + fma (y, hi, -ehi); |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 364 | #else |
| 365 | double_t yhi = asdouble (iy & -1ULL << 27); |
| 366 | double_t ylo = y - yhi; |
| 367 | double_t lhi = asdouble (asuint64 (hi) & -1ULL << 27); |
| 368 | double_t llo = hi - lhi + lo; |
Szabolcs Nagy | 76fd080 | 2018-06-22 17:28:45 +0100 | [diff] [blame] | 369 | ehi = yhi * lhi; |
| 370 | elo = ylo * lhi + y * llo; /* |elo| < |ehi| * 2^-25. */ |
Szabolcs Nagy | ed0ecff | 2018-06-06 18:17:16 +0100 | [diff] [blame] | 371 | #endif |
| 372 | return exp_inline (ehi, elo, sign_bias); |
| 373 | } |
Szabolcs Nagy | b7d568d | 2018-06-06 12:26:56 +0100 | [diff] [blame] | 374 | #if USE_GLIBC_ABI |
| 375 | strong_alias (pow, __pow_finite) |
| 376 | hidden_alias (pow, __ieee754_pow) |
Szabolcs Nagy | f934c86 | 2019-08-28 15:15:15 +0100 | [diff] [blame] | 377 | # if LDBL_MANT_DIG == 53 |
| 378 | long double powl (long double x, long double y) { return pow (x, y); } |
| 379 | # endif |
Szabolcs Nagy | b7d568d | 2018-06-06 12:26:56 +0100 | [diff] [blame] | 380 | #endif |