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 "libm.h"
11 #include "exp_data.h"
12 #include "pow_data.h"
13 
14 /*
15 Worst-case error: 0.54 ULP (~= ulperr_exp + 1024*Ln2*relerr_log*2^53)
16 relerr_log: 1.3 * 2^-68 (Relative error of log, 1.5 * 2^-68 without fma)
17 ulperr_exp: 0.509 ULP (ULP error of exp, 0.511 ULP without fma)
18 */
19 
20 #define T __pow_log_data.tab
21 #define A __pow_log_data.poly
22 #define Ln2hi __pow_log_data.ln2hi
23 #define Ln2lo __pow_log_data.ln2lo
24 #define N (1 << POW_LOG_TABLE_BITS)
25 #define OFF 0x3fe6955500000000
26 
27 /* Top 12 bits of a double (sign and exponent bits).  */
top12(double x)28 static inline uint32_t 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.  */
log_inline(uint64_t ix,double_t * tail)36 static inline double_t 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 __FP_FAST_FMA
61 	r = __builtin_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 __FP_FAST_FMA
84 	hi = t2 + ar2;
85 	lo3 = __builtin_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 	p = (ar3 * (A[1] + r * A[2] +
96 		    ar2 * (A[3] + r * A[4] + ar2 * (A[5] + r * A[6]))));
97 	lo = lo1 + lo2 + lo3 + lo4 + p;
98 	y = hi + lo;
99 	*tail = hi - y + lo;
100 	return y;
101 }
102 
103 #undef N
104 #undef T
105 #define N (1 << EXP_TABLE_BITS)
106 #define InvLn2N __exp_data.invln2N
107 #define NegLn2hiN __exp_data.negln2hiN
108 #define NegLn2loN __exp_data.negln2loN
109 #define Shift __exp_data.shift
110 #define T __exp_data.tab
111 #define C2 __exp_data.poly[5 - EXP_POLY_ORDER]
112 #define C3 __exp_data.poly[6 - EXP_POLY_ORDER]
113 #define C4 __exp_data.poly[7 - EXP_POLY_ORDER]
114 #define C5 __exp_data.poly[8 - EXP_POLY_ORDER]
115 #define C6 __exp_data.poly[9 - EXP_POLY_ORDER]
116 
117 /* Handle cases that may overflow or underflow when computing the result that
118    is scale*(1+TMP) without intermediate rounding.  The bit representation of
119    scale is in SBITS, however it has a computed exponent that may have
120    overflown into the sign bit so that needs to be adjusted before using it as
121    a double.  (int32_t)KI is the k used in the argument reduction and exponent
122    adjustment of scale, positive k here means the result may overflow and
123    negative k means the result may underflow.  */
specialcase(double_t tmp,uint64_t sbits,uint64_t ki)124 static inline double specialcase(double_t tmp, uint64_t sbits, uint64_t ki)
125 {
126 	double_t scale, y;
127 
128 	if ((ki & 0x80000000) == 0) {
129 		/* k > 0, the exponent of scale might have overflowed by <= 460.  */
130 		sbits -= 1009ull << 52;
131 		scale = asdouble(sbits);
132 		y = 0x1p1009 * (scale + scale * tmp);
133 		return eval_as_double(y);
134 	}
135 	/* k < 0, need special care in the subnormal range.  */
136 	sbits += 1022ull << 52;
137 	/* Note: sbits is signed scale.  */
138 	scale = asdouble(sbits);
139 	y = scale + scale * tmp;
140 	if (fabs(y) < 1.0) {
141 		/* Round y to the right precision before scaling it into the subnormal
142 		   range to avoid double rounding that can cause 0.5+E/2 ulp error where
143 		   E is the worst-case ulp error outside the subnormal range.  So this
144 		   is only useful if the goal is better than 1 ulp worst-case error.  */
145 		double_t hi, lo, one = 1.0;
146 		if (y < 0.0)
147 			one = -1.0;
148 		lo = scale - y + scale * tmp;
149 		hi = one + y;
150 		lo = one - hi + y + lo;
151 		y = eval_as_double(hi + lo) - one;
152 		/* Fix the sign of 0.  */
153 		if (y == 0.0)
154 			y = asdouble(sbits & 0x8000000000000000);
155 		/* The underflow exception needs to be signaled explicitly.  */
156 		fp_force_eval(fp_barrier(0x1p-1022) * 0x1p-1022);
157 	}
158 	y = 0x1p-1022 * y;
159 	return eval_as_double(y);
160 }
161 
162 #define SIGN_BIAS (0x800 << EXP_TABLE_BITS)
163 
164 /* Computes sign*exp(x+xtail) where |xtail| < 2^-8/N and |xtail| <= |x|.
165    The sign_bias argument is SIGN_BIAS or 0 and sets the sign to -1 or 1.  */
exp_inline(double_t x,double_t xtail,uint32_t sign_bias)166 static inline double exp_inline(double_t x, double_t xtail, uint32_t sign_bias)
167 {
168 	uint32_t abstop;
169 	uint64_t ki, idx, top, sbits;
170 	/* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
171 	double_t kd, z, r, r2, scale, tail, tmp;
172 
173 	abstop = top12(x) & 0x7ff;
174 	if (predict_false(abstop - top12(0x1p-54) >=
175 			  top12(512.0) - top12(0x1p-54))) {
176 		if (abstop - top12(0x1p-54) >= 0x80000000) {
177 			/* Avoid spurious underflow for tiny x.  */
178 			/* Note: 0 is common input.  */
179 			double_t one = WANT_ROUNDING ? 1.0 + x : 1.0;
180 			return sign_bias ? -one : one;
181 		}
182 		if (abstop >= top12(1024.0)) {
183 			/* Note: inf and nan are already handled.  */
184 			if (asuint64(x) >> 63)
185 				return __math_uflow(sign_bias);
186 			else
187 				return __math_oflow(sign_bias);
188 		}
189 		/* Large x is special cased below.  */
190 		abstop = 0;
191 	}
192 
193 	/* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)].  */
194 	/* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N].  */
195 	z = InvLn2N * x;
196 #if TOINT_INTRINSICS
197 	kd = roundtoint(z);
198 	ki = converttoint(z);
199 #elif EXP_USE_TOINT_NARROW
200 	/* z - kd is in [-0.5-2^-16, 0.5] in all rounding modes.  */
201 	kd = eval_as_double(z + Shift);
202 	ki = asuint64(kd) >> 16;
203 	kd = (double_t)(int32_t)ki;
204 #else
205 	/* z - kd is in [-1, 1] in non-nearest rounding modes.  */
206 	kd = eval_as_double(z + Shift);
207 	ki = asuint64(kd);
208 	kd -= Shift;
209 #endif
210 	r = x + kd * NegLn2hiN + kd * NegLn2loN;
211 	/* The code assumes 2^-200 < |xtail| < 2^-8/N.  */
212 	r += xtail;
213 	/* 2^(k/N) ~= scale * (1 + tail).  */
214 	idx = 2 * (ki % N);
215 	top = (ki + sign_bias) << (52 - EXP_TABLE_BITS);
216 	tail = asdouble(T[idx]);
217 	/* This is only a valid scale when -1023*N < k < 1024*N.  */
218 	sbits = T[idx + 1] + top;
219 	/* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1).  */
220 	/* Evaluation is optimized assuming superscalar pipelined execution.  */
221 	r2 = r * r;
222 	/* Without fma the worst case error is 0.25/N ulp larger.  */
223 	/* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp.  */
224 	tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
225 	if (predict_false(abstop == 0))
226 		return specialcase(tmp, sbits, ki);
227 	scale = asdouble(sbits);
228 	/* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there
229 	   is no spurious underflow here even without fma.  */
230 	return eval_as_double(scale + scale * tmp);
231 }
232 
233 /* Returns 0 if not int, 1 if odd int, 2 if even int.  The argument is
234    the bit representation of a non-zero finite floating-point value.  */
checkint(uint64_t iy)235 static inline int checkint(uint64_t iy)
236 {
237 	int e = iy >> 52 & 0x7ff;
238 	if (e < 0x3ff)
239 		return 0;
240 	if (e > 0x3ff + 52)
241 		return 2;
242 	if (iy & ((1ULL << (0x3ff + 52 - e)) - 1))
243 		return 0;
244 	if (iy & (1ULL << (0x3ff + 52 - e)))
245 		return 1;
246 	return 2;
247 }
248 
249 /* Returns 1 if input is the bit representation of 0, infinity or nan.  */
zeroinfnan(uint64_t i)250 static inline int zeroinfnan(uint64_t i)
251 {
252 	return 2 * i - 1 >= 2 * asuint64(INFINITY) - 1;
253 }
254 
pow(double x,double y)255 double pow(double x, double y)
256 {
257 	uint32_t sign_bias = 0;
258 	uint64_t ix, iy;
259 	uint32_t topx, topy;
260 
261 	ix = asuint64(x);
262 	iy = asuint64(y);
263 	topx = top12(x);
264 	topy = top12(y);
265 	if (predict_false(topx - 0x001 >= 0x7ff - 0x001 ||
266 			  (topy & 0x7ff) - 0x3be >= 0x43e - 0x3be)) {
267 		/* Note: if |y| > 1075 * ln2 * 2^53 ~= 0x1.749p62 then pow(x,y) = inf/0
268 		   and if |y| < 2^-54 / 1075 ~= 0x1.e7b6p-65 then pow(x,y) = +-1.  */
269 		/* Special cases: (x < 0x1p-126 or inf or nan) or
270 		   (|y| < 0x1p-65 or |y| >= 0x1p63 or nan).  */
271 		if (predict_false(zeroinfnan(iy))) {
272 			if (2 * iy == 0)
273 				return issignaling_inline(x) ? x + y : 1.0;
274 			if (ix == asuint64(1.0))
275 				return issignaling_inline(y) ? x + y : 1.0;
276 			if (2 * ix > 2 * asuint64(INFINITY) ||
277 			    2 * iy > 2 * asuint64(INFINITY))
278 				return x + y;
279 			if (2 * ix == 2 * asuint64(1.0))
280 				return 1.0;
281 			if ((2 * ix < 2 * asuint64(1.0)) == !(iy >> 63))
282 				return 0.0; /* |x|<1 && y==inf or |x|>1 && y==-inf.  */
283 			return y * y;
284 		}
285 		if (predict_false(zeroinfnan(ix))) {
286 			double_t x2 = x * x;
287 			if (ix >> 63 && checkint(iy) == 1)
288 				x2 = -x2;
289 			/* Without the barrier some versions of clang hoist the 1/x2 and
290 			   thus division by zero exception can be signaled spuriously.  */
291 			return iy >> 63 ? fp_barrier(1 / x2) : x2;
292 		}
293 		/* Here x and y are non-zero finite.  */
294 		if (ix >> 63) {
295 			/* Finite x < 0.  */
296 			int yint = checkint(iy);
297 			if (yint == 0)
298 				return __math_invalid(x);
299 			if (yint == 1)
300 				sign_bias = SIGN_BIAS;
301 			ix &= 0x7fffffffffffffff;
302 			topx &= 0x7ff;
303 		}
304 		if ((topy & 0x7ff) - 0x3be >= 0x43e - 0x3be) {
305 			/* Note: sign_bias == 0 here because y is not odd.  */
306 			if (ix == asuint64(1.0))
307 				return 1.0;
308 			if ((topy & 0x7ff) < 0x3be) {
309 				/* |y| < 2^-65, x^y ~= 1 + y*log(x).  */
310 				if (WANT_ROUNDING)
311 					return ix > asuint64(1.0) ? 1.0 + y :
312 								    1.0 - y;
313 				else
314 					return 1.0;
315 			}
316 			return (ix > asuint64(1.0)) == (topy < 0x800) ?
317 				       __math_oflow(0) :
318 				       __math_uflow(0);
319 		}
320 		if (topx == 0) {
321 			/* Normalize subnormal x so exponent becomes negative.  */
322 			ix = asuint64(x * 0x1p52);
323 			ix &= 0x7fffffffffffffff;
324 			ix -= 52ULL << 52;
325 		}
326 	}
327 
328 	double_t lo;
329 	double_t hi = log_inline(ix, &lo);
330 	double_t ehi, elo;
331 #if __FP_FAST_FMA
332 	ehi = y * hi;
333 	elo = y * lo + __builtin_fma(y, hi, -ehi);
334 #else
335 	double_t yhi = asdouble(iy & -1ULL << 27);
336 	double_t ylo = y - yhi;
337 	double_t lhi = asdouble(asuint64(hi) & -1ULL << 27);
338 	double_t llo = hi - lhi + lo;
339 	ehi = yhi * lhi;
340 	elo = ylo * lhi + y * llo; /* |elo| < |ehi| * 2^-25.  */
341 #endif
342 	return exp_inline(ehi, elo, sign_bias);
343 }
344