mod common;
use common::Rng;
use multidouble::dd::DD;
use multidouble::exact::{round_to_f64, BigU, Dy};
use multidouble::native::{OV, QV};
use multidouble::od::OD;
use multidouble::qd::QD;
use multidouble::recip::{ratio_parts, recip_parts};
fn numerator(p: i64, q: u64, parts: &[f64]) -> Dy {
let mut num = Dy {
neg: p < 0,
m: BigU::from_u64(p.unsigned_abs()),
e: 0,
};
let qd = Dy {
neg: false,
m: BigU::from_u64(q),
e: 0,
};
for &r in parts {
num = num.sub(&qd.mul(&Dy::from_f64(r)));
}
num
}
fn rn_div(n: &Dy, q: u64) -> f64 {
if n.is_zero() {
return 0.0;
}
let sh = 264u64;
let (quot, rem) = n.m.shl(sh).divrem_small(q);
let v = round_to_f64(", n.e - sh as i64, rem != 0);
if n.neg {
-v
} else {
v
}
}
fn reference(p: i64, q: u64) -> [f64; 8] {
let mut out = [0.0; 8];
for i in 0..8 {
let n = numerator(p, q, &out[..i]);
out[i] = rn_div(&n, q);
if out[i] == 0.0 {
break;
}
}
out
}
fn bits(x: &[f64]) -> Vec<u64> {
x.iter().map(|v| v.to_bits()).collect()
}
fn two_sum(a: f64, b: f64) -> [f64; 2] {
let s = a + b;
let bb = s - a;
[s, (a - (s - bb)) + (b - bb)]
}
const TIES: [u64; 3] = [191516586041457763, 176007846257689259, 383033172082915526];
fn cases() -> Vec<(i64, u64)> {
let mut r = Rng(1099);
let mut v: Vec<(i64, u64)> = (1..=5000u64).map(|k| (1, k)).collect();
for _ in 0..4000 {
let b = 1 + r.next_u64() % 60;
v.push((1, (r.next_u64() >> (64 - b)).max(1)));
}
for _ in 0..4000 {
let pb = 1 + r.next_u64() % 63;
let qb = 1 + r.next_u64() % 60;
let p = (r.next_u64() >> (64 - pb)) as i64 * if r.next_u64() & 1 == 0 { 1 } else { -1 };
v.push((p, (r.next_u64() >> (64 - qb)).max(1)));
}
v.extend([
(1, (1u64 << 60) - 1),
(i64::MAX, 3),
(i64::MIN + 1, (1 << 59) + 1),
(i64::MIN, 1),
(i64::MIN, 3),
(0, 7),
(6, 3),
(1, 1 << 40),
]);
v.extend(TIES.map(|k| (1, k)));
v
}
fn mismatches() -> usize {
let mut bad = 0;
for (p, q) in cases() {
let want = reference(p, q);
let got = if p == 1 { recip_parts(q) } else { ratio_parts(p, q) };
let dd = if p == 1 { DD::recip_int(q) } else { DD::from_ratio(p, q) };
let qd = if p == 1 { QD::recip_int(q) } else { QD::from_ratio(p, q) };
let od = if p == 1 { OD::recip_int(q) } else { OD::from_ratio(p, q) };
if bits(&got) != bits(&want)
|| bits(&[dd.hi, dd.lo]) != bits(&two_sum(want[0], want[1]))
|| bits(&qd.x) != bits(&want[..4])
|| bits(&od.x) != bits(&want)
{
bad += 1;
}
}
bad
}
#[test]
fn ratios_against_exact_rationals() {
let n = cases().len();
let bad = mismatches();
println!("ratio_parts and the types' prefixes against exact rationals: {bad} of {n} cases differ");
assert_eq!(bad, 0);
assert_eq!(bits(&reference(6, 3)), bits(&[2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]));
assert_eq!(reference(1, 3)[0], 1.0 / 3.0);
}
fn dd_not_double_words() -> usize {
let exact = |a: f64, b: f64| Dy::from_f64(a).add(&Dy::from_f64(b));
let mut bad = 0;
for (p, q) in cases() {
let want = reference(p, q);
let dd = if p == 1 { DD::recip_int(q) } else { DD::from_ratio(p, q) };
if dd.hi + dd.lo != dd.hi || !exact(dd.hi, dd.lo).sub(&exact(want[0], want[1])).is_zero() {
bad += 1;
}
}
bad
}
#[test]
fn dd_constants_are_double_words() {
for k in TIES {
let want = reference(1, k);
assert_ne!(want[0] + want[1], want[0], "k = {k}: the greedy pair is no tie");
}
let bad = dd_not_double_words();
println!(
"double-double constants: {bad} of {} cases not a double word of the greedy value",
cases().len()
);
assert_eq!(bad, 0);
}
#[cfg(feature = "mutate")]
#[test]
fn dd_double_words_control() {
use multidouble::recip::mutate;
mutate::set(mutate::DD_GREEDY);
let bad = dd_not_double_words();
mutate::set(0);
println!("control, a double-double's parts without the two-sum: {bad} cases not a double word (must be the ties)");
assert_eq!(bad, TIES.len(), "the control did not fire on exactly the ties");
}
#[cfg(feature = "mutate")]
#[test]
fn ratios_control_last_ulp() {
use multidouble::recip::mutate;
mutate::set(mutate::LAST_ULP);
let bad = mismatches();
mutate::set(0);
let n = cases().len();
println!("control, the types' last part one ulp up: {bad} of {n} cases differ (must be all)");
assert_eq!(bad, n, "the one-ulp control did not fire on every case");
}
fn hp_recip_parts(k: u64) -> [f64; 8] {
fn pow2(e: i64) -> f64 {
assert!((-1022..=1023).contains(&e), "pow2: 2^{e} is not a normal f64");
f64::from_bits(((e + 1023) as u64) << 52)
}
assert!((1..1u64 << 60).contains(&k), "recip_parts: k = {k}");
let d = k as u128;
let kb = 128 - d.leading_zeros() as i64;
let mut out = [0.0; 8];
let (mut n, mut neg, mut e) = (1u128, false, 0i64);
for r in out.iter_mut() {
if n == 0 {
break;
}
let mut s = 52 - ((128 - n.leading_zeros() as i64) - kb);
let mut a = n << s;
if a / d < 1 << 52 {
s += 1;
a <<= 1;
}
let q0 = a / d;
let rem = a - q0 * d;
let q = q0 + (2 * rem > d || (2 * rem == d && q0 & 1 == 1)) as u128;
let m = q as f64 * pow2(-(e + s));
*r = if neg { -m } else { m };
let rr = a as i128 - (q * d) as i128;
neg ^= rr < 0;
n = rr.unsigned_abs();
e += s;
}
out
}
fn hp_recip_lane_qv(r: &[f64; 8]) -> QV {
let dd = |i: usize| QV::from_dv(multidouble::native::DV::splat(DD { hi: r[i], lo: r[i + 1] }));
dd(0) + dd(2)
}
fn hp_recip_lane_ov(r: &[f64; 8]) -> OV {
let dd = |i: usize| OV::from_dv(multidouble::native::DV::splat(DD { hi: r[i], lo: r[i + 1] }));
((dd(0) + dd(2)) + dd(4)) + dd(6)
}
#[test]
fn recip_matches_hp_to_2_20() {
let (mut parts, mut qv, mut ov) = (0, 0, 0);
for k in 1..=(1u64 << 20) {
let r = recip_parts(k);
if bits(&r) != bits(&hp_recip_parts(k)) {
parts += 1;
}
let lane_bits = |c: &[multidouble::native::V8]| -> Vec<u64> {
c.iter().flat_map(|v| v.to_array().map(f64::to_bits)).collect()
};
if lane_bits(&QV::recip_int(k).c) != lane_bits(&hp_recip_lane_qv(&r).c) {
qv += 1;
}
if lane_bits(&OV::recip_int(k).c) != lane_bits(&hp_recip_lane_ov(&r).c) {
ov += 1;
}
}
println!("k = 1..=2^20 against hp: parts {parts}, QV constants {qv}, OV constants {ov} differ");
assert_eq!((parts, qv, ov), (0, 0, 0));
}
#[test]
fn mul_recip_inverts_multiplication() {
let mut r = Rng(7);
let mut worst = [0.0f64; 3];
for _ in 0..3000 {
let k = 1 + r.next_u64() % (1 << 30);
let x = r.dd(20);
let kd = DD::from_f64(k as f64);
let e = [
((x * kd).mul_recip(k) - x).hi.abs() / x.hi.abs(),
((QD::from_dd(x) * QD::from_f64(k as f64)).mul_recip(k) - QD::from_dd(x)).x[0].abs() / x.hi.abs(),
((OD::from_dd(x) * OD::from_f64(k as f64)).mul_recip(k) - OD::from_dd(x)).x[0].abs() / x.hi.abs(),
];
for i in 0..3 {
assert!(e[i].is_finite(), "type {i}: k {k}, error {}", e[i]);
worst[i] = worst[i].max(e[i]);
}
}
let u = [2f64.powi(-106), 2f64.powi(-212), 2f64.powi(-424)];
let bar = [8.0, 64.0, 1024.0];
println!(
"(x k)/k - x relative: DD {:.2e} ({:.2} u^2), QD {:.2e}, OD {:.2e}",
worst[0],
worst[0] / u[0],
worst[1],
worst[2]
);
for i in 0..3 {
assert!(worst[i] <= bar[i] * u[i], "type {i}: {:.2e}", worst[i]);
}
}