use super::acos::HALF_PI_HI;
use super::double_double::{fast_two_sum, horner_dd, mul_dd, round_ties_even, two_prod};
use super::path::Path;
use super::tables::{ACOS_C, ACOS_CT, ACOS_EXCEPTIONS, ACOS_S, f};
const SIXTY_FOUR_OVER_PI: f64 = f64::from_bits(0x4034_5f30_6dc9_c883);
const MAGIC: f64 = f64::from_bits(0x3fb8_0000_0000_0000);
const PI64_H: f64 = f64::from_bits(0x3fa9_21fb_5444_2d00);
const PI64_M: f64 = f64::from_bits(0x3ca8_4698_98cc_5180);
const PI64_L: f64 = f64::from_bits(0xb99f_c8f8_cbb5_bf6c);
#[inline(never)]
#[cold]
pub fn acos_refine(x: f64, phi: f64) -> (f64, Path) {
let (s2, dx2) = two_prod(x, x);
let (c2h, c2l) = fast_two_sum(1.0, -s2);
let (c2h, c2l) = fast_two_sum(c2h, c2l - dx2);
let ch = c2h.sqrt();
let cl = (c2l - ch.mul_add(ch, -c2h)) * (0.5 / ch);
let jf = round_ties_even((phi - HALF_PI_HI).abs() * SIXTY_FOUR_OVER_PI) as usize;
let s = |j: usize, m: usize| f(&ACOS_S, 2 * j + m);
let (cos_h, cos_l) = (s(32 - jf, 1), s(32 - jf, 0));
let (sin_h, sin_l) = (s(jf, 1), s(jf, 0));
let dsh = x.abs() - sin_h;
let dsl = -sin_l;
let dch = ch - cos_h;
let dcl = cl - cos_l;
let sc = sin_h.mul_add(dch, MAGIC) - MAGIC;
let dsc = sin_h.mul_add(dch, -sc);
let cs = cos_h.mul_add(dsh, MAGIC) - MAGIC;
let dcs = cos_h.mul_add(dsh, -cs);
let v = cs - sc;
let dv = ((cos_h * dsl + cos_l * dsh) - (sin_h * dcl + sin_l * dch)) - (dsc - dcs);
let (v, dv) = fast_two_sum(v, dv);
let sgn = 1.0f64.copysign(x);
let jt = 32.0 - (jf as f64) * sgn;
let (v2, dv2) = mul_dd(v, dv, v, dv);
let v = v * -sgn;
let dv = dv * -sgn;
let ct = |k| f(&ACOS_CT, k);
let low = v2 * (ct(0) + v2 * (ct(1) + v2 * ct(2)));
let (fh, fl) = horner_dd(v2, dv2, &ACOS_C, low);
let (fh, fl) = mul_dd(v, dv, fh, fl);
let ph = jt * PI64_H;
let pl = PI64_M * jt;
let ps = PI64_L * jt;
let (pl, ps) = sum(fh, fl, pl, ps);
let (ph, pl) = fast_two_sum(ph, pl);
let (pl, ps) = fast_two_sum(pl, ps);
let (ph, pl) = fast_two_sum(ph, pl);
let (mut pl, ps) = fast_two_sum(pl, ps);
if near_rounding_boundary(pl) {
if let Some(value) = exception(x) {
return (value, Path::Exception);
}
let t = pl.to_bits();
let t = if (ps.to_bits() ^ t) >> 63 != 0 {
t.wrapping_sub(1)
} else {
t.wrapping_add(1)
};
pl = f64::from_bits(t);
}
(ph + pl, Path::Accurate)
}
#[inline(always)]
fn near_rounding_boundary(pl: f64) -> bool {
let t = pl.to_bits();
let e = ((t >> 52) & 0x7ff) as i64 - 1023;
let e = (52 - (107 + e)).clamp(0, 52);
let m = (1u64 << 52) - (1u64 << e);
let e = if e == 0 { 64 } else { e };
t.wrapping_add(1u64 << (e - 1)) & m == 0
}
#[inline(always)]
fn exception(x: f64) -> Option<f64> {
ACOS_EXCEPTIONS
.as_chunks::<3>()
.0
.iter()
.find(|entry| f64::from_bits(entry[0]) == x)
.map(|entry| f64::from_bits(entry[1]) + f64::from_bits(entry[2]))
}
#[inline(always)]
fn two_sum(a: f64, b: f64) -> (f64, f64) {
let s = a + b;
let a_prime = s - b;
let b_prime = s - a_prime;
(s, (a - a_prime) + (b - b_prime))
}
#[inline(always)]
fn sum(xh: f64, xl: f64, ch: f64, cl: f64) -> (f64, f64) {
let (sh, sl) = two_sum(xh, ch);
(sh, (xl + cl) + sl)
}
#[cfg(test)]
mod tests {
use super::super::acos::acos_traced;
use super::*;
#[test]
fn test_listed_exceptions_take_the_exception_path() {
let mut paths = Vec::new();
for entry in ACOS_EXCEPTIONS.as_chunks::<3>().0 {
let x = f64::from_bits(entry[0]);
let (fast, path) = acos_traced(x);
paths.push(path);
let listed = f64::from_bits(entry[1]) + f64::from_bits(entry[2]);
assert_eq!(fast.to_bits(), listed.to_bits(), "{x:e}");
assert_eq!(acos_refine(x, fast), (fast, Path::Exception), "{x:e}");
}
use Path::{Exception, Fast};
assert_eq!(paths, [Fast, Exception, Fast, Fast, Fast]);
}
#[test]
fn test_refine_agrees_with_certified_fast_results() {
let mut x = 1.0e-6;
while x < 1.0 {
for v in [x, -x] {
let (fast, path) = acos_traced(v);
if path == Path::Fast {
assert_eq!(acos_refine(v, fast).0.to_bits(), fast.to_bits(), "{v:e}");
}
}
x *= 1.0137;
}
}
#[test]
fn test_two_sum_is_exact() {
for (a, b) in [(1.0, 1.0e-20), (1.0e-20, -1.0), (0.1, 0.2), (-3.5, 3.5)] {
let (s, e) = two_sum(a, b);
let (s2, e2) = super::super::double_double::two_sum(a, b);
assert_eq!((s, e), (s2, e2), "{a} {b}");
}
}
#[test]
fn test_rounding_boundary_test_reads_the_low_bits() {
assert!(near_rounding_boundary(f64::from_bits(
0x3c30_0000_0000_0000
)));
assert!(!near_rounding_boundary(f64::from_bits(
0x3c30_0000_0000_0123
)));
assert!(near_rounding_boundary(0.0));
}
}