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