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