pxfm/tangent/
atanpif.rs

1/*
2 * // Copyright (c) Radzivon Bartoshyk 6/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 */
29use crate::common::f_fmla;
30
31/// Computes atan(x)/PI
32///
33/// Max ULP 0.5
34#[inline]
35pub fn f_atanpif(x: f32) -> f32 {
36    let t = x.to_bits();
37    let e: i32 = ((t >> 23) & 0xff) as i32;
38    let gt = e >= 127;
39    if e > 127 + 24 {
40        // |x| >= 2^25
41        let f = f32::copysign(0.5, x);
42        if e == 0xff {
43            if (t.wrapping_shl(9)) != 0 {
44                return x + x;
45            } // nan
46            return f; // inf
47        }
48        // Warning: 0x1.45f306p-2f / x underflows for |x| >= 0x1.45f306p+124
49        return if x.abs() >= f32::from_bits(0x7da2f983) {
50            f - f32::copysign(f32::from_bits(0x32800000), x)
51        } else {
52            f - f32::from_bits(0x3ea2f983) / x
53        };
54    }
55    let mut z = x as f64;
56    if e < 127 - 13 {
57        // |x| < 2^-13
58        let sx = z * f64::from_bits(0x3fd45f306dc9c883);
59        if e < 127 - 25 {
60            // |x| < 2^-25
61            return sx as f32;
62        }
63        let zz0 = sx - (f64::from_bits(0x3fd5555555555555) * sx) * (x as f64 * x as f64);
64        return zz0 as f32;
65    }
66    let ax = t & 0x7fff_ffff;
67    if ax == 0x3fa267ddu32 {
68        return f32::copysign(f32::from_bits(0x3e933802), x)
69            - f32::copysign(f32::from_bits(0x24000000), x);
70    };
71    if ax == 0x3f693531u32 {
72        return f32::copysign(f32::from_bits(0x3e70d331), x)
73            + f32::copysign(f32::from_bits(0x31800000), x);
74    };
75    if ax == 0x3f800000u32 {
76        return f32::copysign(f32::from_bits(0x3e800000), x);
77    };
78    if gt {
79        z = 1. / z;
80    }
81    let z2 = z * z;
82    let z4 = z2 * z2;
83    let z8 = z4 * z4;
84    const CN: [u64; 6] = [
85        0x3fd45f306dc9c882,
86        0x3fe733b561bc23d5,
87        0x3fe28d9805bdfbf2,
88        0x3fc8c3ba966ae287,
89        0x3f994a7f81ee634b,
90        0x3f4a6bbf6127a6df,
91    ];
92    let mut cn0 = f_fmla(z2, f64::from_bits(CN[1]), f64::from_bits(CN[0]));
93    let cn2 = f_fmla(z2, f64::from_bits(CN[3]), f64::from_bits(CN[2]));
94    let cn4 = f_fmla(z2, f64::from_bits(CN[5]), f64::from_bits(CN[4]));
95    cn0 += z4 * cn2;
96    cn0 += z8 * cn4;
97    cn0 *= z;
98
99    const CD: [u64; 7] = [
100        0x3ff0000000000000,
101        0x4004e3b3ecc2518f,
102        0x4003ef4a360ff063,
103        0x3ff0f1dc55bad551,
104        0x3fc8da0fecc018a4,
105        0x3f88fa87803776bf,
106        0x3f1dadf2ca0acb43,
107    ];
108
109    let mut cd0 = f_fmla(z2, f64::from_bits(CD[1]), f64::from_bits(CD[0]));
110    let cd2 = f_fmla(z2, f64::from_bits(CD[3]), f64::from_bits(CD[2]));
111    let mut cd4 = f_fmla(z2, f64::from_bits(CD[5]), f64::from_bits(CD[4]));
112    let cd6 = f64::from_bits(CD[6]);
113    cd0 += z4 * cd2;
114    cd4 += z4 * cd6;
115    cd0 += z8 * cd4;
116    let mut r = cn0 / cd0;
117    if gt {
118        r = f64::copysign(0.5, z) - r;
119    }
120    r as f32
121}
122
123#[cfg(test)]
124mod tests {
125    use super::*;
126    #[test]
127    fn test_atanpif() {
128        assert_eq!(f_atanpif(0.0), 0.0);
129        assert_eq!(f_atanpif(1.0), 0.25);
130        assert_eq!(f_atanpif(1.5), 0.31283295);
131        assert_eq!(f_atanpif(-1.0), -0.25);
132        assert_eq!(f_atanpif(-1.5), -0.31283295);
133    }
134}