xref: /freebsd/lib/msun/ld80/s_expl.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2009-2013 Steven G. Kargl
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * Optimized by Bruce D. Evans.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 /**
35  * Compute the exponential of x for Intel 80-bit format.  This is based on:
36  *
37  *   PTP Tang, "Table-driven implementation of the exponential function
38  *   in IEEE floating-point arithmetic," ACM Trans. Math. Soft., 15,
39  *   144-157 (1989).
40  *
41  * where the 32 table entries have been expanded to INTERVALS (see below).
42  */
43 
44 #include <float.h>
45 
46 #ifdef __i386__
47 #include <ieeefp.h>
48 #endif
49 
50 #include "fpmath.h"
51 #include "math.h"
52 #include "math_private.h"
53 #include "k_expl.h"
54 
55 /* XXX Prevent compilers from erroneously constant folding these: */
56 static const volatile long double
57 huge = 0x1p10000L,
58 tiny = 0x1p-10000L;
59 
60 static const long double
61 twom10000 = 0x1p-10000L;
62 
63 static const union IEEEl2bits
64 /* log(2**16384 - 0.5) rounded towards zero: */
65 /* log(2**16384 - 0.5 + 1) rounded towards zero for expm1l() is the same: */
66 o_thresholdu = LD80C(0xb17217f7d1cf79ab, 13,  11356.5234062941439488L),
67 #define o_threshold	 (o_thresholdu.e)
68 /* log(2**(-16381-64-1)) rounded towards zero: */
69 u_thresholdu = LD80C(0xb21dfe7f09e2baa9, 13, -11399.4985314888605581L);
70 #define u_threshold	 (u_thresholdu.e)
71 
72 long double
73 expl(long double x)
74 {
75 	union IEEEl2bits u;
76 	long double hi, lo, t, twopk;
77 	int k;
78 	uint16_t hx, ix;
79 
80 	DOPRINT_START(&x);
81 
82 	/* Filter out exceptional cases. */
83 	u.e = x;
84 	hx = u.xbits.expsign;
85 	ix = hx & 0x7fff;
86 	if (ix >= BIAS + 13) {		/* |x| >= 8192 or x is NaN */
87 		if (ix == BIAS + LDBL_MAX_EXP) {
88 			if (hx & 0x8000)  /* x is -Inf, -NaN or unsupported */
89 				RETURNP(-1 / x);
90 			RETURNP(x + x);	/* x is +Inf, +NaN or unsupported */
91 		}
92 		if (x > o_threshold)
93 			RETURNP(huge * huge);
94 		if (x < u_threshold)
95 			RETURNP(tiny * tiny);
96 	} else if (ix < BIAS - 75) {	/* |x| < 0x1p-75 (includes pseudos) */
97 		RETURN2P(1, x);		/* 1 with inexact iff x != 0 */
98 	}
99 
100 	ENTERI();
101 
102 	twopk = 1;
103 	__k_expl(x, &hi, &lo, &k);
104 	t = SUM2P(hi, lo);
105 
106 	/* Scale by 2**k. */
107 	if (k >= LDBL_MIN_EXP) {
108 		if (k == LDBL_MAX_EXP)
109 			RETURNI(t * 2 * 0x1p16383L);
110 		SET_LDBL_EXPSIGN(twopk, BIAS + k);
111 		RETURNI(t * twopk);
112 	} else {
113 		SET_LDBL_EXPSIGN(twopk, BIAS + k + 10000);
114 		RETURNI(t * twopk * twom10000);
115 	}
116 }
117 
118 /**
119  * Compute expm1l(x) for Intel 80-bit format.  This is based on:
120  *
121  *   PTP Tang, "Table-driven implementation of the Expm1 function
122  *   in IEEE floating-point arithmetic," ACM Trans. Math. Soft., 18,
123  *   211-222 (1992).
124  */
125 
126 /*
127  * Our T1 and T2 are chosen to be approximately the points where method
128  * A and method B have the same accuracy.  Tang's T1 and T2 are the
129  * points where method A's accuracy changes by a full bit.  For Tang,
130  * this drop in accuracy makes method A immediately less accurate than
131  * method B, but our larger INTERVALS makes method A 2 bits more
132  * accurate so it remains the most accurate method significantly
133  * closer to the origin despite losing the full bit in our extended
134  * range for it.
135  */
136 static const double
137 T1 = -0.1659,				/* ~-30.625/128 * log(2) */
138 T2 =  0.1659;				/* ~30.625/128 * log(2) */
139 
140 /*
141  * Domain [-0.1659, 0.1659], range ~[-2.6155e-22, 2.5507e-23]:
142  * |(exp(x)-1-x-x**2/2)/x - p(x)| < 2**-71.6
143  *
144  * XXX the coeffs aren't very carefully rounded, and I get 2.8 more bits,
145  * but unlike for ld128 we can't drop any terms.
146  */
147 static const union IEEEl2bits
148 B3 = LD80C(0xaaaaaaaaaaaaaaab, -3,  1.66666666666666666671e-1L),
149 B4 = LD80C(0xaaaaaaaaaaaaaaac, -5,  4.16666666666666666712e-2L);
150 
151 static const double
152 B5  =  8.3333333333333245e-3,		/*  0x1.111111111110cp-7 */
153 B6  =  1.3888888888888861e-3,		/*  0x1.6c16c16c16c0ap-10 */
154 B7  =  1.9841269841532042e-4,		/*  0x1.a01a01a0319f9p-13 */
155 B8  =  2.4801587302069236e-5,		/*  0x1.a01a01a03cbbcp-16 */
156 B9  =  2.7557316558468562e-6,		/*  0x1.71de37fd33d67p-19 */
157 B10 =  2.7557315829785151e-7,		/*  0x1.27e4f91418144p-22 */
158 B11 =  2.5063168199779829e-8,		/*  0x1.ae94fabdc6b27p-26 */
159 B12 =  2.0887164654459567e-9;		/*  0x1.1f122d6413fe1p-29 */
160 
161 long double
162 expm1l(long double x)
163 {
164 	union IEEEl2bits u, v;
165 	long double fn, hx2_hi, hx2_lo, q, r, r1, r2, t, twomk, twopk, x_hi;
166 	long double x_lo, x2, z;
167 	long double x4;
168 	int k, n, n2;
169 	uint16_t hx, ix;
170 
171 	DOPRINT_START(&x);
172 
173 	/* Filter out exceptional cases. */
174 	u.e = x;
175 	hx = u.xbits.expsign;
176 	ix = hx & 0x7fff;
177 	if (ix >= BIAS + 6) {		/* |x| >= 64 or x is NaN */
178 		if (ix == BIAS + LDBL_MAX_EXP) {
179 			if (hx & 0x8000)  /* x is -Inf, -NaN or unsupported */
180 				RETURNP(-1 / x - 1);
181 			RETURNP(x + x);	/* x is +Inf, +NaN or unsupported */
182 		}
183 		if (x > o_threshold)
184 			RETURNP(huge * huge);
185 		/*
186 		 * expm1l() never underflows, but it must avoid
187 		 * unrepresentable large negative exponents.  We used a
188 		 * much smaller threshold for large |x| above than in
189 		 * expl() so as to handle not so large negative exponents
190 		 * in the same way as large ones here.
191 		 */
192 		if (hx & 0x8000)	/* x <= -64 */
193 			RETURN2P(tiny, -1);	/* good for x < -65ln2 - eps */
194 	}
195 
196 	ENTERI();
197 
198 	if (T1 < x && x < T2) {
199 		if (ix < BIAS - 74) {	/* |x| < 0x1p-74 (includes pseudos) */
200 			/* x (rounded) with inexact if x != 0: */
201 			RETURNPI(x == 0 ? x :
202 			    (0x1p100 * x + fabsl(x)) * 0x1p-100);
203 		}
204 
205 		x2 = x * x;
206 		x4 = x2 * x2;
207 		q = x4 * (x2 * (x4 *
208 		    /*
209 		     * XXX the number of terms is no longer good for
210 		     * pairwise grouping of all except B3, and the
211 		     * grouping is no longer from highest down.
212 		     */
213 		    (x2 *            B12  + (x * B11 + B10)) +
214 		    (x2 * (x * B9 +  B8) +  (x * B7 +  B6))) +
215 			  (x * B5 +  B4.e)) + x2 * x * B3.e;
216 
217 		x_hi = (float)x;
218 		x_lo = x - x_hi;
219 		hx2_hi = x_hi * x_hi / 2;
220 		hx2_lo = x_lo * (x + x_hi) / 2;
221 		if (ix >= BIAS - 7)
222 			RETURN2PI(hx2_hi + x_hi, hx2_lo + x_lo + q);
223 		else
224 			RETURN2PI(x, hx2_lo + q + hx2_hi);
225 	}
226 
227 	/* Reduce x to (k*ln2 + endpoint[n2] + r1 + r2). */
228 	fn = rnintl(x * INV_L);
229 	n = irint(fn);
230 	n2 = (unsigned)n % INTERVALS;
231 	k = n >> LOG2_INTERVALS;
232 	r1 = x - fn * L1;
233 	r2 = fn * -L2;
234 	r = r1 + r2;
235 
236 	/* Prepare scale factor. */
237 	v.e = 1;
238 	v.xbits.expsign = BIAS + k;
239 	twopk = v.e;
240 
241 	/*
242 	 * Evaluate lower terms of
243 	 * expl(endpoint[n2] + r1 + r2) = tbl[n2] * expl(r1 + r2).
244 	 */
245 	z = r * r;
246 	q = r2 + z * (A2 + r * A3) + z * z * (A4 + r * A5) + z * z * z * A6;
247 
248 	t = (long double)tbl[n2].lo + tbl[n2].hi;
249 
250 	if (k == 0) {
251 		t = SUM2P(tbl[n2].hi - 1, tbl[n2].lo * (r1 + 1) + t * q +
252 		    tbl[n2].hi * r1);
253 		RETURNI(t);
254 	}
255 	if (k == -1) {
256 		t = SUM2P(tbl[n2].hi - 2, tbl[n2].lo * (r1 + 1) + t * q +
257 		    tbl[n2].hi * r1);
258 		RETURNI(t / 2);
259 	}
260 	if (k < -7) {
261 		t = SUM2P(tbl[n2].hi, tbl[n2].lo + t * (q + r1));
262 		RETURNI(t * twopk - 1);
263 	}
264 	if (k > 2 * LDBL_MANT_DIG - 1) {
265 		t = SUM2P(tbl[n2].hi, tbl[n2].lo + t * (q + r1));
266 		if (k == LDBL_MAX_EXP)
267 			RETURNI(t * 2 * 0x1p16383L - 1);
268 		RETURNI(t * twopk - 1);
269 	}
270 
271 	v.xbits.expsign = BIAS - k;
272 	twomk = v.e;
273 
274 	if (k > LDBL_MANT_DIG - 1)
275 		t = SUM2P(tbl[n2].hi, tbl[n2].lo - twomk + t * (q + r1));
276 	else
277 		t = SUM2P(tbl[n2].hi - twomk, tbl[n2].lo + t * (q + r1));
278 	RETURNI(t * twopk);
279 }
280