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