use super::abs::abs;
use core::f32::consts::PI;
pub(super) fn atan2_approx(y: f32, x: f32) -> f32 {
let n = atan2_norm_approx(y, x);
PI / 2.0 * if n > 2.0 { n - 4.0 } else { n }
}
pub(super) fn atan2_norm_approx(y: f32, x: f32) -> f32 {
const SIGN_MASK: u32 = 0x8000_0000;
const B: f32 = 0.596_227;
let ux_s = SIGN_MASK & x.to_bits();
let uy_s = SIGN_MASK & y.to_bits();
let q = ((!ux_s & uy_s) >> 29 | ux_s >> 30) as f32;
let bxy_a = abs(B * x * y);
let n = bxy_a + y * y;
let atan_1q = n / (x * x + bxy_a + n);
let uatan_2q = (ux_s ^ uy_s) | atan_1q.to_bits();
q + f32::from_bits(uatan_2q)
}
#[cfg(test)]
mod tests {
use super::atan2_approx;
use core::f32::consts::PI;
const MAX_ERROR: f32 = 0.003;
#[test]
fn sanity_check() {
let test_vectors: &[(f32, f32, f32)] = &[
(0.0, 1.0, 0.0),
(0.0, -1.0, PI),
(3.0, 2.0, (3.0f32 / 2.0).atan()),
(2.0, -1.0, (2.0f32 / -1.0).atan() + PI),
(-2.0, -1.0, (-2.0f32 / -1.0).atan() - PI),
];
for (y, x, expected) in test_vectors {
let actual = atan2_approx(*y, *x);
let delta = actual - expected;
assert!(
delta <= MAX_ERROR,
"delta {} too large: {} vs {}",
delta,
actual,
expected
);
}
}
}