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