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