pxfm/tangent/
atan2pif.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
31static OFF: [f32; 8] = [0.0, 0.5, 1.0, 0.5, -0.0, -0.5, -1.0, -0.5];
32static SGNF: [f32; 2] = [1., -1.];
33static SGN: [f64; 2] = [1., -1.];
34
35/// Computes atan(x/y) / PI
36///
37/// Max found ULP 0.5
38#[inline]
39pub fn f_atan2pif(y: f32, x: f32) -> f32 {
40    let tx = x.to_bits();
41    let ty: u32 = y.to_bits();
42    let ux: u32 = tx;
43    let uy: u32 = ty;
44    let ax: u32 = ux & 0x7fff_ffff;
45    let ay = uy & 0x7fff_ffff;
46    if ay >= (0xff << 23) || ax >= (0xff << 23) {
47        if ay > (0xff << 23) {
48            return x + y;
49        } // nan
50        if ax > (0xff << 23) {
51            return x + y;
52        } // nan
53        let yinf = ay == (0xff << 23);
54        let xinf = ax == (0xff << 23);
55        if yinf & xinf {
56            return if (ux >> 31) != 0 {
57                0.75 * SGNF[(uy >> 31) as usize]
58            } else {
59                0.25 * SGNF[(uy >> 31) as usize]
60            };
61        }
62        if xinf {
63            return if (ux >> 31) != 0 {
64                SGNF[(uy >> 31) as usize]
65            } else {
66                0.0 * SGNF[(uy >> 31) as usize]
67            };
68        }
69        if yinf {
70            return 0.5 * SGNF[(uy >> 31) as usize];
71        }
72    }
73    if ay == 0 {
74        if (ay | ax) == 0 {
75            let i: u32 = (uy >> 31) * 4 + (ux >> 31) * 2;
76            return OFF[i as usize];
77        }
78        if (ux >> 31) == 0 {
79            return 0.0 * SGNF[(uy >> 31) as usize];
80        }
81    }
82    if ax == ay {
83        static S: [f32; 4] = [0.25, 0.75, -0.25, -0.75];
84        let i = (uy >> 31) * 2 + (ux >> 31);
85        return S[i as usize];
86    }
87    let gt: usize = (ay > ax) as usize;
88    let i: u32 = (uy >> 31) * 4 + (ux >> 31) * 2 + gt as u32;
89
90    let zx = x as f64;
91    let zy = y as f64;
92    static M: [f64; 2] = [0., 1.];
93
94    let mut z = f_fmla(M[gt], zx, M[1 - gt] * zy) / f_fmla(M[gt], zy, M[1 - gt] * zx);
95
96    const CN: [u64; 7] = [
97        0x3fd45f306dc9c883,
98        0x3fe988d83a142ada,
99        0x3fe747bebf492057,
100        0x3fd2cc5645094ff3,
101        0x3faa0521c711ab66,
102        0x3f6881b8058b9a0d,
103        0x3efb16ff514a0af0,
104    ];
105
106    let mut r = f64::from_bits(CN[0]);
107    let z2 = z * z;
108    z *= SGN[gt];
109    // avoid spurious underflow in the polynomial evaluation excluding tiny arguments
110    if z2 > f64::from_bits(0x3c90000000000000) {
111        let z4 = z2 * z2;
112        let z8 = z4 * z4;
113        let mut cn0 = f_fmla(z2, f64::from_bits(CN[1]), r);
114        let cn2 = f_fmla(z2, f64::from_bits(CN[3]), f64::from_bits(CN[2]));
115        let mut cn4 = f_fmla(z2, f64::from_bits(CN[5]), f64::from_bits(CN[4]));
116        let cn6 = f64::from_bits(CN[6]);
117        cn0 += z4 * cn2;
118        cn4 += z4 * cn6;
119        cn0 += z8 * cn4;
120
121        const CD: [u64; 7] = [
122            0x3ff0000000000000,
123            0x4006b8b143a3f6da,
124            0x4008421201d18ed5,
125            0x3ff8221d086914eb,
126            0x3fd670657e3a07ba,
127            0x3fa0f4951fd1e72d,
128            0x3f4b3874b8798286,
129        ];
130
131        let mut cd0 = f_fmla(z2, f64::from_bits(CD[1]), f64::from_bits(CD[0]));
132        let cd2 = f_fmla(z2, f64::from_bits(CD[3]), f64::from_bits(CD[2]));
133        let mut cd4 = f_fmla(z2, f64::from_bits(CD[5]), f64::from_bits(CD[4]));
134        let cd6 = f64::from_bits(CD[6]);
135        cd0 += z4 * cd2;
136        cd4 += z4 * cd6;
137        cd0 += z8 * cd4;
138
139        r = cn0 / cd0;
140    }
141    f_fmla(z, r, OFF[i as usize] as f64) as f32
142}
143
144#[cfg(test)]
145mod tests {
146    use super::*;
147    #[test]
148    fn test_atan2pif() {
149        assert_eq!(f_atan2pif(0.32131, 0.987565), 0.10012555);
150        assert_eq!(f_atan2pif(532.32131, 12.987565), 0.49223542);
151        assert_eq!(f_atan2pif(-754.32131, 12.987565), -0.494520042);
152    }
153}