1*05a0b428SJohn Marino /* $OpenBSD: s_exp2f.c,v 1.2 2012/12/05 23:20:04 deraadt Exp $ */
2*05a0b428SJohn Marino /*-
3*05a0b428SJohn Marino * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
4*05a0b428SJohn Marino * All rights reserved.
5*05a0b428SJohn Marino *
6*05a0b428SJohn Marino * Redistribution and use in source and binary forms, with or without
7*05a0b428SJohn Marino * modification, are permitted provided that the following conditions
8*05a0b428SJohn Marino * are met:
9*05a0b428SJohn Marino * 1. Redistributions of source code must retain the above copyright
10*05a0b428SJohn Marino * notice, this list of conditions and the following disclaimer.
11*05a0b428SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
12*05a0b428SJohn Marino * notice, this list of conditions and the following disclaimer in the
13*05a0b428SJohn Marino * documentation and/or other materials provided with the distribution.
14*05a0b428SJohn Marino *
15*05a0b428SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*05a0b428SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*05a0b428SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*05a0b428SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*05a0b428SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*05a0b428SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*05a0b428SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*05a0b428SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*05a0b428SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*05a0b428SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*05a0b428SJohn Marino * SUCH DAMAGE.
26*05a0b428SJohn Marino */
27*05a0b428SJohn Marino
28*05a0b428SJohn Marino #include <float.h>
29*05a0b428SJohn Marino
30*05a0b428SJohn Marino #include "math.h"
31*05a0b428SJohn Marino #include "math_private.h"
32*05a0b428SJohn Marino
33*05a0b428SJohn Marino #define TBLBITS 4
34*05a0b428SJohn Marino #define TBLSIZE (1 << TBLBITS)
35*05a0b428SJohn Marino
36*05a0b428SJohn Marino static const float
37*05a0b428SJohn Marino huge = 0x1p100f,
38*05a0b428SJohn Marino redux = 0x1.8p23f / TBLSIZE,
39*05a0b428SJohn Marino P1 = 0x1.62e430p-1f,
40*05a0b428SJohn Marino P2 = 0x1.ebfbe0p-3f,
41*05a0b428SJohn Marino P3 = 0x1.c6b348p-5f,
42*05a0b428SJohn Marino P4 = 0x1.3b2c9cp-7f;
43*05a0b428SJohn Marino
44*05a0b428SJohn Marino static volatile float twom100 = 0x1p-100f;
45*05a0b428SJohn Marino
46*05a0b428SJohn Marino static const double exp2ft[TBLSIZE] = {
47*05a0b428SJohn Marino 0x1.6a09e667f3bcdp-1,
48*05a0b428SJohn Marino 0x1.7a11473eb0187p-1,
49*05a0b428SJohn Marino 0x1.8ace5422aa0dbp-1,
50*05a0b428SJohn Marino 0x1.9c49182a3f090p-1,
51*05a0b428SJohn Marino 0x1.ae89f995ad3adp-1,
52*05a0b428SJohn Marino 0x1.c199bdd85529cp-1,
53*05a0b428SJohn Marino 0x1.d5818dcfba487p-1,
54*05a0b428SJohn Marino 0x1.ea4afa2a490dap-1,
55*05a0b428SJohn Marino 0x1.0000000000000p+0,
56*05a0b428SJohn Marino 0x1.0b5586cf9890fp+0,
57*05a0b428SJohn Marino 0x1.172b83c7d517bp+0,
58*05a0b428SJohn Marino 0x1.2387a6e756238p+0,
59*05a0b428SJohn Marino 0x1.306fe0a31b715p+0,
60*05a0b428SJohn Marino 0x1.3dea64c123422p+0,
61*05a0b428SJohn Marino 0x1.4bfdad5362a27p+0,
62*05a0b428SJohn Marino 0x1.5ab07dd485429p+0,
63*05a0b428SJohn Marino };
64*05a0b428SJohn Marino
65*05a0b428SJohn Marino /*
66*05a0b428SJohn Marino * exp2f(x): compute the base 2 exponential of x
67*05a0b428SJohn Marino *
68*05a0b428SJohn Marino * Accuracy: Peak error < 0.501 ulp; location of peak: -0.030110927.
69*05a0b428SJohn Marino *
70*05a0b428SJohn Marino * Method: (equally-spaced tables)
71*05a0b428SJohn Marino *
72*05a0b428SJohn Marino * Reduce x:
73*05a0b428SJohn Marino * x = 2**k + y, for integer k and |y| <= 1/2.
74*05a0b428SJohn Marino * Thus we have exp2f(x) = 2**k * exp2(y).
75*05a0b428SJohn Marino *
76*05a0b428SJohn Marino * Reduce y:
77*05a0b428SJohn Marino * y = i/TBLSIZE + z for integer i near y * TBLSIZE.
78*05a0b428SJohn Marino * Thus we have exp2(y) = exp2(i/TBLSIZE) * exp2(z),
79*05a0b428SJohn Marino * with |z| <= 2**-(TBLSIZE+1).
80*05a0b428SJohn Marino *
81*05a0b428SJohn Marino * We compute exp2(i/TBLSIZE) via table lookup and exp2(z) via a
82*05a0b428SJohn Marino * degree-4 minimax polynomial with maximum error under 1.4 * 2**-33.
83*05a0b428SJohn Marino * Using double precision for everything except the reduction makes
84*05a0b428SJohn Marino * roundoff error insignificant and simplifies the scaling step.
85*05a0b428SJohn Marino *
86*05a0b428SJohn Marino * This method is due to Tang, but I do not use his suggested parameters:
87*05a0b428SJohn Marino *
88*05a0b428SJohn Marino * Tang, P. Table-driven Implementation of the Exponential Function
89*05a0b428SJohn Marino * in IEEE Floating-Point Arithmetic. TOMS 15(2), 144-157 (1989).
90*05a0b428SJohn Marino */
91*05a0b428SJohn Marino float
exp2f(float x)92*05a0b428SJohn Marino exp2f(float x)
93*05a0b428SJohn Marino {
94*05a0b428SJohn Marino double tv, twopk, u, z;
95*05a0b428SJohn Marino float t;
96*05a0b428SJohn Marino uint32_t hx, ix, i0;
97*05a0b428SJohn Marino int32_t k;
98*05a0b428SJohn Marino
99*05a0b428SJohn Marino /* Filter out exceptional cases. */
100*05a0b428SJohn Marino GET_FLOAT_WORD(hx, x);
101*05a0b428SJohn Marino ix = hx & 0x7fffffff; /* high word of |x| */
102*05a0b428SJohn Marino if(ix >= 0x43000000) { /* |x| >= 128 */
103*05a0b428SJohn Marino if(ix >= 0x7f800000) {
104*05a0b428SJohn Marino if ((ix & 0x7fffff) != 0 || (hx & 0x80000000) == 0)
105*05a0b428SJohn Marino return (x + x); /* x is NaN or +Inf */
106*05a0b428SJohn Marino else
107*05a0b428SJohn Marino return (0.0); /* x is -Inf */
108*05a0b428SJohn Marino }
109*05a0b428SJohn Marino if(x >= 0x1.0p7f)
110*05a0b428SJohn Marino return (huge * huge); /* overflow */
111*05a0b428SJohn Marino if(x <= -0x1.2cp7f)
112*05a0b428SJohn Marino return (twom100 * twom100); /* underflow */
113*05a0b428SJohn Marino } else if (ix <= 0x33000000) { /* |x| <= 0x1p-25 */
114*05a0b428SJohn Marino return (1.0f + x);
115*05a0b428SJohn Marino }
116*05a0b428SJohn Marino
117*05a0b428SJohn Marino /* Reduce x, computing z, i0, and k. */
118*05a0b428SJohn Marino STRICT_ASSIGN(float, t, x + redux);
119*05a0b428SJohn Marino GET_FLOAT_WORD(i0, t);
120*05a0b428SJohn Marino i0 += TBLSIZE / 2;
121*05a0b428SJohn Marino k = (i0 >> TBLBITS) << 20;
122*05a0b428SJohn Marino i0 &= TBLSIZE - 1;
123*05a0b428SJohn Marino t -= redux;
124*05a0b428SJohn Marino z = x - t;
125*05a0b428SJohn Marino INSERT_WORDS(twopk, 0x3ff00000 + k, 0);
126*05a0b428SJohn Marino
127*05a0b428SJohn Marino /* Compute r = exp2(y) = exp2ft[i0] * p(z). */
128*05a0b428SJohn Marino tv = exp2ft[i0];
129*05a0b428SJohn Marino u = tv * z;
130*05a0b428SJohn Marino tv = tv + u * (P1 + z * P2) + u * (z * z) * (P3 + z * P4);
131*05a0b428SJohn Marino
132*05a0b428SJohn Marino /* Scale by 2**(k>>20). */
133*05a0b428SJohn Marino return (tv * twopk);
134*05a0b428SJohn Marino }
135