/rust/registry/src/index.crates.io-1949cf8c6b5b557f/libm-0.2.16/src/math/expf.rs
Line | Count | Source |
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 | | #[cfg_attr(assert_no_panic, no_panic::no_panic)] |
34 | 0 | pub fn expf(mut x: f32) -> f32 { |
35 | | select_implementation! { |
36 | | name: x87_expf, |
37 | | use_arch_required: x86_no_sse, |
38 | | args: x, |
39 | | } |
40 | | |
41 | 0 | let x1p127 = f32::from_bits(0x7f000000); // 0x1p127f === 2 ^ 127 |
42 | 0 | let x1p_126 = f32::from_bits(0x800000); // 0x1p-126f === 2 ^ -126 /*original 0x1p-149f ??????????? */ |
43 | 0 | let mut hx = x.to_bits(); |
44 | 0 | let sign = (hx >> 31) as i32; /* sign bit of x */ |
45 | 0 | let signb: bool = sign != 0; |
46 | 0 | hx &= 0x7fffffff; /* high word of |x| */ |
47 | | |
48 | | /* special cases */ |
49 | 0 | if hx >= 0x42aeac50 { |
50 | | /* if |x| >= -87.33655f or NaN */ |
51 | 0 | if hx > 0x7f800000 { |
52 | | /* NaN */ |
53 | 0 | return x; |
54 | 0 | } |
55 | 0 | if (hx >= 0x42b17218) && (!signb) { |
56 | | /* x >= 88.722839f */ |
57 | | /* overflow */ |
58 | 0 | x *= x1p127; |
59 | 0 | return x; |
60 | 0 | } |
61 | 0 | if signb { |
62 | | /* underflow */ |
63 | 0 | force_eval!(-x1p_126 / x); |
64 | 0 | if hx >= 0x42cff1b5 { |
65 | | /* x <= -103.972084f */ |
66 | 0 | return 0.; |
67 | 0 | } |
68 | 0 | } |
69 | 0 | } |
70 | | |
71 | | /* argument reduction */ |
72 | | let k: i32; |
73 | | let hi: f32; |
74 | | let lo: f32; |
75 | 0 | if hx > 0x3eb17218 { |
76 | | /* if |x| > 0.5 ln2 */ |
77 | 0 | if hx > 0x3f851592 { |
78 | 0 | /* if |x| > 1.5 ln2 */ |
79 | 0 | k = (INV_LN2 * x + i!(HALF, sign as usize)) as i32; |
80 | 0 | } else { |
81 | 0 | k = 1 - sign - sign; |
82 | 0 | } |
83 | 0 | let kf = k as f32; |
84 | 0 | hi = x - kf * LN2_HI; /* k*ln2hi is exact here */ |
85 | 0 | lo = kf * LN2_LO; |
86 | 0 | x = hi - lo; |
87 | 0 | } else if hx > 0x39000000 { |
88 | 0 | /* |x| > 2**-14 */ |
89 | 0 | k = 0; |
90 | 0 | hi = x; |
91 | 0 | lo = 0.; |
92 | 0 | } else { |
93 | | /* raise inexact */ |
94 | 0 | force_eval!(x1p127 + x); |
95 | 0 | return 1. + x; |
96 | | } |
97 | | |
98 | | /* x is now in primary range */ |
99 | 0 | let xx = x * x; |
100 | 0 | let c = x - xx * (P1 + xx * P2); |
101 | 0 | let y = 1. + (x * c / (2. - c) - lo + hi); |
102 | 0 | if k == 0 { y } else { scalbnf(y, k) } |
103 | 0 | } |