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