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