xref: /haiku/src/system/libroot/posix/musl/math/exp2.c (revision f504f61099b010fbfa94b1cc63d2e9072c7f7185)
1*f504f610SAugustin Cavalier /*
2*f504f610SAugustin Cavalier  * Double-precision 2^x function.
3*f504f610SAugustin Cavalier  *
4*f504f610SAugustin Cavalier  * Copyright (c) 2018, Arm Limited.
5*f504f610SAugustin Cavalier  * SPDX-License-Identifier: MIT
6*f504f610SAugustin Cavalier  */
7*f504f610SAugustin Cavalier 
8*f504f610SAugustin Cavalier #include <math.h>
9*f504f610SAugustin Cavalier #include <stdint.h>
10*f504f610SAugustin Cavalier #include "libm.h"
11*f504f610SAugustin Cavalier #include "exp_data.h"
12*f504f610SAugustin Cavalier 
13*f504f610SAugustin Cavalier #define N (1 << EXP_TABLE_BITS)
14*f504f610SAugustin Cavalier #define Shift __exp_data.exp2_shift
15*f504f610SAugustin Cavalier #define T __exp_data.tab
16*f504f610SAugustin Cavalier #define C1 __exp_data.exp2_poly[0]
17*f504f610SAugustin Cavalier #define C2 __exp_data.exp2_poly[1]
18*f504f610SAugustin Cavalier #define C3 __exp_data.exp2_poly[2]
19*f504f610SAugustin Cavalier #define C4 __exp_data.exp2_poly[3]
20*f504f610SAugustin Cavalier #define C5 __exp_data.exp2_poly[4]
21*f504f610SAugustin Cavalier 
22*f504f610SAugustin Cavalier /* Handle cases that may overflow or underflow when computing the result that
23*f504f610SAugustin Cavalier    is scale*(1+TMP) without intermediate rounding.  The bit representation of
24*f504f610SAugustin Cavalier    scale is in SBITS, however it has a computed exponent that may have
25*f504f610SAugustin Cavalier    overflown into the sign bit so that needs to be adjusted before using it as
26*f504f610SAugustin Cavalier    a double.  (int32_t)KI is the k used in the argument reduction and exponent
27*f504f610SAugustin Cavalier    adjustment of scale, positive k here means the result may overflow and
28*f504f610SAugustin Cavalier    negative k means the result may underflow.  */
specialcase(double_t tmp,uint64_t sbits,uint64_t ki)29*f504f610SAugustin Cavalier static inline double specialcase(double_t tmp, uint64_t sbits, uint64_t ki)
30*f504f610SAugustin Cavalier {
31*f504f610SAugustin Cavalier 	double_t scale, y;
32*f504f610SAugustin Cavalier 
33*f504f610SAugustin Cavalier 	if ((ki & 0x80000000) == 0) {
34*f504f610SAugustin Cavalier 		/* k > 0, the exponent of scale might have overflowed by 1.  */
35*f504f610SAugustin Cavalier 		sbits -= 1ull << 52;
36*f504f610SAugustin Cavalier 		scale = asdouble(sbits);
37*f504f610SAugustin Cavalier 		y = 2 * (scale + scale * tmp);
38*f504f610SAugustin Cavalier 		return eval_as_double(y);
39*f504f610SAugustin Cavalier 	}
40*f504f610SAugustin Cavalier 	/* k < 0, need special care in the subnormal range.  */
41*f504f610SAugustin Cavalier 	sbits += 1022ull << 52;
42*f504f610SAugustin Cavalier 	scale = asdouble(sbits);
43*f504f610SAugustin Cavalier 	y = scale + scale * tmp;
44*f504f610SAugustin Cavalier 	if (y < 1.0) {
45*f504f610SAugustin Cavalier 		/* Round y to the right precision before scaling it into the subnormal
46*f504f610SAugustin Cavalier 		   range to avoid double rounding that can cause 0.5+E/2 ulp error where
47*f504f610SAugustin Cavalier 		   E is the worst-case ulp error outside the subnormal range.  So this
48*f504f610SAugustin Cavalier 		   is only useful if the goal is better than 1 ulp worst-case error.  */
49*f504f610SAugustin Cavalier 		double_t hi, lo;
50*f504f610SAugustin Cavalier 		lo = scale - y + scale * tmp;
51*f504f610SAugustin Cavalier 		hi = 1.0 + y;
52*f504f610SAugustin Cavalier 		lo = 1.0 - hi + y + lo;
53*f504f610SAugustin Cavalier 		y = eval_as_double(hi + lo) - 1.0;
54*f504f610SAugustin Cavalier 		/* Avoid -0.0 with downward rounding.  */
55*f504f610SAugustin Cavalier 		if (WANT_ROUNDING && y == 0.0)
56*f504f610SAugustin Cavalier 			y = 0.0;
57*f504f610SAugustin Cavalier 		/* The underflow exception needs to be signaled explicitly.  */
58*f504f610SAugustin Cavalier 		fp_force_eval(fp_barrier(0x1p-1022) * 0x1p-1022);
59*f504f610SAugustin Cavalier 	}
60*f504f610SAugustin Cavalier 	y = 0x1p-1022 * y;
61*f504f610SAugustin Cavalier 	return eval_as_double(y);
62*f504f610SAugustin Cavalier }
63*f504f610SAugustin Cavalier 
64*f504f610SAugustin Cavalier /* Top 12 bits of a double (sign and exponent bits).  */
top12(double x)65*f504f610SAugustin Cavalier static inline uint32_t top12(double x)
66*f504f610SAugustin Cavalier {
67*f504f610SAugustin Cavalier 	return asuint64(x) >> 52;
68*f504f610SAugustin Cavalier }
69*f504f610SAugustin Cavalier 
exp2(double x)70*f504f610SAugustin Cavalier double exp2(double x)
71*f504f610SAugustin Cavalier {
72*f504f610SAugustin Cavalier 	uint32_t abstop;
73*f504f610SAugustin Cavalier 	uint64_t ki, idx, top, sbits;
74*f504f610SAugustin Cavalier 	double_t kd, r, r2, scale, tail, tmp;
75*f504f610SAugustin Cavalier 
76*f504f610SAugustin Cavalier 	abstop = top12(x) & 0x7ff;
77*f504f610SAugustin Cavalier 	if (predict_false(abstop - top12(0x1p-54) >= top12(512.0) - top12(0x1p-54))) {
78*f504f610SAugustin Cavalier 		if (abstop - top12(0x1p-54) >= 0x80000000)
79*f504f610SAugustin Cavalier 			/* Avoid spurious underflow for tiny x.  */
80*f504f610SAugustin Cavalier 			/* Note: 0 is common input.  */
81*f504f610SAugustin Cavalier 			return WANT_ROUNDING ? 1.0 + x : 1.0;
82*f504f610SAugustin Cavalier 		if (abstop >= top12(1024.0)) {
83*f504f610SAugustin Cavalier 			if (asuint64(x) == asuint64(-INFINITY))
84*f504f610SAugustin Cavalier 				return 0.0;
85*f504f610SAugustin Cavalier 			if (abstop >= top12(INFINITY))
86*f504f610SAugustin Cavalier 				return 1.0 + x;
87*f504f610SAugustin Cavalier 			if (!(asuint64(x) >> 63))
88*f504f610SAugustin Cavalier 				return __math_oflow(0);
89*f504f610SAugustin Cavalier 			else if (asuint64(x) >= asuint64(-1075.0))
90*f504f610SAugustin Cavalier 				return __math_uflow(0);
91*f504f610SAugustin Cavalier 		}
92*f504f610SAugustin Cavalier 		if (2 * asuint64(x) > 2 * asuint64(928.0))
93*f504f610SAugustin Cavalier 			/* Large x is special cased below.  */
94*f504f610SAugustin Cavalier 			abstop = 0;
95*f504f610SAugustin Cavalier 	}
96*f504f610SAugustin Cavalier 
97*f504f610SAugustin Cavalier 	/* exp2(x) = 2^(k/N) * 2^r, with 2^r in [2^(-1/2N),2^(1/2N)].  */
98*f504f610SAugustin Cavalier 	/* x = k/N + r, with int k and r in [-1/2N, 1/2N].  */
99*f504f610SAugustin Cavalier 	kd = eval_as_double(x + Shift);
100*f504f610SAugustin Cavalier 	ki = asuint64(kd); /* k.  */
101*f504f610SAugustin Cavalier 	kd -= Shift; /* k/N for int k.  */
102*f504f610SAugustin Cavalier 	r = x - kd;
103*f504f610SAugustin Cavalier 	/* 2^(k/N) ~= scale * (1 + tail).  */
104*f504f610SAugustin Cavalier 	idx = 2 * (ki % N);
105*f504f610SAugustin Cavalier 	top = ki << (52 - EXP_TABLE_BITS);
106*f504f610SAugustin Cavalier 	tail = asdouble(T[idx]);
107*f504f610SAugustin Cavalier 	/* This is only a valid scale when -1023*N < k < 1024*N.  */
108*f504f610SAugustin Cavalier 	sbits = T[idx + 1] + top;
109*f504f610SAugustin Cavalier 	/* exp2(x) = 2^(k/N) * 2^r ~= scale + scale * (tail + 2^r - 1).  */
110*f504f610SAugustin Cavalier 	/* Evaluation is optimized assuming superscalar pipelined execution.  */
111*f504f610SAugustin Cavalier 	r2 = r * r;
112*f504f610SAugustin Cavalier 	/* Without fma the worst case error is 0.5/N ulp larger.  */
113*f504f610SAugustin Cavalier 	/* Worst case error is less than 0.5+0.86/N+(abs poly error * 2^53) ulp.  */
114*f504f610SAugustin Cavalier 	tmp = tail + r * C1 + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
115*f504f610SAugustin Cavalier 	if (predict_false(abstop == 0))
116*f504f610SAugustin Cavalier 		return specialcase(tmp, sbits, ki);
117*f504f610SAugustin Cavalier 	scale = asdouble(sbits);
118*f504f610SAugustin Cavalier 	/* Note: tmp == 0 or |tmp| > 2^-65 and scale > 2^-928, so there
119*f504f610SAugustin Cavalier 	   is no spurious underflow here even without fma.  */
120*f504f610SAugustin Cavalier 	return eval_as_double(scale + scale * tmp);
121*f504f610SAugustin Cavalier }
122