/rust/registry/src/index.crates.io-1949cf8c6b5b557f/pxfm-0.1.25/src/hyperbolic/atanhf.rs
Line | Count | Source |
1 | | /* |
2 | | * // Copyright (c) Radzivon Bartoshyk 7/2025. All rights reserved. |
3 | | * // |
4 | | * // Redistribution and use in source and binary forms, with or without modification, |
5 | | * // are permitted provided that the following conditions are met: |
6 | | * // |
7 | | * // 1. Redistributions of source code must retain the above copyright notice, this |
8 | | * // list of conditions and the following disclaimer. |
9 | | * // |
10 | | * // 2. Redistributions in binary form must reproduce the above copyright notice, |
11 | | * // this list of conditions and the following disclaimer in the documentation |
12 | | * // and/or other materials provided with the distribution. |
13 | | * // |
14 | | * // 3. Neither the name of the copyright holder nor the names of its |
15 | | * // contributors may be used to endorse or promote products derived from |
16 | | * // this software without specific prior written permission. |
17 | | * // |
18 | | * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
19 | | * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | | * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
21 | | * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
22 | | * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 | | * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
24 | | * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
25 | | * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
26 | | * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | | * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | */ |
29 | | use crate::common::f_fmla; |
30 | | use crate::hyperbolic::asinhf::log_eval; |
31 | | use crate::polyeval::f_polyeval6; |
32 | | |
33 | | /// Hyperbolic atan |
34 | | /// |
35 | | /// Max ULP 0.5 |
36 | | #[inline] |
37 | 0 | pub fn f_atanhf(x: f32) -> f32 { |
38 | 0 | let x_abs = x.to_bits() & 0x7fff_ffff; |
39 | | |
40 | | // |x| >= 1.0 |
41 | 0 | if x_abs >= 0x3F80_0000u32 { |
42 | 0 | if x.is_nan() { |
43 | 0 | return x; |
44 | 0 | } |
45 | | // |x| == 1.0 |
46 | 0 | return if x_abs == 0x3F80_0000u32 { |
47 | 0 | if x.is_sign_positive() { |
48 | 0 | f32::INFINITY |
49 | | } else { |
50 | 0 | f32::NEG_INFINITY |
51 | | } |
52 | | } else { |
53 | 0 | f32::NAN |
54 | | }; |
55 | 0 | } |
56 | | |
57 | | // |x| < ~0.10 |
58 | 0 | if x_abs <= 0x3dcc_0000u32 { |
59 | | // |x| <= 2^-26 |
60 | 0 | if x_abs <= 0x3280_0000u32 { |
61 | 0 | return if x_abs == 0 { |
62 | 0 | x |
63 | | } else { |
64 | 0 | (x as f64 + f64::from_bits(0x3fd5555555555555) * x as f64 * x as f64 * x as f64) |
65 | 0 | as f32 |
66 | | }; |
67 | 0 | } |
68 | | |
69 | 0 | let xdbl = x as f64; |
70 | 0 | let x2 = xdbl * xdbl; |
71 | | // Pure Taylor series. |
72 | 0 | let pe = f_polyeval6( |
73 | 0 | x2, |
74 | | 0.0, |
75 | 0 | f64::from_bits(0x3fd5555555555555), |
76 | 0 | f64::from_bits(0x3fc999999999999a), |
77 | 0 | f64::from_bits(0x3fc2492492492492), |
78 | 0 | f64::from_bits(0x3fbc71c71c71c71c), |
79 | 0 | f64::from_bits(0x3fb745d1745d1746), |
80 | | ); |
81 | 0 | return f_fmla(xdbl, pe, xdbl) as f32; |
82 | 0 | } |
83 | 0 | let xdbl = x as f64; |
84 | 0 | (0.5 * log_eval((xdbl + 1.0) / (xdbl - 1.0))) as f32 |
85 | 0 | } |
86 | | |
87 | | #[cfg(test)] |
88 | | mod tests { |
89 | | use super::*; |
90 | | #[test] |
91 | | fn test_atanhf() { |
92 | | assert_eq!(f_atanhf(0.0), 0.0); |
93 | | assert_eq!(f_atanhf(1.0), f32::INFINITY); |
94 | | assert!(f_atanhf(-1.5).is_nan()); |
95 | | assert_eq!(f_atanhf(0.25), 0.25541282); |
96 | | assert_eq!(f_atanhf(0.124121), 0.12476436); |
97 | | } |
98 | | } |