/rust/registry/src/index.crates.io-1949cf8c6b5b557f/pxfm-0.1.25/src/tangent/atan2pif.rs
Line | Count | Source |
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 | | */ |
29 | | use crate::common::f_fmla; |
30 | | |
31 | | static OFF: [f32; 8] = [0.0, 0.5, 1.0, 0.5, -0.0, -0.5, -1.0, -0.5]; |
32 | | static SGNF: [f32; 2] = [1., -1.]; |
33 | | static SGN: [f64; 2] = [1., -1.]; |
34 | | |
35 | | /// Computes atan(x/y) / PI |
36 | | /// |
37 | | /// Max found ULP 0.5 |
38 | | #[inline] |
39 | 0 | pub fn f_atan2pif(y: f32, x: f32) -> f32 { |
40 | 0 | let tx = x.to_bits(); |
41 | 0 | let ty: u32 = y.to_bits(); |
42 | 0 | let ux: u32 = tx; |
43 | 0 | let uy: u32 = ty; |
44 | 0 | let ax: u32 = ux & 0x7fff_ffff; |
45 | 0 | let ay = uy & 0x7fff_ffff; |
46 | 0 | if ay >= (0xff << 23) || ax >= (0xff << 23) { |
47 | 0 | if ay > (0xff << 23) { |
48 | 0 | return x + y; |
49 | 0 | } // nan |
50 | 0 | if ax > (0xff << 23) { |
51 | 0 | return x + y; |
52 | 0 | } // nan |
53 | 0 | let yinf = ay == (0xff << 23); |
54 | 0 | let xinf = ax == (0xff << 23); |
55 | 0 | if yinf & xinf { |
56 | 0 | return if (ux >> 31) != 0 { |
57 | 0 | 0.75 * SGNF[(uy >> 31) as usize] |
58 | | } else { |
59 | 0 | 0.25 * SGNF[(uy >> 31) as usize] |
60 | | }; |
61 | 0 | } |
62 | 0 | if xinf { |
63 | 0 | return if (ux >> 31) != 0 { |
64 | 0 | SGNF[(uy >> 31) as usize] |
65 | | } else { |
66 | 0 | 0.0 * SGNF[(uy >> 31) as usize] |
67 | | }; |
68 | 0 | } |
69 | 0 | if yinf { |
70 | 0 | return 0.5 * SGNF[(uy >> 31) as usize]; |
71 | 0 | } |
72 | 0 | } |
73 | 0 | if ay == 0 { |
74 | 0 | if (ay | ax) == 0 { |
75 | 0 | let i: u32 = (uy >> 31) * 4 + (ux >> 31) * 2; |
76 | 0 | return OFF[i as usize]; |
77 | 0 | } |
78 | 0 | if (ux >> 31) == 0 { |
79 | 0 | return 0.0 * SGNF[(uy >> 31) as usize]; |
80 | 0 | } |
81 | 0 | } |
82 | 0 | if ax == ay { |
83 | | static S: [f32; 4] = [0.25, 0.75, -0.25, -0.75]; |
84 | 0 | let i = (uy >> 31) * 2 + (ux >> 31); |
85 | 0 | return S[i as usize]; |
86 | 0 | } |
87 | 0 | let gt: usize = (ay > ax) as usize; |
88 | 0 | let i: u32 = (uy >> 31) * 4 + (ux >> 31) * 2 + gt as u32; |
89 | | |
90 | 0 | let zx = x as f64; |
91 | 0 | let zy = y as f64; |
92 | | static M: [f64; 2] = [0., 1.]; |
93 | | |
94 | 0 | 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 | 0 | let mut r = f64::from_bits(CN[0]); |
107 | 0 | let z2 = z * z; |
108 | 0 | z *= SGN[gt]; |
109 | | // avoid spurious underflow in the polynomial evaluation excluding tiny arguments |
110 | 0 | if z2 > f64::from_bits(0x3c90000000000000) { |
111 | 0 | let z4 = z2 * z2; |
112 | 0 | let z8 = z4 * z4; |
113 | 0 | let mut cn0 = f_fmla(z2, f64::from_bits(CN[1]), r); |
114 | 0 | let cn2 = f_fmla(z2, f64::from_bits(CN[3]), f64::from_bits(CN[2])); |
115 | 0 | let mut cn4 = f_fmla(z2, f64::from_bits(CN[5]), f64::from_bits(CN[4])); |
116 | 0 | let cn6 = f64::from_bits(CN[6]); |
117 | 0 | cn0 += z4 * cn2; |
118 | 0 | cn4 += z4 * cn6; |
119 | 0 | 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 | 0 | let mut cd0 = f_fmla(z2, f64::from_bits(CD[1]), f64::from_bits(CD[0])); |
132 | 0 | let cd2 = f_fmla(z2, f64::from_bits(CD[3]), f64::from_bits(CD[2])); |
133 | 0 | let mut cd4 = f_fmla(z2, f64::from_bits(CD[5]), f64::from_bits(CD[4])); |
134 | 0 | let cd6 = f64::from_bits(CD[6]); |
135 | 0 | cd0 += z4 * cd2; |
136 | 0 | cd4 += z4 * cd6; |
137 | 0 | cd0 += z8 * cd4; |
138 | | |
139 | 0 | r = cn0 / cd0; |
140 | 0 | } |
141 | 0 | f_fmla(z, r, OFF[i as usize] as f64) as f32 |
142 | 0 | } |
143 | | |
144 | | #[cfg(test)] |
145 | | mod 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 | | } |