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