xref: /freebsd/lib/msun/ld128/s_expl.c (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
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 /*
33  * ld128 version of s_expl.c.  See ../ld80/s_expl.c for most comments.
34  */
35 
36 #include <float.h>
37 
38 #include "fpmath.h"
39 #include "math.h"
40 #include "math_private.h"
41 #include "k_expl.h"
42 
43 /* XXX Prevent compilers from erroneously constant folding these: */
44 static const volatile long double
45 huge = 0x1p10000L,
46 tiny = 0x1p-10000L;
47 
48 static const long double
49 twom10000 = 0x1p-10000L;
50 
51 static const long double
52 /* log(2**16384 - 0.5) rounded towards zero: */
53 /* log(2**16384 - 0.5 + 1) rounded towards zero for expm1l() is the same: */
54 o_threshold =  11356.523406294143949491931077970763428L,
55 /* log(2**(-16381-64-1)) rounded towards zero: */
56 u_threshold = -11433.462743336297878837243843452621503L;
57 
58 long double
59 expl(long double x)
60 {
61 	union IEEEl2bits u;
62 	long double hi, lo, t, twopk;
63 	int k;
64 	uint16_t hx, ix;
65 
66 	/* Filter out exceptional cases. */
67 	u.e = x;
68 	hx = u.xbits.expsign;
69 	ix = hx & 0x7fff;
70 	if (ix >= BIAS + 13) {		/* |x| >= 8192 or x is NaN */
71 		if (ix == BIAS + LDBL_MAX_EXP) {
72 			if (hx & 0x8000)  /* x is -Inf or -NaN */
73 				RETURNF(-1 / x);
74 			RETURNF(x + x);	/* x is +Inf or +NaN */
75 		}
76 		if (x > o_threshold)
77 			RETURNF(huge * huge);
78 		if (x < u_threshold)
79 			RETURNF(tiny * tiny);
80 	} else if (ix < BIAS - 114) {	/* |x| < 0x1p-114 */
81 		RETURNF(1 + x);		/* 1 with inexact iff x != 0 */
82 	}
83 
84 	ENTERI();
85 
86 	twopk = 1;
87 	__k_expl(x, &hi, &lo, &k);
88 	t = SUM2P(hi, lo);
89 
90 	/* Scale by 2**k. */
91 	/*
92 	 * XXX sparc64 multiplication was so slow that scalbnl() is faster,
93 	 * but performance on aarch64 and riscv hasn't yet been quantified.
94 	 */
95 	if (k >= LDBL_MIN_EXP) {
96 		if (k == LDBL_MAX_EXP)
97 			RETURNI(t * 2 * 0x1p16383L);
98 		SET_LDBL_EXPSIGN(twopk, BIAS + k);
99 		RETURNI(t * twopk);
100 	} else {
101 		SET_LDBL_EXPSIGN(twopk, BIAS + k + 10000);
102 		RETURNI(t * twopk * twom10000);
103 	}
104 }
105 
106 /*
107  * Our T1 and T2 are chosen to be approximately the points where method
108  * A and method B have the same accuracy.  Tang's T1 and T2 are the
109  * points where method A's accuracy changes by a full bit.  For Tang,
110  * this drop in accuracy makes method A immediately less accurate than
111  * method B, but our larger INTERVALS makes method A 2 bits more
112  * accurate so it remains the most accurate method significantly
113  * closer to the origin despite losing the full bit in our extended
114  * range for it.
115  *
116  * Split the interval [T1, T2] into two intervals [T1, T3] and [T3, T2].
117  * Setting T3 to 0 would require the |x| < 0x1p-113 condition to appear
118  * in both subintervals, so set T3 = 2**-5, which places the condition
119  * into the [T1, T3] interval.
120  *
121  * XXX we now do this more to (partially) balance the number of terms
122  * in the C and D polys than to avoid checking the condition in both
123  * intervals.
124  *
125  * XXX these micro-optimizations are excessive.
126  */
127 static const double
128 T1 = -0.1659,				/* ~-30.625/128 * log(2) */
129 T2 =  0.1659,				/* ~30.625/128 * log(2) */
130 T3 =  0.03125;
131 
132 /*
133  * Domain [-0.1659, 0.03125], range ~[2.9134e-44, 1.8404e-37]:
134  * |(exp(x)-1-x-x**2/2)/x - p(x)| < 2**-122.03
135  *
136  * XXX none of the long double C or D coeffs except C10 is correctly printed.
137  * If you re-print their values in %.35Le format, the result is always
138  * different.  For example, the last 2 digits in C3 should be 59, not 67.
139  * 67 is apparently from rounding an extra-precision value to 36 decimal
140  * places.
141  */
142 static const long double
143 C3  =  1.66666666666666666666666666666666667e-1L,
144 C4  =  4.16666666666666666666666666666666645e-2L,
145 C5  =  8.33333333333333333333333333333371638e-3L,
146 C6  =  1.38888888888888888888888888891188658e-3L,
147 C7  =  1.98412698412698412698412697235950394e-4L,
148 C8  =  2.48015873015873015873015112487849040e-5L,
149 C9  =  2.75573192239858906525606685484412005e-6L,
150 C10 =  2.75573192239858906612966093057020362e-7L,
151 C11 =  2.50521083854417203619031960151253944e-8L,
152 C12 =  2.08767569878679576457272282566520649e-9L,
153 C13 =  1.60590438367252471783548748824255707e-10L;
154 
155 /*
156  * XXX this has 1 more coeff than needed.
157  * XXX can start the double coeffs but not the double mults at C10.
158  * With my coeffs (C10-C17 double; s = best_s):
159  * Domain [-0.1659, 0.03125], range ~[-1.1976e-37, 1.1976e-37]:
160  * |(exp(x)-1-x-x**2/2)/x - p(x)| ~< 2**-122.65
161  */
162 static const double
163 C14 =  1.1470745580491932e-11,		/*  0x1.93974a81dae30p-37 */
164 C15 =  7.6471620181090468e-13,		/*  0x1.ae7f3820adab1p-41 */
165 C16 =  4.7793721460260450e-14,		/*  0x1.ae7cd18a18eacp-45 */
166 C17 =  2.8074757356658877e-15,		/*  0x1.949992a1937d9p-49 */
167 C18 =  1.4760610323699476e-16;		/*  0x1.545b43aabfbcdp-53 */
168 
169 /*
170  * Domain [0.03125, 0.1659], range ~[-2.7676e-37, -1.0367e-38]:
171  * |(exp(x)-1-x-x**2/2)/x - p(x)| < 2**-121.44
172  */
173 static const long double
174 D3  =  1.66666666666666666666666666666682245e-1L,
175 D4  =  4.16666666666666666666666666634228324e-2L,
176 D5  =  8.33333333333333333333333364022244481e-3L,
177 D6  =  1.38888888888888888888887138722762072e-3L,
178 D7  =  1.98412698412698412699085805424661471e-4L,
179 D8  =  2.48015873015873015687993712101479612e-5L,
180 D9  =  2.75573192239858944101036288338208042e-6L,
181 D10 =  2.75573192239853161148064676533754048e-7L,
182 D11 =  2.50521083855084570046480450935267433e-8L,
183 D12 =  2.08767569819738524488686318024854942e-9L,
184 D13 =  1.60590442297008495301927448122499313e-10L;
185 
186 /*
187  * XXX this has 1 more coeff than needed.
188  * XXX can start the double coeffs but not the double mults at D11.
189  * With my coeffs (D11-D16 double):
190  * Domain [0.03125, 0.1659], range ~[-1.1980e-37, 1.1980e-37]:
191  * |(exp(x)-1-x-x**2/2)/x - p(x)| ~< 2**-122.65
192  */
193 static const double
194 D14 =  1.1470726176204336e-11,		/*  0x1.93971dc395d9ep-37 */
195 D15 =  7.6478532249581686e-13,		/*  0x1.ae892e3D16fcep-41 */
196 D16 =  4.7628892832607741e-14,		/*  0x1.ad00Dfe41feccp-45 */
197 D17 =  3.0524857220358650e-15;		/*  0x1.D7e8d886Df921p-49 */
198 
199 long double
200 expm1l(long double x)
201 {
202 	union IEEEl2bits u, v;
203 	long double hx2_hi, hx2_lo, q, r, r1, t, twomk, twopk, x_hi;
204 	long double x_lo, x2;
205 	double dr, dx, fn, r2;
206 	int k, n, n2;
207 	uint16_t hx, ix;
208 
209 	/* Filter out exceptional cases. */
210 	u.e = x;
211 	hx = u.xbits.expsign;
212 	ix = hx & 0x7fff;
213 	if (ix >= BIAS + 7) {		/* |x| >= 128 or x is NaN */
214 		if (ix == BIAS + LDBL_MAX_EXP) {
215 			if (hx & 0x8000)  /* x is -Inf or -NaN */
216 				RETURNF(-1 / x - 1);
217 			RETURNF(x + x);	/* x is +Inf or +NaN */
218 		}
219 		if (x > o_threshold)
220 			RETURNF(huge * huge);
221 		/*
222 		 * expm1l() never underflows, but it must avoid
223 		 * unrepresentable large negative exponents.  We used a
224 		 * much smaller threshold for large |x| above than in
225 		 * expl() so as to handle not so large negative exponents
226 		 * in the same way as large ones here.
227 		 */
228 		if (hx & 0x8000)	/* x <= -128 */
229 			RETURNF(tiny - 1);	/* good for x < -114ln2 - eps */
230 	}
231 
232 	ENTERI();
233 
234 	if (T1 < x && x < T2) {
235 		x2 = x * x;
236 		dx = x;
237 
238 		if (x < T3) {
239 			if (ix < BIAS - 113) {	/* |x| < 0x1p-113 */
240 				/* x (rounded) with inexact if x != 0: */
241 				RETURNI(x == 0 ? x :
242 				    (0x1p200 * x + fabsl(x)) * 0x1p-200);
243 			}
244 			q = x * x2 * C3 + x2 * x2 * (C4 + x * (C5 + x * (C6 +
245 			    x * (C7 + x * (C8 + x * (C9 + x * (C10 +
246 			    x * (C11 + x * (C12 + x * (C13 +
247 			    dx * (C14 + dx * (C15 + dx * (C16 +
248 			    dx * (C17 + dx * C18))))))))))))));
249 		} else {
250 			q = x * x2 * D3 + x2 * x2 * (D4 + x * (D5 + x * (D6 +
251 			    x * (D7 + x * (D8 + x * (D9 + x * (D10 +
252 			    x * (D11 + x * (D12 + x * (D13 +
253 			    dx * (D14 + dx * (D15 + dx * (D16 +
254 			    dx * D17)))))))))))));
255 		}
256 
257 		x_hi = (float)x;
258 		x_lo = x - x_hi;
259 		hx2_hi = x_hi * x_hi / 2;
260 		hx2_lo = x_lo * (x + x_hi) / 2;
261 		if (ix >= BIAS - 7)
262 			RETURNI((hx2_hi + x_hi) + (hx2_lo + x_lo + q));
263 		else
264 			RETURNI(x + (hx2_lo + q + hx2_hi));
265 	}
266 
267 	/* Reduce x to (k*ln2 + endpoint[n2] + r1 + r2). */
268 	fn = rnint((double)x * INV_L);
269 	n = irint(fn);
270 	n2 = (unsigned)n % INTERVALS;
271 	k = n >> LOG2_INTERVALS;
272 	r1 = x - fn * L1;
273 	r2 = fn * -L2;
274 	r = r1 + r2;
275 
276 	/* Prepare scale factor. */
277 	v.e = 1;
278 	v.xbits.expsign = BIAS + k;
279 	twopk = v.e;
280 
281 	/*
282 	 * Evaluate lower terms of
283 	 * expl(endpoint[n2] + r1 + r2) = tbl[n2] * expl(r1 + r2).
284 	 */
285 	dr = r;
286 	q = r2 + r * r * (A2 + r * (A3 + r * (A4 + r * (A5 + r * (A6 +
287 	    dr * (A7 + dr * (A8 + dr * (A9 + dr * A10))))))));
288 
289 	t = tbl[n2].lo + tbl[n2].hi;
290 
291 	if (k == 0) {
292 		t = SUM2P(tbl[n2].hi - 1, tbl[n2].lo * (r1 + 1) + t * q +
293 		    tbl[n2].hi * r1);
294 		RETURNI(t);
295 	}
296 	if (k == -1) {
297 		t = SUM2P(tbl[n2].hi - 2, tbl[n2].lo * (r1 + 1) + t * q +
298 		    tbl[n2].hi * r1);
299 		RETURNI(t / 2);
300 	}
301 	if (k < -7) {
302 		t = SUM2P(tbl[n2].hi, tbl[n2].lo + t * (q + r1));
303 		RETURNI(t * twopk - 1);
304 	}
305 	if (k > 2 * LDBL_MANT_DIG - 1) {
306 		t = SUM2P(tbl[n2].hi, tbl[n2].lo + t * (q + r1));
307 		if (k == LDBL_MAX_EXP)
308 			RETURNI(t * 2 * 0x1p16383L - 1);
309 		RETURNI(t * twopk - 1);
310 	}
311 
312 	v.xbits.expsign = BIAS - k;
313 	twomk = v.e;
314 
315 	if (k > LDBL_MANT_DIG - 1)
316 		t = SUM2P(tbl[n2].hi, tbl[n2].lo - twomk + t * (q + r1));
317 	else
318 		t = SUM2P(tbl[n2].hi - twomk, tbl[n2].lo + t * (q + r1));
319 	RETURNI(t * twopk);
320 }
321