/rust/registry/src/index.crates.io-6f17d22bba15001f/libm-0.2.11/src/math/expm1f.rs
Line | Count | Source (jump to first uncovered line) |
1 | | /* origin: FreeBSD /usr/src/lib/msun/src/s_expm1f.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 | | const O_THRESHOLD: f32 = 8.8721679688e+01; /* 0x42b17180 */ |
17 | | const LN2_HI: f32 = 6.9313812256e-01; /* 0x3f317180 */ |
18 | | const LN2_LO: f32 = 9.0580006145e-06; /* 0x3717f7d1 */ |
19 | | const INV_LN2: f32 = 1.4426950216e+00; /* 0x3fb8aa3b */ |
20 | | /* |
21 | | * Domain [-0.34568, 0.34568], range ~[-6.694e-10, 6.696e-10]: |
22 | | * |6 / x * (1 + 2 * (1 / (exp(x) - 1) - 1 / x)) - q(x)| < 2**-30.04 |
23 | | * Scaled coefficients: Qn_here = 2**n * Qn_for_q (see s_expm1.c): |
24 | | */ |
25 | | const Q1: f32 = -3.3333212137e-2; /* -0x888868.0p-28 */ |
26 | | const Q2: f32 = 1.5807170421e-3; /* 0xcf3010.0p-33 */ |
27 | | |
28 | | /// Exponential, base *e*, of x-1 (f32) |
29 | | /// |
30 | | /// Calculates the exponential of `x` and subtract 1, that is, *e* raised |
31 | | /// to the power `x` minus 1 (where *e* is the base of the natural |
32 | | /// system of logarithms, approximately 2.71828). |
33 | | /// The result is accurate even for small values of `x`, |
34 | | /// where using `exp(x)-1` would lose many significant digits. |
35 | | #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] |
36 | 0 | pub fn expm1f(mut x: f32) -> f32 { |
37 | 0 | let x1p127 = f32::from_bits(0x7f000000); // 0x1p127f === 2 ^ 127 |
38 | 0 |
|
39 | 0 | let mut hx = x.to_bits(); |
40 | 0 | let sign = (hx >> 31) != 0; |
41 | 0 | hx &= 0x7fffffff; |
42 | 0 |
|
43 | 0 | /* filter out huge and non-finite argument */ |
44 | 0 | if hx >= 0x4195b844 { |
45 | | /* if |x|>=27*ln2 */ |
46 | 0 | if hx > 0x7f800000 { |
47 | | /* NaN */ |
48 | 0 | return x; |
49 | 0 | } |
50 | 0 | if sign { |
51 | 0 | return -1.; |
52 | 0 | } |
53 | 0 | if x > O_THRESHOLD { |
54 | 0 | x *= x1p127; |
55 | 0 | return x; |
56 | 0 | } |
57 | 0 | } |
58 | | |
59 | | let k: i32; |
60 | | let hi: f32; |
61 | | let lo: f32; |
62 | 0 | let mut c = 0f32; |
63 | 0 | /* argument reduction */ |
64 | 0 | if hx > 0x3eb17218 { |
65 | | /* if |x| > 0.5 ln2 */ |
66 | 0 | if hx < 0x3F851592 { |
67 | | /* and |x| < 1.5 ln2 */ |
68 | 0 | if !sign { |
69 | 0 | hi = x - LN2_HI; |
70 | 0 | lo = LN2_LO; |
71 | 0 | k = 1; |
72 | 0 | } else { |
73 | 0 | hi = x + LN2_HI; |
74 | 0 | lo = -LN2_LO; |
75 | 0 | k = -1; |
76 | 0 | } |
77 | | } else { |
78 | 0 | k = (INV_LN2 * x + (if sign { -0.5 } else { 0.5 })) as i32; |
79 | 0 | let t = k as f32; |
80 | 0 | hi = x - t * LN2_HI; /* t*ln2_hi is exact here */ |
81 | 0 | lo = t * LN2_LO; |
82 | | } |
83 | 0 | x = hi - lo; |
84 | 0 | c = (hi - x) - lo; |
85 | 0 | } else if hx < 0x33000000 { |
86 | | /* when |x|<2**-25, return x */ |
87 | 0 | if hx < 0x00800000 { |
88 | 0 | force_eval!(x * x); |
89 | 0 | } |
90 | 0 | return x; |
91 | 0 | } else { |
92 | 0 | k = 0; |
93 | 0 | } |
94 | | |
95 | | /* x is now in primary range */ |
96 | 0 | let hfx = 0.5 * x; |
97 | 0 | let hxs = x * hfx; |
98 | 0 | let r1 = 1. + hxs * (Q1 + hxs * Q2); |
99 | 0 | let t = 3. - r1 * hfx; |
100 | 0 | let mut e = hxs * ((r1 - t) / (6. - x * t)); |
101 | 0 | if k == 0 { |
102 | | /* c is 0 */ |
103 | 0 | return x - (x * e - hxs); |
104 | 0 | } |
105 | 0 | e = x * (e - c) - c; |
106 | 0 | e -= hxs; |
107 | 0 | /* exp(x) ~ 2^k (x_reduced - e + 1) */ |
108 | 0 | if k == -1 { |
109 | 0 | return 0.5 * (x - e) - 0.5; |
110 | 0 | } |
111 | 0 | if k == 1 { |
112 | 0 | if x < -0.25 { |
113 | 0 | return -2. * (e - (x + 0.5)); |
114 | 0 | } |
115 | 0 | return 1. + 2. * (x - e); |
116 | 0 | } |
117 | 0 | let twopk = f32::from_bits(((0x7f + k) << 23) as u32); /* 2^k */ |
118 | 0 | if (k < 0) || (k > 56) { |
119 | | /* suffice to return exp(x)-1 */ |
120 | 0 | let mut y = x - e + 1.; |
121 | 0 | if k == 128 { |
122 | 0 | y = y * 2. * x1p127; |
123 | 0 | } else { |
124 | 0 | y = y * twopk; |
125 | 0 | } |
126 | 0 | return y - 1.; |
127 | 0 | } |
128 | 0 | let uf = f32::from_bits(((0x7f - k) << 23) as u32); /* 2^-k */ |
129 | 0 | if k < 23 { (x - e + (1. - uf)) * twopk } else { (x - (e + uf) + 1.) * twopk } |
130 | 0 | } |