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