1 /* origin: FreeBSD /usr/src/lib/msun/src/s_exp2f.c */
3 * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following 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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 redux = 0x1.8p23f / TBLSIZE,
39 static const double exp2ft[TBLSIZE] = {
59 * exp2f(x): compute the base 2 exponential of x
61 * Accuracy: Peak error < 0.501 ulp; location of peak: -0.030110927.
63 * Method: (equally-spaced tables)
66 * x = 2**k + y, for integer k and |y| <= 1/2.
67 * Thus we have exp2f(x) = 2**k * exp2(y).
70 * y = i/TBLSIZE + z for integer i near y * TBLSIZE.
71 * Thus we have exp2(y) = exp2(i/TBLSIZE) * exp2(z),
72 * with |z| <= 2**-(TBLSIZE+1).
74 * We compute exp2(i/TBLSIZE) via table lookup and exp2(z) via a
75 * degree-4 minimax polynomial with maximum error under 1.4 * 2**-33.
76 * Using double precision for everything except the reduction makes
77 * roundoff error insignificant and simplifies the scaling step.
79 * This method is due to Tang, but I do not use his suggested parameters:
81 * Tang, P. Table-driven Implementation of the Exponential Function
82 * in IEEE Floating-Point Arithmetic. TOMS 15(2), 144-157 (1989).
86 double tv, twopk, u, z;
88 uint32_t hx, ix, i0, k;
90 /* Filter out exceptional cases. */
91 GET_FLOAT_WORD(hx, x);
93 if (ix >= 0x43000000) { /* |x| >= 128 */
94 if (ix >= 0x7f800000) {
95 if (hx == 0xff800000) /* -inf */
100 STRICT_ASSIGN(float, x, x * 0x1p127);
104 STRICT_ASSIGN(float, x, 0x1p-100*0x1p-100);
107 } else if (ix <= 0x33000000) { /* |x| <= 0x1p-25 */
111 /* Reduce x, computing z, i0, and k. */
112 STRICT_ASSIGN(float, t, x + redux);
113 GET_FLOAT_WORD(i0, t);
115 k = (i0 / TBLSIZE) << 20;
119 INSERT_WORDS(twopk, 0x3ff00000 + k, 0);
121 /* Compute r = exp2(y) = exp2ft[i0] * p(z). */
124 tv = tv + u * (P1 + z * P2) + u * (z * z) * (P3 + z * P4);
126 /* Scale by 2**(k>>20). */