1 /* origin: FreeBSD /usr/src/lib/msun/src/e_expf.c */
2 /*
3  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
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::scalbnf;
17 
18 const HALF: [f32; 2] = [0.5, -0.5];
19 const LN2_HI: f32 = 6.9314575195e-01; /* 0x3f317200 */
20 const LN2_LO: f32 = 1.4286067653e-06; /* 0x35bfbe8e */
21 const INV_LN2: f32 = 1.4426950216e+00; /* 0x3fb8aa3b */
22 /*
23  * Domain [-0.34568, 0.34568], range ~[-4.278e-9, 4.447e-9]:
24  * |x*(exp(x)+1)/(exp(x)-1) - p(x)| < 2**-27.74
25  */
26 const P1: f32 = 1.6666625440e-1; /*  0xaaaa8f.0p-26 */
27 const P2: f32 = -2.7667332906e-3; /* -0xb55215.0p-32 */
28 
29 /// Exponential, base *e* (f32)
30 ///
31 /// Calculate the exponential of `x`, that is, *e* raised to the power `x`
32 /// (where *e* is the base of the natural system of logarithms, approximately 2.71828).
33 #[inline]
34 #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
expf(mut x: f32) -> f3235 pub fn expf(mut x: f32) -> f32 {
36     let x1p127 = f32::from_bits(0x7f000000); // 0x1p127f === 2 ^ 127
37     let x1p_126 = f32::from_bits(0x800000); // 0x1p-126f === 2 ^ -126  /*original 0x1p-149f    ??????????? */
38     let mut hx = x.to_bits();
39     let sign = (hx >> 31) as i32; /* sign bit of x */
40     let signb: bool = sign != 0;
41     hx &= 0x7fffffff; /* high word of |x| */
42 
43     /* special cases */
44     if hx >= 0x42aeac50 {
45         /* if |x| >= -87.33655f or NaN */
46         if hx > 0x7f800000 {
47             /* NaN */
48             return x;
49         }
50         if (hx >= 0x42b17218) && (!signb) {
51             /* x >= 88.722839f */
52             /* overflow */
53             x *= x1p127;
54             return x;
55         }
56         if signb {
57             /* underflow */
58             force_eval!(-x1p_126 / x);
59             if hx >= 0x42cff1b5 {
60                 /* x <= -103.972084f */
61                 return 0.;
62             }
63         }
64     }
65 
66     /* argument reduction */
67     let k: i32;
68     let hi: f32;
69     let lo: f32;
70     if hx > 0x3eb17218 {
71         /* if |x| > 0.5 ln2 */
72         if hx > 0x3f851592 {
73             /* if |x| > 1.5 ln2 */
74             k = (INV_LN2 * x + HALF[sign as usize]) as i32;
75         } else {
76             k = 1 - sign - sign;
77         }
78         let kf = k as f32;
79         hi = x - kf * LN2_HI; /* k*ln2hi is exact here */
80         lo = kf * LN2_LO;
81         x = hi - lo;
82     } else if hx > 0x39000000 {
83         /* |x| > 2**-14 */
84         k = 0;
85         hi = x;
86         lo = 0.;
87     } else {
88         /* raise inexact */
89         force_eval!(x1p127 + x);
90         return 1. + x;
91     }
92 
93     /* x is now in primary range */
94     let xx = x * x;
95     let c = x - xx * (P1 + xx * P2);
96     let y = 1. + (x * c / (2. - c) - lo + hi);
97     if k == 0 {
98         y
99     } else {
100         scalbnf(y, k)
101     }
102 }
103