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