2 * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: src/lib/msun/src/s_exp2f.c,v 1.1 2005/04/05 02:57:15 das Exp $");
31 #include "math_private.h"
34 #define TBLSIZE (1 << TBLBITS)
39 redux
= 0x1.8p23f
/ TBLSIZE
,
45 static const double exp2ft
[TBLSIZE
] = {
65 * exp2f(x): compute the base 2 exponential of x
67 * Accuracy: Peak error < 0.501 ulp; location of peak: -0.030110927.
69 * Method: (equally-spaced tables)
72 * x = 2**k + y, for integer k and |y| <= 1/2.
73 * Thus we have exp2f(x) = 2**k * exp2(y).
76 * y = i/TBLSIZE + z for integer i near y * TBLSIZE.
77 * Thus we have exp2(y) = exp2(i/TBLSIZE) * exp2(z),
78 * with |z| <= 2**-(TBLSIZE+1).
80 * We compute exp2(i/TBLSIZE) via table lookup and exp2(z) via a
81 * degree-4 minimax polynomial with maximum error under 1.4 * 2**-33.
82 * Using double precision in the final calculation avoids roundoff error.
84 * This method is due to Tang, but I do not use his suggested parameters:
86 * Tang, P. Table-driven Implementation of the Exponential Function
87 * in IEEE Floating-Point Arithmetic. TOMS 15(2), 144-157 (1989).
94 volatile float t
; /* prevent gcc from using too much precision */
95 uint32_t hx
, hr
, ix
, i0
;
98 /* Filter out exceptional cases. */
100 ix
= hx
& 0x7fffffff; /* high word of |x| */
101 if(ix
>= 0x43000000) { /* |x| >= 128 */
102 if(ix
>= 0x7f800000) {
103 if ((ix
& 0x7fffff) != 0 || (hx
& 0x80000000) == 0)
104 return (x
); /* x is NaN or +Inf */
106 return (0.0); /* x is -Inf */
109 return (huge
* huge
); /* overflow */
111 return (twom100
* twom100
); /* underflow */
112 } else if (ix
<= 0x33000000) { /* |x| <= 0x1p-25 */
116 /* Reduce x, computing z, i0, and k. */
118 GET_FLOAT_WORD(i0
, t
);
120 k
= (i0
>> TBLBITS
) << 23;
125 /* Compute r = exp2(y) = exp2ft[i0] * p(z). */
127 r
= tv
+ tv
* (z
* (P1
+ z
* (P2
+ z
* (P3
+ z
* P4
))));
129 /* Scale by 2**(k>>23). */
130 if(k
>= -125 << 23) {
132 GET_FLOAT_WORD(hr
, r
);
133 SET_FLOAT_WORD(r
, hr
+ k
);
137 GET_FLOAT_WORD(hr
, r
);
138 SET_FLOAT_WORD(r
, hr
+ (k
+ (100 << 23)));
139 return (r
* twom100
);