]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - lib/msun/ld80/s_expl.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / lib / msun / ld80 / s_expl.c
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$");
31
32 /**
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"
51
52 #define INTERVALS       128
53 #define LOG2_INTERVALS  7
54 #define BIAS    (LDBL_MAX_EXP - 1)
55
56 static const long double
57 huge = 0x1p10000L,
58 twom10000 = 0x1p-10000L;
59 /* XXX Prevent gcc from erroneously constant folding this: */
60 static volatile const long double tiny = 0x1p-10000L;
61
62 static const union IEEEl2bits
63 /* log(2**16384 - 0.5) rounded towards zero: */
64 /* log(2**16384 - 0.5 + 1) rounded towards zero for expm1l() is the same: */
65 o_thresholdu = LD80C(0xb17217f7d1cf79ab, 13,  11356.5234062941439488L),
66 #define o_threshold      (o_thresholdu.e)
67 /* log(2**(-16381-64-1)) rounded towards zero: */
68 u_thresholdu = LD80C(0xb21dfe7f09e2baa9, 13, -11399.4985314888605581L);
69 #define u_threshold      (u_thresholdu.e)
70
71 static const double
72 /*
73  * ln2/INTERVALS = L1+L2 (hi+lo decomposition for multiplication).  L1 must
74  * have at least 22 (= log2(|LDBL_MIN_EXP-extras|) + log2(INTERVALS)) lowest
75  * bits zero so that multiplication of it by n is exact.
76  */
77 INV_L = 1.8466496523378731e+2,          /*  0x171547652b82fe.0p-45 */
78 L1 =  5.4152123484527692e-3,            /*  0x162e42ff000000.0p-60 */
79 L2 = -3.2819649005320973e-13,           /* -0x1718432a1b0e26.0p-94 */
80 /*
81  * Domain [-0.002708, 0.002708], range ~[-5.7136e-24, 5.7110e-24]:
82  * |exp(x) - p(x)| < 2**-77.2
83  * (0.002708 is ln2/(2*INTERVALS) rounded up a little).
84  */
85 A2 =  0.5,
86 A3 =  1.6666666666666119e-1,            /*  0x15555555555490.0p-55 */
87 A4 =  4.1666666666665887e-2,            /*  0x155555555554e5.0p-57 */
88 A5 =  8.3333354987869413e-3,            /*  0x1111115b789919.0p-59 */
89 A6 =  1.3888891738560272e-3;            /*  0x16c16c651633ae.0p-62 */
90
91 /*
92  * 2^(i/INTERVALS) for i in [0,INTERVALS] is represented by two values where
93  * the first 53 bits of the significand are stored in hi and the next 53
94  * bits are in lo.  Tang's paper states that the trailing 6 bits of hi must
95  * be zero for his algorithm in both single and double precision, because
96  * the table is re-used in the implementation of expm1() where a floating
97  * point addition involving hi must be exact.  Here hi is double, so
98  * converting it to long double gives 11 trailing zero bits.
99  */
100 static const struct {
101         double  hi;
102         double  lo;
103 } tbl[INTERVALS] = {
104         0x1p+0, 0x0p+0,
105         0x1.0163da9fb3335p+0, 0x1.b61299ab8cdb7p-54,
106         0x1.02c9a3e778060p+0, 0x1.dcdef95949ef4p-53,
107         0x1.04315e86e7f84p+0, 0x1.7ae71f3441b49p-53,
108         0x1.059b0d3158574p+0, 0x1.d73e2a475b465p-55,
109         0x1.0706b29ddf6ddp+0, 0x1.8db880753b0f6p-53,
110         0x1.0874518759bc8p+0, 0x1.186be4bb284ffp-57,
111         0x1.09e3ecac6f383p+0, 0x1.1487818316136p-54,
112         0x1.0b5586cf9890fp+0, 0x1.8a62e4adc610bp-54,
113         0x1.0cc922b7247f7p+0, 0x1.01edc16e24f71p-54,
114         0x1.0e3ec32d3d1a2p+0, 0x1.03a1727c57b53p-59,
115         0x1.0fb66affed31ap+0, 0x1.e464123bb1428p-53,
116         0x1.11301d0125b50p+0, 0x1.49d77e35db263p-53,
117         0x1.12abdc06c31cbp+0, 0x1.f72575a649ad2p-53,
118         0x1.1429aaea92ddfp+0, 0x1.66820328764b1p-53,
119         0x1.15a98c8a58e51p+0, 0x1.2406ab9eeab0ap-55,
120         0x1.172b83c7d517ap+0, 0x1.b9bef918a1d63p-53,
121         0x1.18af9388c8de9p+0, 0x1.777ee1734784ap-53,
122         0x1.1a35beb6fcb75p+0, 0x1.e5b4c7b4968e4p-55,
123         0x1.1bbe084045cd3p+0, 0x1.3563ce56884fcp-53,
124         0x1.1d4873168b9aap+0, 0x1.e016e00a2643cp-54,
125         0x1.1ed5022fcd91cp+0, 0x1.71033fec2243ap-53,
126         0x1.2063b88628cd6p+0, 0x1.dc775814a8495p-55,
127         0x1.21f49917ddc96p+0, 0x1.2a97e9494a5eep-55,
128         0x1.2387a6e756238p+0, 0x1.9b07eb6c70573p-54,
129         0x1.251ce4fb2a63fp+0, 0x1.ac155bef4f4a4p-55,
130         0x1.26b4565e27cddp+0, 0x1.2bd339940e9d9p-55,
131         0x1.284dfe1f56380p+0, 0x1.2d9e2b9e07941p-53,
132         0x1.29e9df51fdee1p+0, 0x1.612e8afad1255p-55,
133         0x1.2b87fd0dad98fp+0, 0x1.fbbd48ca71f95p-53,
134         0x1.2d285a6e4030bp+0, 0x1.0024754db41d5p-54,
135         0x1.2ecafa93e2f56p+0, 0x1.1ca0f45d52383p-56,
136         0x1.306fe0a31b715p+0, 0x1.6f46ad23182e4p-55,
137         0x1.32170fc4cd831p+0, 0x1.a9ce78e18047cp-55,
138         0x1.33c08b26416ffp+0, 0x1.32721843659a6p-54,
139         0x1.356c55f929ff0p+0, 0x1.928c468ec6e76p-53,
140         0x1.371a7373aa9cap+0, 0x1.4e28aa05e8a8fp-53,
141         0x1.38cae6d05d865p+0, 0x1.0b53961b37da2p-53,
142         0x1.3a7db34e59ff6p+0, 0x1.d43792533c144p-53,
143         0x1.3c32dc313a8e4p+0, 0x1.08003e4516b1ep-53,
144         0x1.3dea64c123422p+0, 0x1.ada0911f09ebcp-55,
145         0x1.3fa4504ac801bp+0, 0x1.417ee03548306p-53,
146         0x1.4160a21f72e29p+0, 0x1.f0864b71e7b6cp-53,
147         0x1.431f5d950a896p+0, 0x1.b8e088728219ap-53,
148         0x1.44e086061892dp+0, 0x1.89b7a04ef80d0p-59,
149         0x1.46a41ed1d0057p+0, 0x1.c944bd1648a76p-54,
150         0x1.486a2b5c13cd0p+0, 0x1.3c1a3b69062f0p-56,
151         0x1.4a32af0d7d3dep+0, 0x1.9cb62f3d1be56p-54,
152         0x1.4bfdad5362a27p+0, 0x1.d4397afec42e2p-56,
153         0x1.4dcb299fddd0dp+0, 0x1.8ecdbbc6a7833p-54,
154         0x1.4f9b2769d2ca6p+0, 0x1.5a67b16d3540ep-53,
155         0x1.516daa2cf6641p+0, 0x1.8225ea5909b04p-53,
156         0x1.5342b569d4f81p+0, 0x1.be1507893b0d5p-53,
157         0x1.551a4ca5d920ep+0, 0x1.8a5d8c4048699p-53,
158         0x1.56f4736b527dap+0, 0x1.9bb2c011d93adp-54,
159         0x1.58d12d497c7fdp+0, 0x1.295e15b9a1de8p-55,
160         0x1.5ab07dd485429p+0, 0x1.6324c054647adp-54,
161         0x1.5c9268a5946b7p+0, 0x1.c4b1b816986a2p-60,
162         0x1.5e76f15ad2148p+0, 0x1.ba6f93080e65ep-54,
163         0x1.605e1b976dc08p+0, 0x1.60edeb25490dcp-53,
164         0x1.6247eb03a5584p+0, 0x1.63e1f40dfa5b5p-53,
165         0x1.6434634ccc31fp+0, 0x1.8edf0e2989db3p-53,
166         0x1.6623882552224p+0, 0x1.224fb3c5371e6p-53,
167         0x1.68155d44ca973p+0, 0x1.038ae44f73e65p-57,
168         0x1.6a09e667f3bccp+0, 0x1.21165f626cdd5p-53,
169         0x1.6c012750bdabep+0, 0x1.daed533001e9ep-53,
170         0x1.6dfb23c651a2ep+0, 0x1.e441c597c3775p-53,
171         0x1.6ff7df9519483p+0, 0x1.9f0fc369e7c42p-53,
172         0x1.71f75e8ec5f73p+0, 0x1.ba46e1e5de15ap-53,
173         0x1.73f9a48a58173p+0, 0x1.7ab9349cd1562p-53,
174         0x1.75feb564267c8p+0, 0x1.7edd354674916p-53,
175         0x1.780694fde5d3fp+0, 0x1.866b80a02162dp-54,
176         0x1.7a11473eb0186p+0, 0x1.afaa2047ed9b4p-53,
177         0x1.7c1ed0130c132p+0, 0x1.f124cd1164dd6p-54,
178         0x1.7e2f336cf4e62p+0, 0x1.05d02ba15797ep-56,
179         0x1.80427543e1a11p+0, 0x1.6c1bccec9346bp-53,
180         0x1.82589994cce12p+0, 0x1.159f115f56694p-53,
181         0x1.8471a4623c7acp+0, 0x1.9ca5ed72f8c81p-53,
182         0x1.868d99b4492ecp+0, 0x1.01c83b21584a3p-53,
183         0x1.88ac7d98a6699p+0, 0x1.994c2f37cb53ap-54,
184         0x1.8ace5422aa0dbp+0, 0x1.6e9f156864b27p-54,
185         0x1.8cf3216b5448bp+0, 0x1.de55439a2c38bp-53,
186         0x1.8f1ae99157736p+0, 0x1.5cc13a2e3976cp-55,
187         0x1.9145b0b91ffc5p+0, 0x1.114c368d3ed6ep-53,
188         0x1.93737b0cdc5e4p+0, 0x1.e8a0387e4a814p-53,
189         0x1.95a44cbc8520ep+0, 0x1.d36906d2b41f9p-53,
190         0x1.97d829fde4e4fp+0, 0x1.173d241f23d18p-53,
191         0x1.9a0f170ca07b9p+0, 0x1.7462137188ce7p-53,
192         0x1.9c49182a3f090p+0, 0x1.c7c46b071f2bep-56,
193         0x1.9e86319e32323p+0, 0x1.824ca78e64c6ep-56,
194         0x1.a0c667b5de564p+0, 0x1.6535b51719567p-53,
195         0x1.a309bec4a2d33p+0, 0x1.6305c7ddc36abp-54,
196         0x1.a5503b23e255cp+0, 0x1.1684892395f0fp-53,
197         0x1.a799e1330b358p+0, 0x1.bcb7ecac563c7p-54,
198         0x1.a9e6b5579fdbfp+0, 0x1.0fac90ef7fd31p-54,
199         0x1.ac36bbfd3f379p+0, 0x1.81b72cd4624ccp-53,
200         0x1.ae89f995ad3adp+0, 0x1.7a1cd345dcc81p-54,
201         0x1.b0e07298db665p+0, 0x1.2108559bf8deep-53,
202         0x1.b33a2b84f15fap+0, 0x1.ed7fa1cf7b290p-53,
203         0x1.b59728de55939p+0, 0x1.1c7102222c90ep-53,
204         0x1.b7f76f2fb5e46p+0, 0x1.d54f610356a79p-53,
205         0x1.ba5b030a10649p+0, 0x1.0819678d5eb69p-53,
206         0x1.bcc1e904bc1d2p+0, 0x1.23dd07a2d9e84p-55,
207         0x1.bf2c25bd71e08p+0, 0x1.0811ae04a31c7p-53,
208         0x1.c199bdd85529cp+0, 0x1.11065895048ddp-55,
209         0x1.c40ab5fffd07ap+0, 0x1.b4537e083c60ap-54,
210         0x1.c67f12e57d14bp+0, 0x1.2884dff483cadp-54,
211         0x1.c8f6d9406e7b5p+0, 0x1.1acbc48805c44p-56,
212         0x1.cb720dcef9069p+0, 0x1.503cbd1e949dbp-56,
213         0x1.cdf0b555dc3f9p+0, 0x1.889f12b1f58a3p-53,
214         0x1.d072d4a07897bp+0, 0x1.1a1e45e4342b2p-53,
215         0x1.d2f87080d89f1p+0, 0x1.15bc247313d44p-53,
216         0x1.d5818dcfba487p+0, 0x1.2ed02d75b3707p-55,
217         0x1.d80e316c98397p+0, 0x1.7709f3a09100cp-53,
218         0x1.da9e603db3285p+0, 0x1.c2300696db532p-54,
219         0x1.dd321f301b460p+0, 0x1.2da5778f018c3p-54,
220         0x1.dfc97337b9b5ep+0, 0x1.72d195873da52p-53,
221         0x1.e264614f5a128p+0, 0x1.424ec3f42f5b5p-53,
222         0x1.e502ee78b3ff6p+0, 0x1.39e8980a9cc8fp-55,
223         0x1.e7a51fbc74c83p+0, 0x1.2d522ca0c8de2p-54,
224         0x1.ea4afa2a490d9p+0, 0x1.0b1ee7431ebb6p-53,
225         0x1.ecf482d8e67f0p+0, 0x1.1b60625f7293ap-53,
226         0x1.efa1bee615a27p+0, 0x1.dc7f486a4b6b0p-54,
227         0x1.f252b376bba97p+0, 0x1.3a1a5bf0d8e43p-54,
228         0x1.f50765b6e4540p+0, 0x1.9d3e12dd8a18bp-54,
229         0x1.f7bfdad9cbe13p+0, 0x1.1227697fce57bp-53,
230         0x1.fa7c1819e90d8p+0, 0x1.74853f3a5931ep-55,
231         0x1.fd3c22b8f71f1p+0, 0x1.2eb74966579e7p-57
232 };
233
234 long double
235 expl(long double x)
236 {
237         union IEEEl2bits u, v;
238         long double fn, q, r, r1, r2, t, twopk, twopkp10000;
239         long double z;
240         int k, n, n2;
241         uint16_t hx, ix;
242
243         /* Filter out exceptional cases. */
244         u.e = x;
245         hx = u.xbits.expsign;
246         ix = hx & 0x7fff;
247         if (ix >= BIAS + 13) {          /* |x| >= 8192 or x is NaN */
248                 if (ix == BIAS + LDBL_MAX_EXP) {
249                         if (hx & 0x8000)  /* x is -Inf, -NaN or unsupported */
250                                 return (-1 / x);
251                         return (x + x); /* x is +Inf, +NaN or unsupported */
252                 }
253                 if (x > o_threshold)
254                         return (huge * huge);
255                 if (x < u_threshold)
256                         return (tiny * tiny);
257         } else if (ix < BIAS - 65) {    /* |x| < 0x1p-65 (includes pseudos) */
258                 return (1 + x);         /* 1 with inexact iff x != 0 */
259         }
260
261         ENTERI();
262
263         /* Reduce x to (k*ln2 + endpoint[n2] + r1 + r2). */
264         /* Use a specialized rint() to get fn.  Assume round-to-nearest. */
265         fn = x * INV_L + 0x1.8p63 - 0x1.8p63;
266         r = x - fn * L1 - fn * L2;      /* r = r1 + r2 done independently. */
267 #if defined(HAVE_EFFICIENT_IRINTL)
268         n = irintl(fn);
269 #elif defined(HAVE_EFFICIENT_IRINT)
270         n = irint(fn);
271 #else
272         n = (int)fn;
273 #endif
274         n2 = (unsigned)n % INTERVALS;
275         /* Depend on the sign bit being propagated: */
276         k = n >> LOG2_INTERVALS;
277         r1 = x - fn * L1;
278         r2 = fn * -L2;
279
280         /* Prepare scale factors. */
281         v.e = 1;
282         if (k >= LDBL_MIN_EXP) {
283                 v.xbits.expsign = BIAS + k;
284                 twopk = v.e;
285         } else {
286                 v.xbits.expsign = BIAS + k + 10000;
287                 twopkp10000 = v.e;
288         }
289
290         /* Evaluate expl(endpoint[n2] + r1 + r2) = tbl[n2] * expl(r1 + r2). */
291         z = r * r;
292         q = r2 + z * (A2 + r * A3) + z * z * (A4 + r * A5) + z * z * z * A6;
293         t = (long double)tbl[n2].lo + tbl[n2].hi;
294         t = tbl[n2].lo + t * (q + r1) + tbl[n2].hi;
295
296         /* Scale by 2**k. */
297         if (k >= LDBL_MIN_EXP) {
298                 if (k == LDBL_MAX_EXP)
299                         RETURNI(t * 2 * 0x1p16383L);
300                 RETURNI(t * twopk);
301         } else {
302                 RETURNI(t * twopkp10000 * twom10000);
303         }
304 }
305
306 /**
307  * Compute expm1l(x) for Intel 80-bit format.  This is based on:
308  *
309  *   PTP Tang, "Table-driven implementation of the Expm1 function
310  *   in IEEE floating-point arithmetic," ACM Trans. Math. Soft., 18,
311  *   211-222 (1992).
312  */
313
314 /*
315  * Our T1 and T2 are chosen to be approximately the points where method
316  * A and method B have the same accuracy.  Tang's T1 and T2 are the
317  * points where method A's accuracy changes by a full bit.  For Tang,
318  * this drop in accuracy makes method A immediately less accurate than
319  * method B, but our larger INTERVALS makes method A 2 bits more
320  * accurate so it remains the most accurate method significantly
321  * closer to the origin despite losing the full bit in our extended
322  * range for it.
323  */
324 static const double
325 T1 = -0.1659,                           /* ~-30.625/128 * log(2) */
326 T2 =  0.1659;                           /* ~30.625/128 * log(2) */
327
328 /*
329  * Domain [-0.1659, 0.1659], range ~[-1.2027e-22, 3.4417e-22]:
330  * |(exp(x)-1-x-x**2/2)/x - p(x)| < 2**-71.2
331  */
332 static const union IEEEl2bits
333 B3 = LD80C(0xaaaaaaaaaaaaaaab, -3,  1.66666666666666666671e-1L),
334 B4 = LD80C(0xaaaaaaaaaaaaaaac, -5,  4.16666666666666666712e-2L);
335
336 static const double
337 B5  =  8.3333333333333245e-3,           /*  0x1.111111111110cp-7 */
338 B6  =  1.3888888888888861e-3,           /*  0x1.6c16c16c16c0ap-10 */
339 B7  =  1.9841269841532042e-4,           /*  0x1.a01a01a0319f9p-13 */
340 B8  =  2.4801587302069236e-5,           /*  0x1.a01a01a03cbbcp-16 */
341 B9  =  2.7557316558468562e-6,           /*  0x1.71de37fd33d67p-19 */
342 B10 =  2.7557315829785151e-7,           /*  0x1.27e4f91418144p-22 */
343 B11 =  2.5063168199779829e-8,           /*  0x1.ae94fabdc6b27p-26 */
344 B12 =  2.0887164654459567e-9;           /*  0x1.1f122d6413fe1p-29 */
345
346 long double
347 expm1l(long double x)
348 {
349         union IEEEl2bits u, v;
350         long double fn, hx2_hi, hx2_lo, q, r, r1, r2, t, twomk, twopk, x_hi;
351         long double x_lo, x2, z;
352         long double x4;
353         int k, n, n2;
354         uint16_t hx, ix;
355
356         /* Filter out exceptional cases. */
357         u.e = x;
358         hx = u.xbits.expsign;
359         ix = hx & 0x7fff;
360         if (ix >= BIAS + 6) {           /* |x| >= 64 or x is NaN */
361                 if (ix == BIAS + LDBL_MAX_EXP) {
362                         if (hx & 0x8000)  /* x is -Inf, -NaN or unsupported */
363                                 return (-1 / x - 1);
364                         return (x + x); /* x is +Inf, +NaN or unsupported */
365                 }
366                 if (x > o_threshold)
367                         return (huge * huge);
368                 /*
369                  * expm1l() never underflows, but it must avoid
370                  * unrepresentable large negative exponents.  We used a
371                  * much smaller threshold for large |x| above than in
372                  * expl() so as to handle not so large negative exponents
373                  * in the same way as large ones here.
374                  */
375                 if (hx & 0x8000)        /* x <= -64 */
376                         return (tiny - 1);      /* good for x < -65ln2 - eps */
377         }
378
379         ENTERI();
380
381         if (T1 < x && x < T2) {
382                 if (ix < BIAS - 64) {   /* |x| < 0x1p-64 (includes pseudos) */
383                         /* x (rounded) with inexact if x != 0: */
384                         RETURNI(x == 0 ? x :
385                             (0x1p100 * x + fabsl(x)) * 0x1p-100);
386                 }
387
388                 x2 = x * x;
389                 x4 = x2 * x2;
390                 q = x4 * (x2 * (x4 *
391                     /*
392                      * XXX the number of terms is no longer good for
393                      * pairwise grouping of all except B3, and the
394                      * grouping is no longer from highest down.
395                      */
396                     (x2 *            B12  + (x * B11 + B10)) +
397                     (x2 * (x * B9 +  B8) +  (x * B7 +  B6))) +
398                           (x * B5 +  B4.e)) + x2 * x * B3.e;
399
400                 x_hi = (float)x;
401                 x_lo = x - x_hi;
402                 hx2_hi = x_hi * x_hi / 2;
403                 hx2_lo = x_lo * (x + x_hi) / 2;
404                 if (ix >= BIAS - 7)
405                         RETURNI(hx2_lo + x_lo + q + (hx2_hi + x_hi));
406                 else
407                         RETURNI(hx2_lo + q + hx2_hi + x);
408         }
409
410         /* Reduce x to (k*ln2 + endpoint[n2] + r1 + r2). */
411         /* Use a specialized rint() to get fn.  Assume round-to-nearest. */
412         fn = x * INV_L + 0x1.8p63 - 0x1.8p63;
413 #if defined(HAVE_EFFICIENT_IRINTL)
414         n = irintl(fn);
415 #elif defined(HAVE_EFFICIENT_IRINT)
416         n = irint(fn);
417 #else
418         n = (int)fn;
419 #endif
420         n2 = (unsigned)n % INTERVALS;
421         k = n >> LOG2_INTERVALS;
422         r1 = x - fn * L1;
423         r2 = fn * -L2;
424         r = r1 + r2;
425
426         /* Prepare scale factor. */
427         v.e = 1;
428         v.xbits.expsign = BIAS + k;
429         twopk = v.e;
430
431         /*
432          * Evaluate lower terms of
433          * expl(endpoint[n2] + r1 + r2) = tbl[n2] * expl(r1 + r2).
434          */
435         z = r * r;
436         q = r2 + z * (A2 + r * A3) + z * z * (A4 + r * A5) + z * z * z * A6;
437
438         t = (long double)tbl[n2].lo + tbl[n2].hi;
439
440         if (k == 0) {
441                 t = tbl[n2].lo * (r1 + 1) + t * q + tbl[n2].hi * r1 +
442                     (tbl[n2].hi - 1);
443                 RETURNI(t);
444         }
445         if (k == -1) {
446                 t = tbl[n2].lo * (r1 + 1) + t * q + tbl[n2].hi * r1 + 
447                     (tbl[n2].hi - 2);
448                 RETURNI(t / 2);
449         }
450         if (k < -7) {
451                 t = tbl[n2].lo + t * (q + r1) + tbl[n2].hi;
452                 RETURNI(t * twopk - 1);
453         }
454         if (k > 2 * LDBL_MANT_DIG - 1) {
455                 t = tbl[n2].lo + t * (q + r1) + tbl[n2].hi;
456                 if (k == LDBL_MAX_EXP)
457                         RETURNI(t * 2 * 0x1p16383L - 1);
458                 RETURNI(t * twopk - 1);
459         }
460
461         v.xbits.expsign = BIAS - k;
462         twomk = v.e;
463
464         if (k > LDBL_MANT_DIG - 1)
465                 t = tbl[n2].lo - twomk + t * (q + r1) + tbl[n2].hi;
466         else
467                 t = tbl[n2].lo + t * (q + r1) + (tbl[n2].hi - twomk);
468         RETURNI(t * twopk);
469 }