use crate::dd::DD;
use crate::dv::DV;
use crate::od::OD;
use crate::ov::OV;
use crate::qd::QD;
use crate::qv::QV;
use crate::simd::Simd;
pub mod mutate {
pub const LAST_ULP: u32 = 1;
pub const DD_GREEDY: u32 = 2;
pub const N: u32 = 2;
pub const NAMES: [&str; 3] = [
"none",
"reciprocals: the last part one ulp up",
"reciprocals: a double-double's parts without the two-sum",
];
#[cfg(feature = "mutate")]
thread_local!(static ID: std::cell::Cell<u32> = const { std::cell::Cell::new(0) });
#[cfg(feature = "mutate")]
pub fn set(id: u32) {
ID.with(|m| m.set(id));
}
#[cfg(feature = "mutate")]
#[inline(always)]
pub fn on(id: u32) -> bool {
ID.with(|m| m.get() == id)
}
#[cfg(not(feature = "mutate"))]
#[inline(always)]
pub fn on(_id: u32) -> bool {
false
}
}
fn pow2(e: i64) -> f64 {
assert!((-1022..=1023).contains(&e), "recip: 2^{e} is not a normal f64");
f64::from_bits(((e + 1023) as u64) << 52)
}
pub fn ratio_parts(p: i64, q: u64) -> [f64; 8] {
assert!((1..1u64 << 60).contains(&q), "ratio_parts: q = {q}, not in [1, 2^60)");
let d = q as u128;
let qb = 128 - d.leading_zeros() as i64;
let mut out = [0.0; 8];
let (mut n, mut neg, mut e) = (p.unsigned_abs() as u128, p < 0, 0i64);
for r in out.iter_mut() {
if n == 0 {
break;
}
let split = |s: i64| if s >= 0 { (n << s, d) } else { (n, d << -s) };
let mut s = 52 - ((128 - n.leading_zeros() as i64) - qb);
let (mut a, mut dv) = split(s);
if a / dv < 1 << 52 {
s += 1;
(a, dv) = split(s);
}
let q0 = a / dv;
let rem = a - q0 * dv;
let qr = q0 + (2 * rem > dv || (2 * rem == dv && q0 & 1 == 1)) as u128;
let m = qr as f64 * pow2(-(e + s));
*r = if neg { -m } else { m };
let rr = a as i128 - (qr * dv) as i128;
neg ^= rr < 0;
n = rr.unsigned_abs();
e += s.max(0);
}
out
}
pub fn recip_parts(k: u64) -> [f64; 8] {
ratio_parts(1, k)
}
#[inline]
fn prefix<const P: usize>(r: [f64; 8]) -> [f64; P] {
let mut x: [f64; P] = std::array::from_fn(|i| r[i]);
if mutate::on(mutate::LAST_ULP) {
x[P - 1] = x[P - 1].next_up();
}
x
}
#[inline]
fn dd_of(r: [f64; 8]) -> DD {
let [hi, lo] = prefix::<2>(r);
if mutate::on(mutate::DD_GREEDY) {
DD { hi, lo }
} else {
DD::new(hi, lo)
}
}
impl DD {
pub fn recip_int(k: u64) -> DD {
dd_of(recip_parts(k))
}
pub fn from_ratio(p: i64, q: u64) -> DD {
dd_of(ratio_parts(p, q))
}
pub fn mul_recip(self, k: u64) -> DD {
self * DD::recip_int(k)
}
}
impl QD {
pub fn recip_int(k: u64) -> QD {
QD {
x: prefix::<4>(recip_parts(k)),
}
}
pub fn from_ratio(p: i64, q: u64) -> QD {
QD {
x: prefix::<4>(ratio_parts(p, q)),
}
}
pub fn mul_recip(self, k: u64) -> QD {
self * QD::recip_int(k)
}
}
impl OD {
pub fn recip_int(k: u64) -> OD {
OD {
x: prefix::<8>(recip_parts(k)),
}
}
pub fn from_ratio(p: i64, q: u64) -> OD {
OD {
x: prefix::<8>(ratio_parts(p, q)),
}
}
pub fn mul_recip(self, k: u64) -> OD {
self * OD::recip_int(k)
}
}
impl<S: Simd> DV<S> {
pub fn recip_int(k: u64) -> Self {
DV::splat(DD::recip_int(k))
}
pub fn mul_recip(self, k: u64) -> Self {
self * DV::recip_int(k)
}
}
impl<S: Simd> QV<S> {
pub fn recip_int(k: u64) -> Self {
QV::splat(QD::recip_int(k))
}
pub fn mul_recip(self, k: u64) -> Self {
self * QV::recip_int(k)
}
}
impl<S: Simd> OV<S> {
pub fn recip_int(k: u64) -> Self {
OV::splat(OD::recip_int(k))
}
pub fn mul_recip(self, k: u64) -> Self {
self * OV::recip_int(k)
}
}