blob: d39150975225ab39056bc70563481ad441b84b93 [file] [log] [blame]
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +01001/*
2 * Double-precision x^y function.
3 *
4 * Copyright (c) 2018, Arm Limited.
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#include <math.h>
21#include <stdint.h>
22#include "math_config.h"
23
24/*
Szabolcs Nagy5049bfa2018-06-25 12:33:56 +010025Worst-case error: 0.54 ULP (~= ulperr_exp + 1024*Ln2*relerr_log*2^53)
26relerr_log: 1.3 * 2^-68 (Relative error of log, 1.5 * 2^-68 without fma)
27ulperr_exp: 0.509 ULP (ULP error of exp, 0.511 ULP without fma)
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +010028*/
29
30#define T __pow_log_data.tab
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +010031#define A __pow_log_data.poly
32#define Ln2hi __pow_log_data.ln2hi
33#define Ln2lo __pow_log_data.ln2lo
34#define N (1 << POW_LOG_TABLE_BITS)
Szabolcs Nagya6230322018-06-21 17:53:15 +010035#define OFF 0x3fe6955500000000
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +010036
Szabolcs Nagy58ce45c2018-06-29 11:06:13 +010037/* Top 12 bits of a double (sign and exponent bits). */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +010038static inline uint32_t
Szabolcs Nagy2117b832018-06-14 10:54:06 +010039top12 (double x)
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +010040{
Szabolcs Nagy2117b832018-06-14 10:54:06 +010041 return asuint64 (x) >> 52;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +010042}
43
Szabolcs Nagy58ce45c2018-06-29 11:06:13 +010044/* Compute y+tail = log(x) where the rounded result is y and tail has about
45 additional 15 bits precision. The bit representation of x if in ix, but
46 normalized in the subnormal range using sign bit too for the exponent. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +010047static inline double_t
48log_inline (uint64_t ix, double_t *tail)
49{
50 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
Szabolcs Nagya6230322018-06-21 17:53:15 +010051 double_t z, r, y, invc, logc, logctail, kd, hi, t1, t2, lo, lo1, lo2, p;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +010052 uint64_t iz, tmp;
53 int k, i;
54
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +010055 /* x = 2^k z; where z is in range [OFF,2*OFF) and exact.
56 The range is split into N subintervals.
57 The ith subinterval contains z and c is near its center. */
58 tmp = ix - OFF;
59 i = (tmp >> (52 - POW_LOG_TABLE_BITS)) % N;
60 k = (int64_t) tmp >> 52; /* arithmetic shift */
61 iz = ix - (tmp & 0xfffULL << 52);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +010062 z = asdouble (iz);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +010063 kd = (double_t) k;
64
Szabolcs Nagya6230322018-06-21 17:53:15 +010065 /* log(x) = k*Ln2 + log(c) + log1p(z/c-1). */
66 invc = T[i].invc;
67 logc = T[i].logc;
68 logctail = T[i].logctail;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +010069
Szabolcs Nagya6230322018-06-21 17:53:15 +010070 /* r = z/c - 1, arranged to be exact. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +010071#if HAVE_FAST_FMA
72 r = fma (z, invc, -1.0);
73#else
Szabolcs Nagya6230322018-06-21 17:53:15 +010074 double_t zhi = asdouble (iz & (-1ULL << 32));
75 double_t zlo = z - zhi;
76 double_t rhi = zhi * invc - 1.0;
77 double_t rlo = zlo * invc;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +010078 r = rhi + rlo;
79#endif
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +010080
Szabolcs Nagya6230322018-06-21 17:53:15 +010081 /* k*Ln2 + log(c) + r. */
82 t1 = kd * Ln2hi + logc;
83 t2 = t1 + r;
84 lo1 = kd * Ln2lo + logctail;
85 lo2 = t1 - t2 + r;
86
87 /* Evaluation is optimized assuming superscalar pipelined execution. */
88 double_t ar, ar2, ar3, lo3, lo4;
89 ar = A[0] * r; /* A[0] = -0.5. */
90 ar2 = r * ar;
91 ar3 = r * ar2;
92 /* k*Ln2 + log(c) + r + A[0]*r*r. */
93#if HAVE_FAST_FMA
94 hi = t2 + ar2;
95 lo3 = fma (ar, r, -ar2);
96 lo4 = t2 - hi + ar2;
97#else
98 double_t arhi = A[0] * rhi;
99 double_t arhi2 = rhi * arhi;
100 hi = t2 + arhi2;
101 lo3 = rlo * (ar + arhi);
102 lo4 = t2 - hi + arhi2;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100103#endif
Szabolcs Nagya6230322018-06-21 17:53:15 +0100104 /* p = log1p(r) - r - A[0]*r*r. */
105#if POW_LOG_POLY_ORDER == 8
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100106 p = ar3
107 * (A[1] + r * A[2] + ar2 * (A[3] + r * A[4] + ar2 * (A[5] + r * A[6])));
Szabolcs Nagya6230322018-06-21 17:53:15 +0100108#endif
109 lo = lo1 + lo2 + lo3 + lo4 + p;
110 y = hi + lo;
111 *tail = hi - y + lo;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100112 return y;
113}
114
115#undef N
116#undef T
117#define N (1 << EXP_TABLE_BITS)
118#define InvLn2N __exp_data.invln2N
119#define NegLn2hiN __exp_data.negln2hiN
120#define NegLn2loN __exp_data.negln2loN
121#define Shift __exp_data.shift
122#define T __exp_data.tab
123#define C2 __exp_data.poly[5 - EXP_POLY_ORDER]
124#define C3 __exp_data.poly[6 - EXP_POLY_ORDER]
125#define C4 __exp_data.poly[7 - EXP_POLY_ORDER]
126#define C5 __exp_data.poly[8 - EXP_POLY_ORDER]
127#define C6 __exp_data.poly[9 - EXP_POLY_ORDER]
128
Szabolcs Nagy58ce45c2018-06-29 11:06:13 +0100129/* Handle inputs that may overflow or underflow when computing the result
130 that is scale*(1+tmp), the exponent bits of scale might have overflown
131 into the sign bit so that needs correction before sbits is used as a
132 double, ki is only used to determine the sign of the exponent. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100133static inline double
134specialcase (double_t tmp, uint64_t sbits, uint64_t ki)
135{
136 double_t scale, y;
137
138 if ((ki & 0x80000000) == 0)
139 {
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100140 /* k > 0, the exponent of scale might have overflowed by <= 460. */
141 sbits -= 1009ull << 52;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100142 scale = asdouble (sbits);
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100143 y = 0x1p1009 * (scale + scale * tmp);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100144 return check_oflow (y);
145 }
146 /* k < 0, need special care in the subnormal range. */
147 sbits += 1022ull << 52;
148 /* Note: sbits is signed scale. */
149 scale = asdouble (sbits);
150 y = scale + scale * tmp;
151 if (fabs (y) < 1.0)
152 {
153 /* Round y to the right precision before scaling it into the subnormal
154 range to avoid double rounding that can cause 0.5+E/2 ulp error where
155 E is the worst-case ulp error outside the subnormal range. So this
156 is only useful if the goal is better than 1 ulp worst-case error. */
157 double_t hi, lo, one = 1.0;
158 if (y < 0.0)
159 one = -1.0;
160 lo = scale - y + scale * tmp;
161 hi = one + y;
162 lo = one - hi + y + lo;
163 y = eval_as_double (hi + lo) - one;
Szabolcs Nagye00696a2018-06-19 13:53:40 +0100164 /* Fix the sign of 0. */
165 if (y == 0.0)
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100166 y = asdouble (sbits & 0x8000000000000000);
167 /* The underflow exception needs to be signaled explicitly. */
Szabolcs Nagy5fa69e12018-06-12 17:18:24 +0100168 force_eval_double (opt_barrier_double (0x1p-1022) * 0x1p-1022);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100169 }
170 y = 0x1p-1022 * y;
171 return check_uflow (y);
172}
173
174#define SIGN_BIAS (0x800 << EXP_TABLE_BITS)
175
Szabolcs Nagy58ce45c2018-06-29 11:06:13 +0100176/* Computes sign*exp(x+xtail) where |xtail| < 2^-8/N and |xtail| <= |x|.
177 The sign_bias argument is SIGN_BIAS or 0 and sets the sign to -1 or 1. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100178static inline double
179exp_inline (double x, double xtail, uint32_t sign_bias)
180{
181 uint32_t abstop;
182 uint64_t ki, idx, top, sbits;
183 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
184 double_t kd, z, r, r2, scale, tail, tmp;
185
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100186 abstop = top12 (x) & 0x7ff;
187 if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54)))
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100188 {
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100189 if (abstop - top12 (0x1p-54) >= 0x80000000)
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100190 {
191 /* Avoid spurious underflow for tiny x. */
192 /* Note: 0 is common input. */
193 double_t one = WANT_ROUNDING ? 1.0 + x : 1.0;
194 return sign_bias ? -one : one;
195 }
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100196 if (abstop >= top12 (1024.0))
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100197 {
198 /* Note: inf and nan are already handled. */
199 if (asuint64 (x) >> 63)
200 return __math_uflow (sign_bias);
201 else
202 return __math_oflow (sign_bias);
203 }
204 /* Large x is special cased below. */
205 abstop = 0;
206 }
207
208 /* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)]. */
209 /* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N]. */
210 z = InvLn2N * x;
211#if TOINT_INTRINSICS
212 kd = roundtoint (z);
213 ki = converttoint (z);
214#elif EXP_USE_TOINT_NARROW
215 /* z - kd is in [-0.5-2^-16, 0.5] in all rounding modes. */
216 kd = eval_as_double (z + Shift);
217 ki = asuint64 (kd) >> 16;
218 kd = (double_t) (int32_t) ki;
219#else
220 /* z - kd is in [-1, 1] in non-nearest rounding modes. */
221 kd = eval_as_double (z + Shift);
222 ki = asuint64 (kd);
223 kd -= Shift;
224#endif
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100225 r = x + kd * NegLn2hiN + kd * NegLn2loN;
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100226 /* The code assumes 2^-200 < |xtail| < 2^-8/N. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100227 r += xtail;
228 /* 2^(k/N) ~= scale * (1 + tail). */
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100229 idx = 2 * (ki % N);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100230 top = (ki + sign_bias) << (52 - EXP_TABLE_BITS);
231 tail = asdouble (T[idx]);
232 /* This is only a valid scale when -1023*N < k < 1024*N. */
233 sbits = T[idx + 1] + top;
234 /* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1). */
235 /* Evaluation is optimized assuming superscalar pipelined execution. */
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100236 r2 = r * r;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100237 /* Without fma the worst case error is 0.25/N ulp larger. */
238 /* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp. */
239#if EXP_POLY_ORDER == 4
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100240 tmp = tail + r + r2 * C2 + r * r2 * (C3 + r * C4);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100241#elif EXP_POLY_ORDER == 5
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100242 tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100243#elif EXP_POLY_ORDER == 6
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100244 tmp = tail + r + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100245#endif
246 if (unlikely (abstop == 0))
247 return specialcase (tmp, sbits, ki);
248 scale = asdouble (sbits);
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100249 /* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there
250 is no spurious underflow here even without fma. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100251 return scale + scale * tmp;
252}
253
254/* Returns 0 if not int, 1 if odd int, 2 if even int. */
255static inline int
256checkint (uint64_t iy)
257{
258 int e = iy >> 52 & 0x7ff;
259 if (e < 0x3ff)
260 return 0;
261 if (e > 0x3ff + 52)
262 return 2;
263 if (iy & ((1ULL << (0x3ff + 52 - e)) - 1))
264 return 0;
265 if (iy & (1ULL << (0x3ff + 52 - e)))
266 return 1;
267 return 2;
268}
269
Szabolcs Nagy58ce45c2018-06-29 11:06:13 +0100270/* Returns 1 if input is the bit representation of 0, infinity or nan. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100271static inline int
272zeroinfnan (uint64_t i)
273{
274 return 2 * i - 1 >= 2 * asuint64 (INFINITY) - 1;
275}
276
277double
278pow (double x, double y)
279{
Szabolcs Nagydb6e4e92018-06-18 11:03:27 +0100280 uint32_t sign_bias = 0;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100281 uint64_t ix, iy;
282 uint32_t topx, topy;
283
284 ix = asuint64 (x);
285 iy = asuint64 (y);
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100286 topx = top12 (x);
287 topy = top12 (y);
Szabolcs Nagy76fd0802018-06-22 17:28:45 +0100288 if (unlikely (topx - 0x001 >= 0x7ff - 0x001
289 || (topy & 0x7ff) - 0x3be >= 0x43e - 0x3be))
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100290 {
291 /* Note: if |y| > 1075 * ln2 * 2^53 ~= 0x1.749p62 then pow(x,y) = inf/0
292 and if |y| < 2^-54 / 1075 ~= 0x1.e7b6p-65 then pow(x,y) = +-1. */
293 /* Special cases: (x < 0x1p-126 or inf or nan) or
294 (|y| < 0x1p-65 or |y| >= 0x1p63 or nan). */
295 if (unlikely (zeroinfnan (iy)))
296 {
297 if (2 * iy == 0)
298 return issignaling_inline (x) ? x + y : 1.0;
299 if (ix == asuint64 (1.0))
300 return issignaling_inline (y) ? x + y : 1.0;
Szabolcs Nagy76fd0802018-06-22 17:28:45 +0100301 if (2 * ix > 2 * asuint64 (INFINITY)
302 || 2 * iy > 2 * asuint64 (INFINITY))
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100303 return x + y;
304 if (2 * ix == 2 * asuint64 (1.0))
305 return 1.0;
306 if ((2 * ix < 2 * asuint64 (1.0)) == !(iy >> 63))
307 return 0.0; /* |x|<1 && y==inf or |x|>1 && y==-inf. */
308 return y * y;
309 }
310 if (unlikely (zeroinfnan (ix)))
311 {
312 double_t x2 = x * x;
313 if (ix >> 63 && checkint (iy) == 1)
314 {
315 x2 = -x2;
316 sign_bias = 1;
317 }
318 if (WANT_ERRNO && 2 * ix == 0 && iy >> 63)
319 return __math_divzero (sign_bias);
Szabolcs Nagy5fa69e12018-06-12 17:18:24 +0100320 /* Without the barrier some versions of clang hoist the 1/x2 and
321 thus division by zero exception can be signaled spuriously. */
322 return iy >> 63 ? opt_barrier_double (1 / x2) : x2;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100323 }
324 /* Here x and y are non-zero finite. */
325 if (ix >> 63)
326 {
327 /* Finite x < 0. */
328 int yint = checkint (iy);
329 if (yint == 0)
330 return __math_invalid (x);
331 if (yint == 1)
332 sign_bias = SIGN_BIAS;
333 ix &= 0x7fffffffffffffff;
334 topx &= 0x7ff;
335 }
336 if ((topy & 0x7ff) - 0x3be >= 0x43e - 0x3be)
337 {
338 /* Note: sign_bias == 0 here because y is not odd. */
339 if (ix == asuint64 (1.0))
340 return 1.0;
341 if ((topy & 0x7ff) < 0x3be)
342 {
343 /* |y| < 2^-65, x^y ~= 1 + y*log(x). */
344 if (WANT_ROUNDING)
345 return ix > asuint64 (1.0) ? 1.0 + y : 1.0 - y;
346 else
347 return 1.0;
348 }
Szabolcs Nagy76fd0802018-06-22 17:28:45 +0100349 return (ix > asuint64 (1.0)) == (topy < 0x800) ? __math_oflow (0)
350 : __math_uflow (0);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100351 }
352 if (topx == 0)
353 {
354 /* Normalize subnormal x so exponent becomes negative. */
355 ix = asuint64 (x * 0x1p52);
356 ix &= 0x7fffffffffffffff;
357 ix -= 52ULL << 52;
358 }
359 }
360
361 double_t lo;
362 double_t hi = log_inline (ix, &lo);
363 double_t ehi, elo;
364#if HAVE_FAST_FMA
Szabolcs Nagy76fd0802018-06-22 17:28:45 +0100365 ehi = y * hi;
366 elo = y * lo + fma (y, hi, -ehi);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100367#else
368 double_t yhi = asdouble (iy & -1ULL << 27);
369 double_t ylo = y - yhi;
370 double_t lhi = asdouble (asuint64 (hi) & -1ULL << 27);
371 double_t llo = hi - lhi + lo;
Szabolcs Nagy76fd0802018-06-22 17:28:45 +0100372 ehi = yhi * lhi;
373 elo = ylo * lhi + y * llo; /* |elo| < |ehi| * 2^-25. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100374#endif
375 return exp_inline (ehi, elo, sign_bias);
376}
Szabolcs Nagyb7d568d2018-06-06 12:26:56 +0100377#if USE_GLIBC_ABI
378strong_alias (pow, __pow_finite)
379hidden_alias (pow, __ieee754_pow)
380#endif