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