use crate::acospi::PI_OVER_TWO_F128;
use crate::asin::asin_eval;
use crate::asin_eval_dyadic::asin_eval_dyadic;
use crate::common::f_fmla;
use crate::double_double::DoubleDouble;
use crate::dyadic_float::{DyadicFloat128, DyadicSign};
use crate::rounding::CpuRound;
pub fn f_acos(x: f64) -> f64 {
let x_e = (x.to_bits() >> 52) & 0x7ff;
const E_BIAS: u64 = (1u64 << (11 - 1u64)) - 1u64;
const PI_OVER_TWO: DoubleDouble = DoubleDouble::new(
f64::from_bits(0x3c91a62633145c07),
f64::from_bits(0x3ff921fb54442d18),
);
let x_abs = f64::from_bits(x.to_bits() & 0x7fff_ffff_ffff_ffff);
if x_e < E_BIAS - 1 {
if x_e < E_BIAS - 55 {
return (x_abs + f64::from_bits(0x35f0000000000000)) + PI_OVER_TWO.hi;
}
let x_sq = DoubleDouble::from_exact_mult(x, x);
let err = x_abs * f64::from_bits(0x3cc0000000000000);
let (p, err) = asin_eval(x_sq, err);
let r0 = DoubleDouble::from_exact_mult(x, p.hi);
let r_hi = f_fmla(-x, p.hi, PI_OVER_TWO.hi);
let mut r_lo = ((PI_OVER_TWO.hi - r_hi) - r0.hi) - r0.lo;
r_lo = f_fmla(-x, p.lo, r_lo + PI_OVER_TWO.lo);
let r_upper = r_hi + (r_lo + err);
let r_lower = r_hi + (r_lo - err);
if r_upper == r_lower {
return r_upper;
}
return acos_less_0p5_hard(x, x_sq);
}
let x_sign = if x.is_sign_negative() { -1.0 } else { 1.0 };
const PI: DoubleDouble = DoubleDouble::new(
f64::from_bits(0x3ca1a62633145c07),
f64::from_bits(0x400921fb54442d18),
);
if x_e >= E_BIAS {
if x_abs == 1.0 {
return if x == 1.0 {
0.0
} else {
f_fmla(-x_sign, PI.hi, PI.lo)
};
}
return f64::NAN;
}
let u = f_fmla(x_abs, -0.5, 0.5);
let v_hi = u.sqrt();
let h;
#[cfg(any(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "fma"
),
target_arch = "aarch64"
))]
{
h = f_fmla(v_hi, -v_hi, u);
}
#[cfg(not(any(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "fma"
),
target_arch = "aarch64"
)))]
{
let v_hi_sq = DoubleDouble::from_exact_mult(v_hi, v_hi);
h = (u - v_hi_sq.hi) - v_hi_sq.lo;
}
let vh = v_hi * 2.0;
let vl = h / v_hi;
let err = vh * f64::from_bits(0x3cc0000000000000);
let (p, err) = asin_eval(DoubleDouble::new(0.0, u), err);
let r0 = DoubleDouble::quick_mult(DoubleDouble::new(vl, vh), p);
let r_hi;
let r_lo;
if x.is_sign_positive() {
r_hi = r0.hi;
r_lo = r0.lo;
} else {
let r = DoubleDouble::from_exact_add(PI.hi, -r0.hi);
r_hi = r.hi;
r_lo = (PI.lo - r0.lo) + r.lo;
}
let r_upper = r_hi + (r_lo + err);
let r_lower = r_hi + (r_lo - err);
if r_upper == r_lower {
return r_upper;
}
acos_hard(x, u, v_hi, h, vh, vl)
}
#[cold]
#[inline(never)]
fn acos_hard(x: f64, u: f64, v_hi: f64, h: f64, vh: f64, vl: f64) -> f64 {
let idx = (u * f64::from_bits(0x4050000000000000)).cpu_round() as usize;
let vl_lo;
#[cfg(any(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "fma"
),
target_arch = "aarch64"
))]
{
vl_lo = f_fmla(-v_hi, vl, h) / v_hi;
}
#[cfg(not(any(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "fma"
),
target_arch = "aarch64"
)))]
{
let vh_vl = DoubleDouble::from_exact_mult(v_hi, vl);
vl_lo = ((h - vh_vl.hi) - vh_vl.lo) / v_hi;
}
let t = h * (-0.25) / u;
let vll = f_fmla(vl, t, vl_lo);
let m_v_p = DyadicFloat128::new_from_f64(vl) + DyadicFloat128::new_from_f64(vll);
let mut m_v = DyadicFloat128::new_from_f64(vh) + m_v_p;
m_v.sign = if x.is_sign_negative() {
DyadicSign::Neg
} else {
DyadicSign::Pos
};
let y_f128 =
DyadicFloat128::new_from_f64(f_fmla(idx as f64, f64::from_bits(0xbf90000000000000), u));
let p_f128 = asin_eval_dyadic(y_f128, idx);
let mut r_f128 = m_v * p_f128;
if x.is_sign_negative() {
const PI_F128: DyadicFloat128 = DyadicFloat128 {
sign: DyadicSign::Pos,
exponent: -126,
mantissa: 0xc90fdaa2_2168c234_c4c6628b_80dc1cd1_u128,
};
r_f128 = PI_F128 + r_f128;
}
r_f128.fast_as_f64()
}
#[cold]
#[inline(never)]
fn acos_less_0p5_hard(x: f64, x_sq: DoubleDouble) -> f64 {
let idx = (x_sq.hi * f64::from_bits(0x4050000000000000)).cpu_round() as usize;
let mut x_f128 = DyadicFloat128::new_from_f64(x);
let u: DyadicFloat128;
#[cfg(any(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "fma"
),
target_arch = "aarch64"
))]
{
let u_hi = DyadicFloat128::new_from_f64(f_fmla(
idx as f64,
f64::from_bits(0xbf90000000000000),
x_sq.hi,
));
u = u_hi.quick_add(&DyadicFloat128::new_from_f64(x_sq.lo));
}
#[cfg(not(any(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "fma"
),
target_arch = "aarch64"
)))]
{
let x_sq_f128 = x_f128.quick_mul(&x_f128);
u = x_sq_f128.quick_add(&DyadicFloat128::new_from_f64(
idx as f64 * f64::from_bits(0xbf90000000000000),
));
}
let p_f128 = asin_eval_dyadic(u, idx);
x_f128.sign = x_f128.sign.negate();
let r = PI_OVER_TWO_F128.quick_add(&x_f128.quick_mul(&p_f128));
r.fast_as_f64()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn f_acos_test() {
assert_eq!(f_acos(0.7), 0.7953988301841436);
assert_eq!(f_acos(-0.1), 1.6709637479564565);
assert_eq!(f_acos(-0.4), 1.9823131728623846);
}
}