xref: /freebsd/contrib/arm-optimized-routines/math/pow.c (revision 2a58b312b62f908ec92311d1bd8536dbaeb8e55b)
1 /*
2  * Double-precision x^y function.
3  *
4  * Copyright (c) 2018-2020, Arm Limited.
5  * SPDX-License-Identifier: MIT
6  */
7 
8 #include <float.h>
9 #include <math.h>
10 #include <stdint.h>
11 #include "math_config.h"
12 
13 /*
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)
17 */
18 
19 #define T __pow_log_data.tab
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)
24 #define OFF 0x3fe6955500000000
25 
26 /* Top 12 bits of a double (sign and exponent bits).  */
27 static inline uint32_t
28 top12 (double x)
29 {
30   return asuint64 (x) >> 52;
31 }
32 
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.  */
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.  */
40   double_t z, r, y, invc, logc, logctail, kd, hi, t1, t2, lo, lo1, lo2, p;
41   uint64_t iz, tmp;
42   int k, i;
43 
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);
51   z = asdouble (iz);
52   kd = (double_t) k;
53 
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;
58 
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.  */
61 #if HAVE_FAST_FMA
62   r = fma (z, invc, -1.0);
63 #else
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));
66   double_t zlo = z - zhi;
67   double_t rhi = zhi * invc - 1.0;
68   double_t rlo = zlo * invc;
69   r = rhi + rlo;
70 #endif
71 
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;
94 #endif
95   /* p = log1p(r) - r - A[0]*r*r.  */
96 #if POW_LOG_POLY_ORDER == 8
97   p = (ar3
98        * (A[1] + r * A[2] + ar2 * (A[3] + r * A[4] + ar2 * (A[5] + r * A[6]))));
99 #endif
100   lo = lo1 + lo2 + lo3 + lo4 + p;
101   y = hi + lo;
102   *tail = hi - y + lo;
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 
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.  */
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     {
134       /* k > 0, the exponent of scale might have overflowed by <= 460.  */
135       sbits -= 1009ull << 52;
136       scale = asdouble (sbits);
137       y = 0x1p1009 * (scale + scale * tmp);
138       return check_oflow (eval_as_double (y));
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;
158       /* Fix the sign of 0.  */
159       if (y == 0.0)
160 	y = asdouble (sbits & 0x8000000000000000);
161       /* The underflow exception needs to be signaled explicitly.  */
162       force_eval_double (opt_barrier_double (0x1p-1022) * 0x1p-1022);
163     }
164   y = 0x1p-1022 * y;
165   return check_uflow (eval_as_double (y));
166 }
167 
168 #define SIGN_BIAS (0x800 << EXP_TABLE_BITS)
169 
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.  */
172 static inline double
173 exp_inline (double_t x, double_t xtail, uint32_t sign_bias)
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 
180   abstop = top12 (x) & 0x7ff;
181   if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54)))
182     {
183       if (abstop - top12 (0x1p-54) >= 0x80000000)
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 	}
190       if (abstop >= top12 (1024.0))
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
219   r = x + kd * NegLn2hiN + kd * NegLn2loN;
220   /* The code assumes 2^-200 < |xtail| < 2^-8/N.  */
221   r += xtail;
222   /* 2^(k/N) ~= scale * (1 + tail).  */
223   idx = 2 * (ki % N);
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.  */
230   r2 = r * r;
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
234   tmp = tail + r + r2 * C2 + r * r2 * (C3 + r * C4);
235 #elif EXP_POLY_ORDER == 5
236   tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
237 #elif EXP_POLY_ORDER == 6
238   tmp = tail + r + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6);
239 #endif
240   if (unlikely (abstop == 0))
241     return specialcase (tmp, sbits, ki);
242   scale = asdouble (sbits);
243   /* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there
244      is no spurious underflow here even without fma.  */
245   return eval_as_double (scale + scale * tmp);
246 }
247 
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.  */
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 
265 /* Returns 1 if input is the bit representation of 0, infinity or nan.  */
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 {
275   uint32_t sign_bias = 0;
276   uint64_t ix, iy;
277   uint32_t topx, topy;
278 
279   ix = asuint64 (x);
280   iy = asuint64 (y);
281   topx = top12 (x);
282   topy = top12 (y);
283   if (unlikely (topx - 0x001 >= 0x7ff - 0x001
284 		|| (topy & 0x7ff) - 0x3be >= 0x43e - 0x3be))
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;
296 	  if (2 * ix > 2 * asuint64 (INFINITY)
297 	      || 2 * iy > 2 * asuint64 (INFINITY))
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);
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;
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 	    }
344 	  return (ix > asuint64 (1.0)) == (topy < 0x800) ? __math_oflow (0)
345 							 : __math_uflow (0);
346 	}
347       if (topx == 0)
348 	{
349 	  /* Normalize subnormal x so exponent becomes negative.  */
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);
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
362   ehi = y * hi;
363   elo = y * lo + fma (y, hi, -ehi);
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;
369   ehi = yhi * lhi;
370   elo = ylo * lhi + y * llo; /* |elo| < |ehi| * 2^-25.  */
371 #endif
372   return exp_inline (ehi, elo, sign_bias);
373 }
374 #if USE_GLIBC_ABI
375 strong_alias (pow, __pow_finite)
376 hidden_alias (pow, __ieee754_pow)
377 # if LDBL_MANT_DIG == 53
378 long double powl (long double x, long double y) { return pow (x, y); }
379 # endif
380 #endif
381