libm/math/
tanhf.rs

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