|
1 | | -// origin: FreeBSD /usr/src/lib/msun/src/s_exp2f.c |
2 | | -//- |
3 | | -// Copyright (c) 2005 David Schultz <[email protected]> |
4 | | -// All rights reserved. |
5 | | -// |
6 | | -// Redistribution and use in source and binary forms, with or without |
7 | | -// modification, are permitted provided that the following conditions |
8 | | -// are met: |
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. |
14 | | -// |
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 |
25 | | -// SUCH DAMAGE. |
26 | | - |
27 | | -const TBLSIZE: usize = 16; |
28 | | - |
29 | | -static EXP2FT: [u64; TBLSIZE] = [ |
30 | | - 0x3fe6a09e667f3bcd, |
31 | | - 0x3fe7a11473eb0187, |
32 | | - 0x3fe8ace5422aa0db, |
33 | | - 0x3fe9c49182a3f090, |
34 | | - 0x3feae89f995ad3ad, |
35 | | - 0x3fec199bdd85529c, |
36 | | - 0x3fed5818dcfba487, |
37 | | - 0x3feea4afa2a490da, |
38 | | - 0x3ff0000000000000, |
39 | | - 0x3ff0b5586cf9890f, |
40 | | - 0x3ff172b83c7d517b, |
41 | | - 0x3ff2387a6e756238, |
42 | | - 0x3ff306fe0a31b715, |
43 | | - 0x3ff3dea64c123422, |
44 | | - 0x3ff4bfdad5362a27, |
45 | | - 0x3ff5ab07dd485429, |
46 | | -]; |
47 | | - |
48 | | -// exp2f(x): compute the base 2 exponential of x |
49 | | -// |
50 | | -// Accuracy: Peak error < 0.501 ulp; location of peak: -0.030110927. |
51 | | -// |
52 | | -// Method: (equally-spaced tables) |
53 | | -// |
54 | | -// Reduce x: |
55 | | -// x = k + y, for integer k and |y| <= 1/2. |
56 | | -// Thus we have exp2f(x) = 2**k * exp2(y). |
57 | | -// |
58 | | -// Reduce y: |
59 | | -// y = i/TBLSIZE + z for integer i near y * TBLSIZE. |
60 | | -// Thus we have exp2(y) = exp2(i/TBLSIZE) * exp2(z), |
61 | | -// with |z| <= 2**-(TBLSIZE+1). |
62 | | -// |
63 | | -// We compute exp2(i/TBLSIZE) via table lookup and exp2(z) via a |
64 | | -// degree-4 minimax polynomial with maximum error under 1.4 * 2**-33. |
65 | | -// Using double precision for everything except the reduction makes |
66 | | -// roundoff error insignificant and simplifies the scaling step. |
67 | | -// |
68 | | -// This method is due to Tang, but I do not use his suggested parameters: |
69 | | -// |
70 | | -// Tang, P. Table-driven Implementation of the Exponential Function |
71 | | -// in IEEE Floating-Point Arithmetic. TOMS 15(2), 144-157 (1989). |
| 1 | +/* wf_exp2.c -- float version of w_exp2.c. |
| 2 | + * Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected]. |
| 3 | + */ |
| 4 | + |
| 5 | +/* |
| 6 | + * ==================================================== |
| 7 | + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
| 8 | + * |
| 9 | + * Developed at SunPro, a Sun Microsystems, Inc. business. |
| 10 | + * Permission to use, copy, modify, and distribute this |
| 11 | + * software is freely granted, provided that this notice |
| 12 | + * is preserved. |
| 13 | + * ==================================================== |
| 14 | + */ |
| 15 | + |
| 16 | +use super::powf; |
72 | 17 |
|
73 | 18 | /// Exponential, base 2 (f32) |
74 | 19 | /// |
75 | 20 | /// Calculate `2^x`, that is, 2 raised to the power `x`. |
76 | 21 | #[inline] |
77 | 22 | #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] |
78 | | -pub fn exp2f(mut x: f32) -> f32 { |
79 | | - let redux = f32::from_bits(0x4b400000) / TBLSIZE as f32; |
80 | | - let p1 = f32::from_bits(0x3f317218); |
81 | | - let p2 = f32::from_bits(0x3e75fdf0); |
82 | | - let p3 = f32::from_bits(0x3d6359a4); |
83 | | - let p4 = f32::from_bits(0x3c1d964e); |
84 | | - |
85 | | - // double_t t, r, z; |
86 | | - // uint32_t ix, i0, k; |
87 | | - |
88 | | - let x1p127 = f32::from_bits(0x7f000000); |
| 23 | +pub fn exp2f(x: f32) -> f32 { |
| 24 | + powf(2f32, x) |
| 25 | +} |
89 | 26 |
|
90 | | - /* Filter out exceptional cases. */ |
91 | | - let ui = f32::to_bits(x); |
92 | | - let ix = ui & 0x7fffffff; |
93 | | - if ix > 0x42fc0000 { |
94 | | - /* |x| > 126 */ |
95 | | - if ix > 0x7f800000 { |
96 | | - /* NaN */ |
97 | | - return x; |
98 | | - } |
99 | | - if ui >= 0x43000000 && ui < 0x80000000 { |
100 | | - /* x >= 128 */ |
101 | | - x *= x1p127; |
102 | | - return x; |
103 | | - } |
104 | | - if ui >= 0x80000000 { |
105 | | - /* x < -126 */ |
106 | | - if ui >= 0xc3160000 || (ui & 0x0000ffff != 0) { |
107 | | - force_eval!(f32::from_bits(0x80000001) / x); |
108 | | - } |
109 | | - if ui >= 0xc3160000 { |
110 | | - /* x <= -150 */ |
111 | | - return 0.0; |
112 | | - } |
113 | | - } |
114 | | - } else if ix <= 0x33000000 { |
115 | | - /* |x| <= 0x1p-25 */ |
116 | | - return 1.0 + x; |
| 27 | +#[cfg(test)] |
| 28 | +mod tests { |
| 29 | + #[test] |
| 30 | + fn sanity_check() { |
| 31 | + assert_eq!(super::exp2f(1.1), 2.143547); |
117 | 32 | } |
118 | | - |
119 | | - /* Reduce x, computing z, i0, and k. */ |
120 | | - let ui = f32::to_bits(x + redux); |
121 | | - let mut i0 = ui; |
122 | | - i0 += TBLSIZE as u32 / 2; |
123 | | - let k = i0 / TBLSIZE as u32; |
124 | | - let ukf = f64::from_bits(((0x3ff + k) as u64) << 52); |
125 | | - i0 &= TBLSIZE as u32 - 1; |
126 | | - let mut uf = f32::from_bits(ui); |
127 | | - uf -= redux; |
128 | | - let z: f64 = (x - uf) as f64; |
129 | | - /* Compute r = exp2(y) = exp2ft[i0] * p(z). */ |
130 | | - let r: f64 = f64::from_bits(EXP2FT[i0 as usize]); |
131 | | - let t: f64 = r as f64 * z; |
132 | | - let r: f64 = r + t * (p1 as f64 + z * p2 as f64) + t * (z * z) * (p3 as f64 + z * p4 as f64); |
133 | | - |
134 | | - /* Scale by 2**k */ |
135 | | - (r * ukf) as f32 |
136 | 33 | } |
0 commit comments