1 | use super::expm1f; |
2 | |
3 | /// The hyperbolic tangent of `x` (f32). |
4 | /// |
5 | /// `x` is specified in radians. |
6 | #[cfg_attr (all(test, assert_no_panic), no_panic::no_panic)] |
7 | pub fn tanhf(mut x: f32) -> f32 { |
8 | /* x = |x| */ |
9 | let mut ix = x.to_bits(); |
10 | let sign = (ix >> 31) != 0; |
11 | ix &= 0x7fffffff; |
12 | x = f32::from_bits(ix); |
13 | let w = ix; |
14 | |
15 | let tt = if w > 0x3f0c9f54 { |
16 | /* |x| > log(3)/2 ~= 0.5493 or nan */ |
17 | if w > 0x41200000 { |
18 | /* |x| > 10 */ |
19 | 1. + 0. / x |
20 | } else { |
21 | let t = expm1f(2. * x); |
22 | 1. - 2. / (t + 2.) |
23 | } |
24 | } else if w > 0x3e82c578 { |
25 | /* |x| > log(5/3)/2 ~= 0.2554 */ |
26 | let t = expm1f(2. * x); |
27 | t / (t + 2.) |
28 | } else if w >= 0x00800000 { |
29 | /* |x| >= 0x1p-126 */ |
30 | let t = expm1f(-2. * x); |
31 | -t / (t + 2.) |
32 | } else { |
33 | /* |x| is subnormal */ |
34 | force_eval!(x * x); |
35 | x |
36 | }; |
37 | if sign { -tt } else { tt } |
38 | } |
39 | |