| 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 | pub fn f_atanhf(x: f32) -> f32 { |
| 38 | let x_abs = x.to_bits() & 0x7fff_ffff; |
| 39 | |
| 40 | // |x| >= 1.0 |
| 41 | if x_abs >= 0x3F80_0000u32 { |
| 42 | if x.is_nan() { |
| 43 | return x; |
| 44 | } |
| 45 | // |x| == 1.0 |
| 46 | return if x_abs == 0x3F80_0000u32 { |
| 47 | if x.is_sign_positive() { |
| 48 | f32::INFINITY |
| 49 | } else { |
| 50 | f32::NEG_INFINITY |
| 51 | } |
| 52 | } else { |
| 53 | f32::NAN |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | // |x| < ~0.10 |
| 58 | if x_abs <= 0x3dcc_0000u32 { |
| 59 | // |x| <= 2^-26 |
| 60 | if x_abs <= 0x3280_0000u32 { |
| 61 | return if x_abs == 0 { |
| 62 | x |
| 63 | } else { |
| 64 | (x as f64 + f64::from_bits(0x3fd5555555555555) * x as f64 * x as f64 * x as f64) |
| 65 | as f32 |
| 66 | }; |
| 67 | } |
| 68 | |
| 69 | let xdbl = x as f64; |
| 70 | let x2 = xdbl * xdbl; |
| 71 | // Pure Taylor series. |
| 72 | let pe = f_polyeval6( |
| 73 | x2, |
| 74 | 0.0, |
| 75 | f64::from_bits(0x3fd5555555555555), |
| 76 | f64::from_bits(0x3fc999999999999a), |
| 77 | f64::from_bits(0x3fc2492492492492), |
| 78 | f64::from_bits(0x3fbc71c71c71c71c), |
| 79 | f64::from_bits(0x3fb745d1745d1746), |
| 80 | ); |
| 81 | return f_fmla(xdbl, pe, xdbl) as f32; |
| 82 | } |
| 83 | let xdbl = x as f64; |
| 84 | (0.5 * log_eval((xdbl + 1.0) / (xdbl - 1.0))) as f32 |
| 85 | } |
| 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 | } |