xref: /freebsd/lib/msun/ld80/s_expl.c (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
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 /**
32  * Compute the exponential of x for Intel 80-bit format.  This is based on:
33  *
34  *   PTP Tang, "Table-driven implementation of the exponential function
35  *   in IEEE floating-point arithmetic," ACM Trans. Math. Soft., 15,
36  *   144-157 (1989).
37  *
38  * where the 32 table entries have been expanded to INTERVALS (see below).
39  */
40 
41 #include <float.h>
42 
43 #ifdef __i386__
44 #include <ieeefp.h>
45 #endif
46 
47 #include "fpmath.h"
48 #include "math.h"
49 #include "math_private.h"
50 #include "k_expl.h"
51 
52 /* XXX Prevent compilers from erroneously constant folding these: */
53 static const volatile long double
54 huge = 0x1p10000L,
55 tiny = 0x1p-10000L;
56 
57 static const long double
58 twom10000 = 0x1p-10000L;
59 
60 static const union IEEEl2bits
61 /* log(2**16384 - 0.5) rounded towards zero: */
62 /* log(2**16384 - 0.5 + 1) rounded towards zero for expm1l() is the same: */
63 o_thresholdu = LD80C(0xb17217f7d1cf79ab, 13,  11356.5234062941439488L),
64 #define o_threshold	 (o_thresholdu.e)
65 /* log(2**(-16381-64-1)) rounded towards zero: */
66 u_thresholdu = LD80C(0xb21dfe7f09e2baa9, 13, -11399.4985314888605581L);
67 #define u_threshold	 (u_thresholdu.e)
68 
69 long double
70 expl(long double x)
71 {
72 	union IEEEl2bits u;
73 	long double hi, lo, t, twopk;
74 	int k;
75 	uint16_t hx, ix;
76 
77 	/* Filter out exceptional cases. */
78 	u.e = x;
79 	hx = u.xbits.expsign;
80 	ix = hx & 0x7fff;
81 	if (ix >= BIAS + 13) {		/* |x| >= 8192 or x is NaN */
82 		if (ix == BIAS + LDBL_MAX_EXP) {
83 			if (hx & 0x8000)  /* x is -Inf, -NaN or unsupported */
84 				RETURNF(-1 / x);
85 			RETURNF(x + x);	/* x is +Inf, +NaN or unsupported */
86 		}
87 		if (x > o_threshold)
88 			RETURNF(huge * huge);
89 		if (x < u_threshold)
90 			RETURNF(tiny * tiny);
91 	} else if (ix < BIAS - 75) {	/* |x| < 0x1p-75 (includes pseudos) */
92 		RETURNF(1 + x);		/* 1 with inexact iff x != 0 */
93 	}
94 
95 	ENTERI();
96 
97 	twopk = 1;
98 	__k_expl(x, &hi, &lo, &k);
99 	t = SUM2P(hi, lo);
100 
101 	/* Scale by 2**k. */
102 	if (k >= LDBL_MIN_EXP) {
103 		if (k == LDBL_MAX_EXP)
104 			RETURNI(t * 2 * 0x1p16383L);
105 		SET_LDBL_EXPSIGN(twopk, BIAS + k);
106 		RETURNI(t * twopk);
107 	} else {
108 		SET_LDBL_EXPSIGN(twopk, BIAS + k + 10000);
109 		RETURNI(t * twopk * twom10000);
110 	}
111 }
112 
113 /**
114  * Compute expm1l(x) for Intel 80-bit format.  This is based on:
115  *
116  *   PTP Tang, "Table-driven implementation of the Expm1 function
117  *   in IEEE floating-point arithmetic," ACM Trans. Math. Soft., 18,
118  *   211-222 (1992).
119  */
120 
121 /*
122  * Our T1 and T2 are chosen to be approximately the points where method
123  * A and method B have the same accuracy.  Tang's T1 and T2 are the
124  * points where method A's accuracy changes by a full bit.  For Tang,
125  * this drop in accuracy makes method A immediately less accurate than
126  * method B, but our larger INTERVALS makes method A 2 bits more
127  * accurate so it remains the most accurate method significantly
128  * closer to the origin despite losing the full bit in our extended
129  * range for it.
130  */
131 static const double
132 T1 = -0.1659,				/* ~-30.625/128 * log(2) */
133 T2 =  0.1659;				/* ~30.625/128 * log(2) */
134 
135 /*
136  * Domain [-0.1659, 0.1659], range ~[-2.6155e-22, 2.5507e-23]:
137  * |(exp(x)-1-x-x**2/2)/x - p(x)| < 2**-71.6
138  *
139  * XXX the coeffs aren't very carefully rounded, and I get 2.8 more bits,
140  * but unlike for ld128 we can't drop any terms.
141  */
142 static const union IEEEl2bits
143 B3 = LD80C(0xaaaaaaaaaaaaaaab, -3,  1.66666666666666666671e-1L),
144 B4 = LD80C(0xaaaaaaaaaaaaaaac, -5,  4.16666666666666666712e-2L);
145 
146 static const double
147 B5  =  8.3333333333333245e-3,		/*  0x1.111111111110cp-7 */
148 B6  =  1.3888888888888861e-3,		/*  0x1.6c16c16c16c0ap-10 */
149 B7  =  1.9841269841532042e-4,		/*  0x1.a01a01a0319f9p-13 */
150 B8  =  2.4801587302069236e-5,		/*  0x1.a01a01a03cbbcp-16 */
151 B9  =  2.7557316558468562e-6,		/*  0x1.71de37fd33d67p-19 */
152 B10 =  2.7557315829785151e-7,		/*  0x1.27e4f91418144p-22 */
153 B11 =  2.5063168199779829e-8,		/*  0x1.ae94fabdc6b27p-26 */
154 B12 =  2.0887164654459567e-9;		/*  0x1.1f122d6413fe1p-29 */
155 
156 long double
157 expm1l(long double x)
158 {
159 	union IEEEl2bits u, v;
160 	long double fn, hx2_hi, hx2_lo, q, r, r1, r2, t, twomk, twopk, x_hi;
161 	long double x_lo, x2, z;
162 	long double x4;
163 	int k, n, n2;
164 	uint16_t hx, ix;
165 
166 	/* Filter out exceptional cases. */
167 	u.e = x;
168 	hx = u.xbits.expsign;
169 	ix = hx & 0x7fff;
170 	if (ix >= BIAS + 6) {		/* |x| >= 64 or x is NaN */
171 		if (ix == BIAS + LDBL_MAX_EXP) {
172 			if (hx & 0x8000)  /* x is -Inf, -NaN or unsupported */
173 				RETURNF(-1 / x - 1);
174 			RETURNF(x + x);	/* x is +Inf, +NaN or unsupported */
175 		}
176 		if (x > o_threshold)
177 			RETURNF(huge * huge);
178 		/*
179 		 * expm1l() never underflows, but it must avoid
180 		 * unrepresentable large negative exponents.  We used a
181 		 * much smaller threshold for large |x| above than in
182 		 * expl() so as to handle not so large negative exponents
183 		 * in the same way as large ones here.
184 		 */
185 		if (hx & 0x8000)	/* x <= -64 */
186 			RETURNF(tiny - 1);	/* good for x < -65ln2 - eps */
187 	}
188 
189 	ENTERI();
190 
191 	if (T1 < x && x < T2) {
192 		if (ix < BIAS - 74) {	/* |x| < 0x1p-74 (includes pseudos) */
193 			/* x (rounded) with inexact if x != 0: */
194 			RETURNI(x == 0 ? x :
195 			    (0x1p100 * x + fabsl(x)) * 0x1p-100);
196 		}
197 
198 		x2 = x * x;
199 		x4 = x2 * x2;
200 		q = x4 * (x2 * (x4 *
201 		    /*
202 		     * XXX the number of terms is no longer good for
203 		     * pairwise grouping of all except B3, and the
204 		     * grouping is no longer from highest down.
205 		     */
206 		    (x2 *            B12  + (x * B11 + B10)) +
207 		    (x2 * (x * B9 +  B8) +  (x * B7 +  B6))) +
208 			  (x * B5 +  B4.e)) + x2 * x * B3.e;
209 
210 		x_hi = (float)x;
211 		x_lo = x - x_hi;
212 		hx2_hi = x_hi * x_hi / 2;
213 		hx2_lo = x_lo * (x + x_hi) / 2;
214 		if (ix >= BIAS - 7)
215 			RETURNI((hx2_hi + x_hi) + (hx2_lo + x_lo + q));
216 		else
217 			RETURNI(x + (hx2_lo + q + hx2_hi));
218 	}
219 
220 	/* Reduce x to (k*ln2 + endpoint[n2] + r1 + r2). */
221 	fn = rnintl(x * INV_L);
222 	n = irint(fn);
223 	n2 = (unsigned)n % INTERVALS;
224 	k = n >> LOG2_INTERVALS;
225 	r1 = x - fn * L1;
226 	r2 = fn * -L2;
227 	r = r1 + r2;
228 
229 	/* Prepare scale factor. */
230 	v.e = 1;
231 	v.xbits.expsign = BIAS + k;
232 	twopk = v.e;
233 
234 	/*
235 	 * Evaluate lower terms of
236 	 * expl(endpoint[n2] + r1 + r2) = tbl[n2] * expl(r1 + r2).
237 	 */
238 	z = r * r;
239 	q = r2 + z * (A2 + r * A3) + z * z * (A4 + r * A5) + z * z * z * A6;
240 
241 	t = (long double)tbl[n2].lo + tbl[n2].hi;
242 
243 	if (k == 0) {
244 		t = SUM2P(tbl[n2].hi - 1, tbl[n2].lo * (r1 + 1) + t * q +
245 		    tbl[n2].hi * r1);
246 		RETURNI(t);
247 	}
248 	if (k == -1) {
249 		t = SUM2P(tbl[n2].hi - 2, tbl[n2].lo * (r1 + 1) + t * q +
250 		    tbl[n2].hi * r1);
251 		RETURNI(t / 2);
252 	}
253 	if (k < -7) {
254 		t = SUM2P(tbl[n2].hi, tbl[n2].lo + t * (q + r1));
255 		RETURNI(t * twopk - 1);
256 	}
257 	if (k > 2 * LDBL_MANT_DIG - 1) {
258 		t = SUM2P(tbl[n2].hi, tbl[n2].lo + t * (q + r1));
259 		if (k == LDBL_MAX_EXP)
260 			RETURNI(t * 2 * 0x1p16383L - 1);
261 		RETURNI(t * twopk - 1);
262 	}
263 
264 	v.xbits.expsign = BIAS - k;
265 	twomk = v.e;
266 
267 	if (k > LDBL_MANT_DIG - 1)
268 		t = SUM2P(tbl[n2].hi, tbl[n2].lo - twomk + t * (q + r1));
269 	else
270 		t = SUM2P(tbl[n2].hi - twomk, tbl[n2].lo + t * (q + r1));
271 	RETURNI(t * twopk);
272 }
273