blob: 9f953e97e842eec7d1e100e4ea3557630b53b02f [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 Nagyae8bc7d2018-07-09 17:42:10 +010044/* Compute y+TAIL = log(x) where the rounded result is y and TAIL has about
45 additional 15 bits precision. IX is the bit representation of x, but
46 normalized in the subnormal range using the sign bit 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 Nagybc4b9012018-07-03 18:54:51 +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 Nagybc4b9012018-07-03 18:54:51 +0100131/* Handle cases that may overflow or underflow when computing the result that
132 is scale*(1+TMP) without intermediate rounding. The bit representation of
133 scale is in SBITS, however it has a computed exponent that may have
134 overflown into the sign bit so that needs to be adjusted before using it as
135 a double. (int32_t)KI is the k used in the argument reduction and exponent
136 adjustment of scale, positive k here means the result may overflow and
137 negative k means the result may underflow. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100138static inline double
139specialcase (double_t tmp, uint64_t sbits, uint64_t ki)
140{
141 double_t scale, y;
142
143 if ((ki & 0x80000000) == 0)
144 {
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100145 /* k > 0, the exponent of scale might have overflowed by <= 460. */
146 sbits -= 1009ull << 52;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100147 scale = asdouble (sbits);
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100148 y = 0x1p1009 * (scale + scale * tmp);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100149 return check_oflow (y);
150 }
151 /* k < 0, need special care in the subnormal range. */
152 sbits += 1022ull << 52;
153 /* Note: sbits is signed scale. */
154 scale = asdouble (sbits);
155 y = scale + scale * tmp;
156 if (fabs (y) < 1.0)
157 {
158 /* Round y to the right precision before scaling it into the subnormal
159 range to avoid double rounding that can cause 0.5+E/2 ulp error where
160 E is the worst-case ulp error outside the subnormal range. So this
161 is only useful if the goal is better than 1 ulp worst-case error. */
162 double_t hi, lo, one = 1.0;
163 if (y < 0.0)
164 one = -1.0;
165 lo = scale - y + scale * tmp;
166 hi = one + y;
167 lo = one - hi + y + lo;
168 y = eval_as_double (hi + lo) - one;
Szabolcs Nagye00696a2018-06-19 13:53:40 +0100169 /* Fix the sign of 0. */
170 if (y == 0.0)
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100171 y = asdouble (sbits & 0x8000000000000000);
172 /* The underflow exception needs to be signaled explicitly. */
Szabolcs Nagy5fa69e12018-06-12 17:18:24 +0100173 force_eval_double (opt_barrier_double (0x1p-1022) * 0x1p-1022);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100174 }
175 y = 0x1p-1022 * y;
176 return check_uflow (y);
177}
178
179#define SIGN_BIAS (0x800 << EXP_TABLE_BITS)
180
Szabolcs Nagy58ce45c2018-06-29 11:06:13 +0100181/* Computes sign*exp(x+xtail) where |xtail| < 2^-8/N and |xtail| <= |x|.
182 The sign_bias argument is SIGN_BIAS or 0 and sets the sign to -1 or 1. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100183static inline double
184exp_inline (double x, double xtail, uint32_t sign_bias)
185{
186 uint32_t abstop;
187 uint64_t ki, idx, top, sbits;
188 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
189 double_t kd, z, r, r2, scale, tail, tmp;
190
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100191 abstop = top12 (x) & 0x7ff;
192 if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54)))
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100193 {
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100194 if (abstop - top12 (0x1p-54) >= 0x80000000)
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100195 {
196 /* Avoid spurious underflow for tiny x. */
197 /* Note: 0 is common input. */
198 double_t one = WANT_ROUNDING ? 1.0 + x : 1.0;
199 return sign_bias ? -one : one;
200 }
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100201 if (abstop >= top12 (1024.0))
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100202 {
203 /* Note: inf and nan are already handled. */
204 if (asuint64 (x) >> 63)
205 return __math_uflow (sign_bias);
206 else
207 return __math_oflow (sign_bias);
208 }
209 /* Large x is special cased below. */
210 abstop = 0;
211 }
212
213 /* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)]. */
214 /* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N]. */
215 z = InvLn2N * x;
216#if TOINT_INTRINSICS
217 kd = roundtoint (z);
218 ki = converttoint (z);
219#elif EXP_USE_TOINT_NARROW
220 /* z - kd is in [-0.5-2^-16, 0.5] in all rounding modes. */
221 kd = eval_as_double (z + Shift);
222 ki = asuint64 (kd) >> 16;
223 kd = (double_t) (int32_t) ki;
224#else
225 /* z - kd is in [-1, 1] in non-nearest rounding modes. */
226 kd = eval_as_double (z + Shift);
227 ki = asuint64 (kd);
228 kd -= Shift;
229#endif
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100230 r = x + kd * NegLn2hiN + kd * NegLn2loN;
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100231 /* The code assumes 2^-200 < |xtail| < 2^-8/N. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100232 r += xtail;
233 /* 2^(k/N) ~= scale * (1 + tail). */
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100234 idx = 2 * (ki % N);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100235 top = (ki + sign_bias) << (52 - EXP_TABLE_BITS);
236 tail = asdouble (T[idx]);
237 /* This is only a valid scale when -1023*N < k < 1024*N. */
238 sbits = T[idx + 1] + top;
239 /* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1). */
240 /* Evaluation is optimized assuming superscalar pipelined execution. */
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100241 r2 = r * r;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100242 /* Without fma the worst case error is 0.25/N ulp larger. */
243 /* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp. */
244#if EXP_POLY_ORDER == 4
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100245 tmp = tail + r + r2 * C2 + r * r2 * (C3 + r * C4);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100246#elif EXP_POLY_ORDER == 5
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100247 tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100248#elif EXP_POLY_ORDER == 6
Szabolcs Nagy5e838912018-06-29 09:56:54 +0100249 tmp = tail + r + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100250#endif
251 if (unlikely (abstop == 0))
252 return specialcase (tmp, sbits, ki);
253 scale = asdouble (sbits);
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100254 /* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there
255 is no spurious underflow here even without fma. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100256 return scale + scale * tmp;
257}
258
259/* Returns 0 if not int, 1 if odd int, 2 if even int. */
260static inline int
261checkint (uint64_t iy)
262{
263 int e = iy >> 52 & 0x7ff;
264 if (e < 0x3ff)
265 return 0;
266 if (e > 0x3ff + 52)
267 return 2;
268 if (iy & ((1ULL << (0x3ff + 52 - e)) - 1))
269 return 0;
270 if (iy & (1ULL << (0x3ff + 52 - e)))
271 return 1;
272 return 2;
273}
274
Szabolcs Nagy58ce45c2018-06-29 11:06:13 +0100275/* Returns 1 if input is the bit representation of 0, infinity or nan. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100276static inline int
277zeroinfnan (uint64_t i)
278{
279 return 2 * i - 1 >= 2 * asuint64 (INFINITY) - 1;
280}
281
282double
283pow (double x, double y)
284{
Szabolcs Nagydb6e4e92018-06-18 11:03:27 +0100285 uint32_t sign_bias = 0;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100286 uint64_t ix, iy;
287 uint32_t topx, topy;
288
289 ix = asuint64 (x);
290 iy = asuint64 (y);
Szabolcs Nagy2117b832018-06-14 10:54:06 +0100291 topx = top12 (x);
292 topy = top12 (y);
Szabolcs Nagy76fd0802018-06-22 17:28:45 +0100293 if (unlikely (topx - 0x001 >= 0x7ff - 0x001
294 || (topy & 0x7ff) - 0x3be >= 0x43e - 0x3be))
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100295 {
296 /* Note: if |y| > 1075 * ln2 * 2^53 ~= 0x1.749p62 then pow(x,y) = inf/0
297 and if |y| < 2^-54 / 1075 ~= 0x1.e7b6p-65 then pow(x,y) = +-1. */
298 /* Special cases: (x < 0x1p-126 or inf or nan) or
299 (|y| < 0x1p-65 or |y| >= 0x1p63 or nan). */
300 if (unlikely (zeroinfnan (iy)))
301 {
302 if (2 * iy == 0)
303 return issignaling_inline (x) ? x + y : 1.0;
304 if (ix == asuint64 (1.0))
305 return issignaling_inline (y) ? x + y : 1.0;
Szabolcs Nagy76fd0802018-06-22 17:28:45 +0100306 if (2 * ix > 2 * asuint64 (INFINITY)
307 || 2 * iy > 2 * asuint64 (INFINITY))
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100308 return x + y;
309 if (2 * ix == 2 * asuint64 (1.0))
310 return 1.0;
311 if ((2 * ix < 2 * asuint64 (1.0)) == !(iy >> 63))
312 return 0.0; /* |x|<1 && y==inf or |x|>1 && y==-inf. */
313 return y * y;
314 }
315 if (unlikely (zeroinfnan (ix)))
316 {
317 double_t x2 = x * x;
318 if (ix >> 63 && checkint (iy) == 1)
319 {
320 x2 = -x2;
321 sign_bias = 1;
322 }
323 if (WANT_ERRNO && 2 * ix == 0 && iy >> 63)
324 return __math_divzero (sign_bias);
Szabolcs Nagy5fa69e12018-06-12 17:18:24 +0100325 /* Without the barrier some versions of clang hoist the 1/x2 and
326 thus division by zero exception can be signaled spuriously. */
327 return iy >> 63 ? opt_barrier_double (1 / x2) : x2;
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100328 }
329 /* Here x and y are non-zero finite. */
330 if (ix >> 63)
331 {
332 /* Finite x < 0. */
333 int yint = checkint (iy);
334 if (yint == 0)
335 return __math_invalid (x);
336 if (yint == 1)
337 sign_bias = SIGN_BIAS;
338 ix &= 0x7fffffffffffffff;
339 topx &= 0x7ff;
340 }
341 if ((topy & 0x7ff) - 0x3be >= 0x43e - 0x3be)
342 {
343 /* Note: sign_bias == 0 here because y is not odd. */
344 if (ix == asuint64 (1.0))
345 return 1.0;
346 if ((topy & 0x7ff) < 0x3be)
347 {
348 /* |y| < 2^-65, x^y ~= 1 + y*log(x). */
349 if (WANT_ROUNDING)
350 return ix > asuint64 (1.0) ? 1.0 + y : 1.0 - y;
351 else
352 return 1.0;
353 }
Szabolcs Nagy76fd0802018-06-22 17:28:45 +0100354 return (ix > asuint64 (1.0)) == (topy < 0x800) ? __math_oflow (0)
355 : __math_uflow (0);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100356 }
357 if (topx == 0)
358 {
359 /* Normalize subnormal x so exponent becomes negative. */
360 ix = asuint64 (x * 0x1p52);
361 ix &= 0x7fffffffffffffff;
362 ix -= 52ULL << 52;
363 }
364 }
365
366 double_t lo;
367 double_t hi = log_inline (ix, &lo);
368 double_t ehi, elo;
369#if HAVE_FAST_FMA
Szabolcs Nagy76fd0802018-06-22 17:28:45 +0100370 ehi = y * hi;
371 elo = y * lo + fma (y, hi, -ehi);
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100372#else
373 double_t yhi = asdouble (iy & -1ULL << 27);
374 double_t ylo = y - yhi;
375 double_t lhi = asdouble (asuint64 (hi) & -1ULL << 27);
376 double_t llo = hi - lhi + lo;
Szabolcs Nagy76fd0802018-06-22 17:28:45 +0100377 ehi = yhi * lhi;
378 elo = ylo * lhi + y * llo; /* |elo| < |ehi| * 2^-25. */
Szabolcs Nagyed0ecff2018-06-06 18:17:16 +0100379#endif
380 return exp_inline (ehi, elo, sign_bias);
381}
Szabolcs Nagyb7d568d2018-06-06 12:26:56 +0100382#if USE_GLIBC_ABI
383strong_alias (pow, __pow_finite)
384hidden_alias (pow, __ieee754_pow)
385#endif