blob: ec9bc37812d8decb026c353f6bd28693bdba7279 [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 Nagy2105bad2018-07-03 11:35:57 +010070 /* Note: 1/c is j/N or j/N/2 where j is an integer in [N,2N) and
71 |z/c - 1| < 1/N, so r = z/c - 1 is exactly representible. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +010072#if HAVE_FAST_FMA
73 r = fma (z, invc, -1.0);
74#else
Szabolcs Nagy2105bad2018-07-03 11:35:57 +010075 /* Split z such that rhi, rlo and rhi*rhi are exact and |rlo| <= |r|. */
76 double_t zhi = asdouble ((iz + (1ULL << 31)) & (-1ULL << 32));
Szabolcs Nagya6230322018-06-21 17:53:15 +010077 double_t zlo = z - zhi;
78 double_t rhi = zhi * invc - 1.0;
79 double_t rlo = zlo * invc;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +010080 r = rhi + rlo;
81#endif
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +010082
Szabolcs Nagya6230322018-06-21 17:53:15 +010083 /* k*Ln2 + log(c) + r. */
84 t1 = kd * Ln2hi + logc;
85 t2 = t1 + r;
86 lo1 = kd * Ln2lo + logctail;
87 lo2 = t1 - t2 + r;
88
89 /* Evaluation is optimized assuming superscalar pipelined execution. */
90 double_t ar, ar2, ar3, lo3, lo4;
91 ar = A[0] * r; /* A[0] = -0.5. */
92 ar2 = r * ar;
93 ar3 = r * ar2;
94 /* k*Ln2 + log(c) + r + A[0]*r*r. */
95#if HAVE_FAST_FMA
96 hi = t2 + ar2;
97 lo3 = fma (ar, r, -ar2);
98 lo4 = t2 - hi + ar2;
99#else
100 double_t arhi = A[0] * rhi;
101 double_t arhi2 = rhi * arhi;
102 hi = t2 + arhi2;
103 lo3 = rlo * (ar + arhi);
104 lo4 = t2 - hi + arhi2;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100105#endif
Szabolcs Nagya6230322018-06-21 17:53:15 +0100106 /* p = log1p(r) - r - A[0]*r*r. */
107#if POW_LOG_POLY_ORDER == 8
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100108 p = ar3
109 * (A[1] + r * A[2] + ar2 * (A[3] + r * A[4] + ar2 * (A[5] + r * A[6])));
Szabolcs Nagya6230322018-06-21 17:53:15 +0100110#endif
111 lo = lo1 + lo2 + lo3 + lo4 + p;
112 y = hi + lo;
113 *tail = hi - y + lo;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100114 return y;
115}
116
117#undef N
118#undef T
119#define N (1 << EXP_TABLE_BITS)
120#define InvLn2N __exp_data.invln2N
121#define NegLn2hiN __exp_data.negln2hiN
122#define NegLn2loN __exp_data.negln2loN
123#define Shift __exp_data.shift
124#define T __exp_data.tab
125#define C2 __exp_data.poly[5 - EXP_POLY_ORDER]
126#define C3 __exp_data.poly[6 - EXP_POLY_ORDER]
127#define C4 __exp_data.poly[7 - EXP_POLY_ORDER]
128#define C5 __exp_data.poly[8 - EXP_POLY_ORDER]
129#define C6 __exp_data.poly[9 - EXP_POLY_ORDER]
130
Szabolcs Nagy58ce45c2018-06-29 11:06:13 +0100131/* Handle inputs that may overflow or underflow when computing the result
132 that is scale*(1+tmp), the exponent bits of scale might have overflown
133 into the sign bit so that needs correction before sbits is used as a
134 double, ki is only used to determine the sign of the exponent. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100135static inline double
136specialcase (double_t tmp, uint64_t sbits, uint64_t ki)
137{
138 double_t scale, y;
139
140 if ((ki & 0x80000000) == 0)
141 {
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100142 /* k > 0, the exponent of scale might have overflowed by <= 460. */
143 sbits -= 1009ull << 52;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100144 scale = asdouble (sbits);
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100145 y = 0x1p1009 * (scale + scale * tmp);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100146 return check_oflow (y);
147 }
148 /* k < 0, need special care in the subnormal range. */
149 sbits += 1022ull << 52;
150 /* Note: sbits is signed scale. */
151 scale = asdouble (sbits);
152 y = scale + scale * tmp;
153 if (fabs (y) < 1.0)
154 {
155 /* Round y to the right precision before scaling it into the subnormal
156 range to avoid double rounding that can cause 0.5+E/2 ulp error where
157 E is the worst-case ulp error outside the subnormal range. So this
158 is only useful if the goal is better than 1 ulp worst-case error. */
159 double_t hi, lo, one = 1.0;
160 if (y < 0.0)
161 one = -1.0;
162 lo = scale - y + scale * tmp;
163 hi = one + y;
164 lo = one - hi + y + lo;
165 y = eval_as_double (hi + lo) - one;
Szabolcs Nagye00696a2018-06-19 13:53:40 +0100166 /* Fix the sign of 0. */
167 if (y == 0.0)
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100168 y = asdouble (sbits & 0x8000000000000000);
169 /* The underflow exception needs to be signaled explicitly. */
Szabolcs Nagy5fa69e12018-06-12 17:18:24 +0100170 force_eval_double (opt_barrier_double (0x1p-1022) * 0x1p-1022);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100171 }
172 y = 0x1p-1022 * y;
173 return check_uflow (y);
174}
175
176#define SIGN_BIAS (0x800 << EXP_TABLE_BITS)
177
Szabolcs Nagy58ce45c2018-06-29 11:06:13 +0100178/* Computes sign*exp(x+xtail) where |xtail| < 2^-8/N and |xtail| <= |x|.
179 The sign_bias argument is SIGN_BIAS or 0 and sets the sign to -1 or 1. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100180static inline double
181exp_inline (double x, double xtail, uint32_t sign_bias)
182{
183 uint32_t abstop;
184 uint64_t ki, idx, top, sbits;
185 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
186 double_t kd, z, r, r2, scale, tail, tmp;
187
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100188 abstop = top12 (x) & 0x7ff;
189 if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54)))
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100190 {
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100191 if (abstop - top12 (0x1p-54) >= 0x80000000)
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100192 {
193 /* Avoid spurious underflow for tiny x. */
194 /* Note: 0 is common input. */
195 double_t one = WANT_ROUNDING ? 1.0 + x : 1.0;
196 return sign_bias ? -one : one;
197 }
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100198 if (abstop >= top12 (1024.0))
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100199 {
200 /* Note: inf and nan are already handled. */
201 if (asuint64 (x) >> 63)
202 return __math_uflow (sign_bias);
203 else
204 return __math_oflow (sign_bias);
205 }
206 /* Large x is special cased below. */
207 abstop = 0;
208 }
209
210 /* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)]. */
211 /* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N]. */
212 z = InvLn2N * x;
213#if TOINT_INTRINSICS
214 kd = roundtoint (z);
215 ki = converttoint (z);
216#elif EXP_USE_TOINT_NARROW
217 /* z - kd is in [-0.5-2^-16, 0.5] in all rounding modes. */
218 kd = eval_as_double (z + Shift);
219 ki = asuint64 (kd) >> 16;
220 kd = (double_t) (int32_t) ki;
221#else
222 /* z - kd is in [-1, 1] in non-nearest rounding modes. */
223 kd = eval_as_double (z + Shift);
224 ki = asuint64 (kd);
225 kd -= Shift;
226#endif
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100227 r = x + kd * NegLn2hiN + kd * NegLn2loN;
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100228 /* The code assumes 2^-200 < |xtail| < 2^-8/N. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100229 r += xtail;
230 /* 2^(k/N) ~= scale * (1 + tail). */
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100231 idx = 2 * (ki % N);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100232 top = (ki + sign_bias) << (52 - EXP_TABLE_BITS);
233 tail = asdouble (T[idx]);
234 /* This is only a valid scale when -1023*N < k < 1024*N. */
235 sbits = T[idx + 1] + top;
236 /* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1). */
237 /* Evaluation is optimized assuming superscalar pipelined execution. */
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100238 r2 = r * r;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100239 /* Without fma the worst case error is 0.25/N ulp larger. */
240 /* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp. */
241#if EXP_POLY_ORDER == 4
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100242 tmp = tail + r + r2 * C2 + r * r2 * (C3 + r * C4);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100243#elif EXP_POLY_ORDER == 5
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100244 tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100245#elif EXP_POLY_ORDER == 6
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100246 tmp = tail + r + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100247#endif
248 if (unlikely (abstop == 0))
249 return specialcase (tmp, sbits, ki);
250 scale = asdouble (sbits);
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100251 /* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there
252 is no spurious underflow here even without fma. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100253 return scale + scale * tmp;
254}
255
256/* Returns 0 if not int, 1 if odd int, 2 if even int. */
257static inline int
258checkint (uint64_t iy)
259{
260 int e = iy >> 52 & 0x7ff;
261 if (e < 0x3ff)
262 return 0;
263 if (e > 0x3ff + 52)
264 return 2;
265 if (iy & ((1ULL << (0x3ff + 52 - e)) - 1))
266 return 0;
267 if (iy & (1ULL << (0x3ff + 52 - e)))
268 return 1;
269 return 2;
270}
271
Szabolcs Nagy58ce45c2018-06-29 11:06:13 +0100272/* Returns 1 if input is the bit representation of 0, infinity or nan. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100273static inline int
274zeroinfnan (uint64_t i)
275{
276 return 2 * i - 1 >= 2 * asuint64 (INFINITY) - 1;
277}
278
279double
280pow (double x, double y)
281{
Szabolcs Nagydb6e4e92018-06-18 11:03:27 +0100282 uint32_t sign_bias = 0;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100283 uint64_t ix, iy;
284 uint32_t topx, topy;
285
286 ix = asuint64 (x);
287 iy = asuint64 (y);
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100288 topx = top12 (x);
289 topy = top12 (y);
Szabolcs Nagy76fd0802018-06-22 17:28:45 +0100290 if (unlikely (topx - 0x001 >= 0x7ff - 0x001
291 || (topy & 0x7ff) - 0x3be >= 0x43e - 0x3be))
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100292 {
293 /* Note: if |y| > 1075 * ln2 * 2^53 ~= 0x1.749p62 then pow(x,y) = inf/0
294 and if |y| < 2^-54 / 1075 ~= 0x1.e7b6p-65 then pow(x,y) = +-1. */
295 /* Special cases: (x < 0x1p-126 or inf or nan) or
296 (|y| < 0x1p-65 or |y| >= 0x1p63 or nan). */
297 if (unlikely (zeroinfnan (iy)))
298 {
299 if (2 * iy == 0)
300 return issignaling_inline (x) ? x + y : 1.0;
301 if (ix == asuint64 (1.0))
302 return issignaling_inline (y) ? x + y : 1.0;
Szabolcs Nagy76fd0802018-06-22 17:28:45 +0100303 if (2 * ix > 2 * asuint64 (INFINITY)
304 || 2 * iy > 2 * asuint64 (INFINITY))
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100305 return x + y;
306 if (2 * ix == 2 * asuint64 (1.0))
307 return 1.0;
308 if ((2 * ix < 2 * asuint64 (1.0)) == !(iy >> 63))
309 return 0.0; /* |x|<1 && y==inf or |x|>1 && y==-inf. */
310 return y * y;
311 }
312 if (unlikely (zeroinfnan (ix)))
313 {
314 double_t x2 = x * x;
315 if (ix >> 63 && checkint (iy) == 1)
316 {
317 x2 = -x2;
318 sign_bias = 1;
319 }
320 if (WANT_ERRNO && 2 * ix == 0 && iy >> 63)
321 return __math_divzero (sign_bias);
Szabolcs Nagy5fa69e12018-06-12 17:18:24 +0100322 /* Without the barrier some versions of clang hoist the 1/x2 and
323 thus division by zero exception can be signaled spuriously. */
324 return iy >> 63 ? opt_barrier_double (1 / x2) : x2;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100325 }
326 /* Here x and y are non-zero finite. */
327 if (ix >> 63)
328 {
329 /* Finite x < 0. */
330 int yint = checkint (iy);
331 if (yint == 0)
332 return __math_invalid (x);
333 if (yint == 1)
334 sign_bias = SIGN_BIAS;
335 ix &= 0x7fffffffffffffff;
336 topx &= 0x7ff;
337 }
338 if ((topy & 0x7ff) - 0x3be >= 0x43e - 0x3be)
339 {
340 /* Note: sign_bias == 0 here because y is not odd. */
341 if (ix == asuint64 (1.0))
342 return 1.0;
343 if ((topy & 0x7ff) < 0x3be)
344 {
345 /* |y| < 2^-65, x^y ~= 1 + y*log(x). */
346 if (WANT_ROUNDING)
347 return ix > asuint64 (1.0) ? 1.0 + y : 1.0 - y;
348 else
349 return 1.0;
350 }
Szabolcs Nagy76fd0802018-06-22 17:28:45 +0100351 return (ix > asuint64 (1.0)) == (topy < 0x800) ? __math_oflow (0)
352 : __math_uflow (0);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100353 }
354 if (topx == 0)
355 {
356 /* Normalize subnormal x so exponent becomes negative. */
357 ix = asuint64 (x * 0x1p52);
358 ix &= 0x7fffffffffffffff;
359 ix -= 52ULL << 52;
360 }
361 }
362
363 double_t lo;
364 double_t hi = log_inline (ix, &lo);
365 double_t ehi, elo;
366#if HAVE_FAST_FMA
Szabolcs Nagy76fd0802018-06-22 17:28:45 +0100367 ehi = y * hi;
368 elo = y * lo + fma (y, hi, -ehi);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100369#else
370 double_t yhi = asdouble (iy & -1ULL << 27);
371 double_t ylo = y - yhi;
372 double_t lhi = asdouble (asuint64 (hi) & -1ULL << 27);
373 double_t llo = hi - lhi + lo;
Szabolcs Nagy76fd0802018-06-22 17:28:45 +0100374 ehi = yhi * lhi;
375 elo = ylo * lhi + y * llo; /* |elo| < |ehi| * 2^-25. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100376#endif
377 return exp_inline (ehi, elo, sign_bias);
378}
Szabolcs Nagyb7d568d2018-06-06 12:26:56 +0100379#if USE_GLIBC_ABI
380strong_alias (pow, __pow_finite)
381hidden_alias (pow, __ieee754_pow)
382#endif