#![allow(clippy::needless_range_loop)]
mod common;
use common::Rng;
use multidouble::dd::DD;
use multidouble::dual::{take_counts, Counted, Lane, COUNTED_OPS};
use multidouble::dv::DV;
use multidouble::native::{Dc, Dtn, Du, Fu, Fun, Jet, Qu, F8, QV};
use multidouble::qd::QD;
use multidouble::simd::{Native, Simd};
use multidouble::v8::V8;
fn f<T: Lane>(x: T, y: T) -> T {
let two = T::cst(DV::splat_f(2.0));
let three = T::cst(DV::splat_f(3.0));
(x * x * x + two * x * y) / (x + three)
}
fn f_dd(x: DD, y: DD) -> (DD, DD, DD) {
let two = DD::from_f64(2.0);
let three = DD::from_f64(3.0);
let num = x * x * x + two * x * y;
let den = x + three;
let fx = ((DD::from_f64(3.0) * x * x + two * y) * den - num) / (den * den);
let fy = (two * x) / den;
(num / den, fx, fy)
}
fn inputs(r: &mut Rng) -> ([DD; 8], [DD; 8]) {
let x = std::array::from_fn(|_| DD::new(1.0 + 4.0 * r.unit(), r.sym() * 1e-17));
let y = std::array::from_fn(|_| DD::new(r.sym() * 5.0, r.sym() * 1e-17));
(x, y)
}
fn dv<S: Simd>(v: &[DD; 8]) -> DV<S> {
let hi: Vec<f64> = v.iter().map(|d| d.hi).collect();
let lo: Vec<f64> = v.iter().map(|d| d.lo).collect();
DV::load(&hi, &lo)
}
fn rel(a: DD, b: DD) -> f64 {
((a - b).to_f64() / b.to_f64()).abs()
}
#[test]
fn values_match_the_scalar_formula() {
let mut r = Rng(21);
for _ in 0..500 {
let (x, y) = inputs(&mut r);
let v = f(dv::<Native>(&x), dv(&y));
for j in 0..8 {
assert!(rel(v.lane(j), f_dd(x[j], y[j]).0) < 1e-30);
}
}
}
#[test]
fn du_gives_exact_partial_derivatives() {
let mut r = Rng(22);
let mut worst: f64 = 0.0;
for _ in 0..500 {
let (x, y) = inputs(&mut r);
let one = DV::splat_f(1.0);
let gx = f(Du::from_vt(dv(&x), one), Du::from_vt(dv(&y), DV::zero()));
let gy = f(Du::from_vt(dv(&x), DV::zero()), Du::from_vt(dv(&y), one));
for j in 0..8 {
let (val, fx, fy) = f_dd(x[j], y[j]);
for (got, want) in [(gx.val().lane(j), val), (gx.tan().lane(j), fx), (gy.tan().lane(j), fy)] {
let e = rel(got, want);
worst = worst.max(e);
assert!(e < 1e-29, "lane {j}: {got:?} vs {want:?}");
}
}
}
println!("Du: worst relative error {worst:.1e}");
}
#[test]
fn dtn_gives_both_partials_in_one_pass() {
let mut r = Rng(23);
for _ in 0..500 {
let (x, y) = inputs(&mut r);
let (one, zero) = (V8::splat(1.0), V8::zero());
let xs = Dtn::<2> {
v: dv(&x),
d: [one, zero],
};
let ys = Dtn::<2> {
v: dv(&y),
d: [zero, one],
};
let g = f(xs, ys);
let (dx, dy) = (g.d[0].to_array(), g.d[1].to_array());
for j in 0..8 {
let (val, fx, fy) = f_dd(x[j], y[j]);
assert!(rel(g.v.lane(j), val) < 1e-30);
assert!(
((dx[j] - fx.to_f64()) / fx.to_f64()).abs() < 1e-13,
"lane {j}: {} vs {:?}",
dx[j],
fx
);
assert!(
((dy[j] - fy.to_f64()) / fy.to_f64()).abs() < 1e-13,
"lane {j}: {} vs {:?}",
dy[j],
fy
);
}
}
}
#[test]
fn fu_is_plain_f64_forward_mode() {
let mut r = Rng(24);
for _ in 0..500 {
let (x, y) = inputs(&mut r);
let hx = V8::from_array(std::array::from_fn(|j| x[j].hi));
let hy = V8::from_array(std::array::from_fn(|j| y[j].hi));
let g = f(
Fu {
v: hx,
d: V8::splat(1.0),
},
Fu { v: hy, d: V8::zero() },
);
let (v, d) = (g.v.to_array(), g.d.to_array());
for j in 0..8 {
let (xh, yh) = (x[j].hi, y[j].hi);
let (val, fx, _) = f_dd(DD::from_f64(xh), DD::from_f64(yh));
let scale = (xh.powi(3).abs() + (2.0 * xh * yh).abs()) / (xh + 3.0);
let dscale = (3.0 * xh * xh + (2.0 * yh).abs()) / (xh + 3.0) + scale / (xh + 3.0);
assert!(
(v[j] - val.to_f64()).abs() < 1e-14 * scale,
"lane {j}: {} vs {:?}",
v[j],
val
);
assert!(
(d[j] - fx.to_f64()).abs() < 1e-14 * dscale,
"lane {j}: {} vs {:?}",
d[j],
fx
);
}
}
}
#[test]
fn control_wrong_derivative_is_caught() {
let mut r = Rng(25);
let (x, y) = inputs(&mut r);
let one = DV::splat_f(1.0);
let gx = f(Du::from_vt(dv(&x), one), Du::from_vt(dv(&y), DV::zero()));
let caught = (0..8).any(|j| {
let (val, fx, _) = f_dd(x[j], y[j]);
let wrong = fx + DD::from_f64(2.0) * val / (x[j] + DD::from_f64(3.0));
rel(gx.tan().lane(j), wrong) > 1e-29
});
assert!(caught);
}
fn g<T: Lane>(x: T, y: T, c: DV<T::S>, s: DD) -> T {
let a = x.mulc(c) - y.muls(s);
let p = T::fin(T::acc1(T::accc(T::acc(T::acc0(), x, y), x, c), -y.addc(c)));
T::zero() + (a * x.mulf(0.5) + p) / (x.bcast(0) + T::splat(DD::from_f64(3.0)))
}
type Reference = fn(DD, DD, DD, DD, DD) -> (DD, DD, DD);
fn g_dd(x: DD, y: DD, c: DD, s: DD, x0: DD) -> (DD, DD, DD) {
let half = DD::from_f64(0.5);
let den = x0 + DD::from_f64(3.0);
let q = (c * x - s * y) * (x * half) + x * y + c * x - (y + c);
let qx = c * x - s * y * half + y + c;
let qy = x - s * x * half - DD::ONE;
(q / den, (qx * den - q) / (den * den), qy / den)
}
fn errors<T: Lane>(reference: Reference) -> [f64; 3] {
let mut r = Rng(26);
let s = DD::ONE / DD::from_f64(3.0);
let (one, zero) = (DV::splat_f(1.0), DV::zero());
let mut worst = [0.0f64; 3];
for _ in 0..200 {
let x: [DD; 8] = std::array::from_fn(|_| DD::new(2.0 + r.unit(), r.sym() * 1e-16));
let y: [DD; 8] = std::array::from_fn(|_| DD::new(1.0 + r.unit(), r.sym() * 1e-16));
let c: [DD; 8] = std::array::from_fn(|_| DD::new(1.0 + r.unit(), r.sym() * 1e-16));
let gx = g(T::from_vt(dv(&x), one), T::from_vt(dv(&y), zero), dv(&c), s);
let k = T::NT.saturating_sub(1);
let mut ys = T::from_vt(dv(&y), if T::TAN_F64 { zero } else { one });
if T::TAN_F64 {
ys.set_tan64(k, V8::splat(1.0));
}
let gy = g(T::from_vt(dv(&x), zero), ys, dv(&c), s);
let gy_tan = if T::TAN_F64 {
DV {
hi: gy.tan64(k),
lo: V8::zero(),
}
} else {
gy.tan()
};
for j in 0..8 {
let h = |d: DD| if T::VAL_F64 { DD::from_f64(d.hi) } else { d };
let (val, fx, fy) = reference(h(x[j]), h(y[j]), h(c[j]), h(s), h(x[0]));
worst[0] = worst[0].max(rel(gx.val().lane(j), val));
if T::TANGENT {
worst[1] = worst[1].max(rel(gx.tan().lane(j), fx));
worst[2] = worst[2].max(rel(gy_tan.lane(j), fy));
}
}
}
worst
}
fn within<T: Lane>(e: [f64; 3]) -> bool {
let tv = if T::VAL_F64 { 1e-13 } else { 1e-29 };
let tt = if T::TAN_F64 { 1e-13 } else { 1e-29 };
e[0] < tv && e[1] < tt && e[2] < tt
}
#[test]
fn every_lane_method_on_every_lane_type() {
fn run<T: Lane>(name: &str) {
let e = errors::<T>(g_dd);
println!(
"{name}: worst relative errors {:.1e} value, {:.1e} d/dx, {:.1e} d/dy",
e[0], e[1], e[2]
);
assert!(within::<T>(e), "{name}: {e:?}");
}
run::<DV<Native>>("DV");
run::<F8>("F8");
run::<Du>("Du");
run::<Fu>("Fu");
run::<Dtn<2>>("Dtn<2>");
run::<Fun<2>>("Fun<2>");
run::<Qu>("Qu");
run::<Dc>("Dc");
run::<Jet<2>>("Jet<2>");
run::<Jet<5>>("Jet<5>");
}
#[test]
fn controls_on_bcast_are_caught() {
fn own_x(x: DD, y: DD, c: DD, s: DD, _x0: DD) -> (DD, DD, DD) {
g_dd(x, y, c, s, x)
}
assert!(!within::<DV<Native>>(errors::<DV<Native>>(own_x)));
assert!(!within::<F8>(errors::<F8>(own_x)));
fn no_bcast_tangent(x: DD, y: DD, c: DD, s: DD, x0: DD) -> (DD, DD, DD) {
let (val, _, fy) = g_dd(x, y, c, s, x0);
let qx = c * x - s * y * DD::from_f64(0.5) + y + c;
(val, qx / (x0 + DD::from_f64(3.0)), fy)
}
assert!(!within::<Du>(errors::<Du>(no_bcast_tangent)));
assert!(!within::<Fu>(errors::<Fu>(no_bcast_tangent)));
assert!(!within::<Dtn<2>>(errors::<Dtn<2>>(no_bcast_tangent)));
assert!(!within::<Fun<2>>(errors::<Fun<2>>(no_bcast_tangent)));
assert!(!within::<Qu>(errors::<Qu>(no_bcast_tangent)));
assert!(!within::<Jet<2>>(errors::<Jet<2>>(no_bcast_tangent)));
}
fn parts<T: Lane>(x: T) -> Vec<u64> {
let mut out = Vec::new();
let mut push = |v: V8<T::S>| {
let mut a = [0.0; 8];
v.store(&mut a);
out.extend(a.iter().map(|f| f.to_bits()));
};
let v = x.val();
push(v.hi);
push(v.lo);
if T::TAN_F64 {
for k in 0..T::NT {
push(x.tan64(k));
}
} else if T::TANGENT {
let t = x.tan();
push(t.hi);
push(t.lo);
}
out
}
fn random_lane<T: Lane>(r: &mut Rng) -> T {
nudged_lane(r, false)
}
fn nudged_lane<T: Lane>(r: &mut Rng, nudge: bool) -> T {
let mut v: [DD; 8] = std::array::from_fn(|_| r.dd(8));
if nudge {
for d in &mut v {
d.hi = f64::from_bits(d.hi.to_bits() ^ 1);
}
}
let t: [DD; 8] = std::array::from_fn(|_| r.dd(8));
let mut x = T::from_vt(dv(&v), dv(&t));
if T::TAN_F64 {
for k in 0..T::NT {
let a: [f64; 8] = std::array::from_fn(|_| r.dd(8).hi);
x.set_tan64(k, V8::load(&a));
}
}
x
}
fn scales_exactly<T: Lane>(scale: impl Fn(T, f64) -> T) -> bool {
let mut r = Rng(53);
for _ in 0..2000 {
let x = random_lane::<T>(&mut r);
let s = 2f64.powi((r.next_u64() % 401) as i32 - 200);
let y = scale(x, s);
if parts(y) != parts(x.mulf(s)) || parts(scale(y, 1.0 / s)) != parts(x) {
return false;
}
}
true
}
#[test]
fn mul_pwr2_is_exact_on_every_lane_type() {
assert!(scales_exactly::<DV<Native>>(|x, s| x.mul_pwr2(s)), "DV");
assert!(scales_exactly::<F8>(|x, s| x.mul_pwr2(s)), "F8");
assert!(scales_exactly::<Du>(|x, s| x.mul_pwr2(s)), "Du");
assert!(scales_exactly::<Fu>(|x, s| x.mul_pwr2(s)), "Fu");
assert!(scales_exactly::<Dtn<2>>(|x, s| x.mul_pwr2(s)), "Dtn<2>");
assert!(scales_exactly::<Fun<2>>(|x, s| x.mul_pwr2(s)), "Fun<2>");
assert!(scales_exactly::<Qu>(|x, s| x.mul_pwr2(s)), "Qu");
assert!(scales_exactly::<Dc>(|x, s| x.mul_pwr2(s)), "Dc");
assert!(scales_exactly::<Jet<3>>(|x, s| x.mul_pwr2(s)), "Jet<3>");
let mut r = Rng(54);
for _ in 0..2000 {
let x = r.dd(30);
let s = 2f64.powi((r.next_u64() % 401) as i32 - 200);
let y = x.mul_pwr2(s);
assert_eq!((y.hi.to_bits(), y.lo.to_bits()), {
let m = x.mul_f(s);
(m.hi.to_bits(), m.lo.to_bits())
});
assert_eq!(y.mul_pwr2(1.0 / s), x);
}
}
#[test]
fn controls_on_mul_pwr2_are_caught() {
fn wrong_power<T: Lane>(x: T, s: f64) -> T {
x.mul_pwr2(2.0 * s)
}
assert!(!scales_exactly::<DV<Native>>(wrong_power));
assert!(!scales_exactly::<F8>(wrong_power));
assert!(!scales_exactly::<Du>(wrong_power));
assert!(!scales_exactly::<Fu>(wrong_power));
assert!(!scales_exactly::<Dtn<2>>(wrong_power));
assert!(!scales_exactly::<Fun<2>>(wrong_power));
assert!(!scales_exactly::<Qu>(wrong_power));
assert!(!scales_exactly::<Dc>(wrong_power));
assert!(!scales_exactly::<Jet<3>>(wrong_power));
fn lo_unscaled<T: Lane>(x: T, s: f64) -> T {
let y = x.mul_pwr2(s);
let v = DV {
hi: y.val().hi,
lo: x.val().lo,
};
let mut z = T::from_vt(v, y.tan());
if T::TAN_F64 {
for k in 0..T::NT {
z.set_tan64(k, y.tan64(k));
}
}
z
}
assert!(!scales_exactly::<DV<Native>>(lo_unscaled));
assert!(!scales_exactly::<Du>(lo_unscaled));
assert!(!scales_exactly::<Dtn<2>>(lo_unscaled));
assert!(!scales_exactly::<Qu>(lo_unscaled));
assert!(!scales_exactly::<Dc>(lo_unscaled));
assert!(!scales_exactly::<Jet<3>>(lo_unscaled));
fn tangent_unscaled<T: Lane>(x: T, s: f64) -> T {
let mut y = x.mul_pwr2(s);
if T::TAN_F64 {
y.set_tan64(0, x.tan64(0));
} else {
y = T::from_vt(y.val(), x.tan());
}
y
}
assert!(!scales_exactly::<Du>(tangent_unscaled));
assert!(!scales_exactly::<Fu>(tangent_unscaled));
assert!(!scales_exactly::<Dtn<2>>(tangent_unscaled));
assert!(!scales_exactly::<Fun<2>>(tangent_unscaled));
assert!(!scales_exactly::<Qu>(tangent_unscaled));
assert!(!scales_exactly::<Dc>(tangent_unscaled));
assert!(!scales_exactly::<Jet<3>>(tangent_unscaled));
let mut r = Rng(57);
for _ in 0..200 {
let x: Dc = random_lane(&mut r);
let x = Dc {
i: dv(&std::array::from_fn(|_| r.dd(8))),
..x
};
let s = 2f64.powi((r.next_u64() % 401) as i32 - 200);
let y = x.mul_pwr2(s);
let wrong = Dc { i: x.i, ..y };
assert!(bits(y.i) == bits(x.i.mulf(s)) && (s == 1.0 || bits(wrong.i) != bits(y.i)));
}
}
fn bits(v: DV<Native>) -> [u64; 16] {
let (h, l) = (v.hi.to_array(), v.lo.to_array());
std::array::from_fn(|k| if k < 8 { h[k].to_bits() } else { l[k - 8].to_bits() })
}
#[test]
fn dc_is_du_on_the_real_and_the_imaginary_part() {
let mut r = Rng(58);
let s = DD::ONE / DD::from_f64(3.0);
let (one, zero) = (DV::splat_f(1.0), DV::zero());
for _ in 0..200 {
let x: [DD; 8] = std::array::from_fn(|_| DD::new(2.0 + r.unit(), r.sym() * 1e-16));
let y: [DD; 8] = std::array::from_fn(|_| DD::new(1.0 + r.unit(), r.sym() * 1e-16));
let c: [DD; 8] = std::array::from_fn(|_| DD::new(1.0 + r.unit(), r.sym() * 1e-16));
let gx = g(Du::from_vt(dv(&x), one), Du::from_vt(dv(&y), zero), dv(&c), s);
let gy = g(Du::from_vt(dv(&x), zero), Du::from_vt(dv(&y), one), dv(&c), s);
let real = g(Dc::from_vt(dv(&x), one), Dc::from_vt(dv(&y), zero), dv(&c), s);
assert!(bits(real.v) == bits(gx.v) && bits(real.r) == bits(gx.d) && bits(real.i) == bits(zero));
let both = g(
Dc {
v: dv(&x),
r: zero,
i: one,
},
Dc {
v: dv(&y),
r: one,
i: zero,
},
dv(&c),
s,
);
assert!(bits(both.v) == bits(gx.v) && bits(both.i) == bits(gx.d) && bits(both.r) == bits(gy.d));
assert!(bits(both.r) != bits(gx.d), "control: the parts swapped agree");
let two = g(
Dc {
v: dv(&x),
r: zero,
i: DV::splat_f(2.0),
},
Dc::from_vt(dv(&y), zero),
dv(&c),
s,
);
assert!(
bits(two.i) != bits(gx.d),
"control: a seed of 2 agrees with a seed of 1"
);
}
}
#[test]
fn val_qd_marks_the_quad_double_lanes() {
fn val_qd<T: Lane>() -> bool {
T::VAL_QD
}
let quad = [
val_qd::<QV>(),
val_qd::<Qu>(),
val_qd::<Counted<Qu>>(),
val_qd::<Counted<QV>>(),
];
let other = [
val_qd::<DV<Native>>(),
val_qd::<Du>(),
val_qd::<Dc>(),
val_qd::<F8>(),
val_qd::<Fu>(),
val_qd::<Dtn<2>>(),
val_qd::<Fun<2>>(),
val_qd::<Counted<Du>>(),
val_qd::<Jet<4>>(),
];
assert!(quad.iter().all(|&q| q), "{quad:?}");
assert!(other.iter().all(|&q| !q), "{other:?}");
}
#[test]
fn is_pwr2_accepts_normal_powers_of_two_only() {
use multidouble::dd::is_pwr2;
for k in -1022..=1023 {
assert!(is_pwr2(2f64.powi(k)) && is_pwr2(-(2f64.powi(k))), "2^{k}");
}
for b in [
0.0,
-0.0,
3.0,
0.75,
1.0 + f64::EPSILON,
f64::MIN_POSITIVE / 2.0,
f64::INFINITY,
f64::NAN,
] {
assert!(!is_pwr2(b), "{b:e}");
}
}
#[cfg(debug_assertions)]
#[test]
#[should_panic(expected = "not a power of two")]
fn mul_pwr2_refuses_other_scales_in_debug() {
let _ = Du::from_vt(DV::splat_f(1.0), DV::splat_f(1.0)).mul_pwr2(3.0);
}
fn f_acc<T: Lane>(x: T, y: T) -> T {
let c = DV::splat_f(0.5);
let a = T::acc1(T::accc(T::acc(T::acc(T::acc0(), x, y), y, y), x, c), x);
-(T::fin(a) - f(x, y)).mulf(3.0).addc(c).mul_pwr2(0.25).mulc(c)
}
#[test]
fn counted_lanes_count_and_change_nothing() {
fn run<T: Lane>(x: T, y: T) -> (T, [u64; 11]) {
take_counts();
let v = f_acc(x, y);
(v, take_counts())
}
let mut r = Rng(31);
let (x, y) = inputs(&mut r);
let (xd, yd) = (dv::<Native>(&x), dv::<Native>(&y));
let want = [3, 4, 1, 1, 1, 2, 1, 2, 1, 1, 1];
let (plain, n0) = run(xd, yd);
let (cv, n1) = run(Counted(xd), Counted(yd));
assert_eq!(n0, [0; 11], "plain lanes are not counted");
assert_eq!(n1, want, "counts of g on Counted<DV> ({COUNTED_OPS:?})");
for j in 0..8 {
let (a, b) = (plain.lane(j), cv.0.lane(j));
assert!(
a.hi.to_bits() == b.hi.to_bits() && a.lo.to_bits() == b.lo.to_bits(),
"lane {j}"
);
}
let (xu, yu) = (Du::from_vt(xd, DV::splat_f(1.0)), Du::from_vt(yd, DV::zero()));
let (pu, _) = run(xu, yu);
let (cu, nu) = run(Counted(xu), Counted(yu));
assert_eq!(nu, want, "counts of g on Counted<Du>");
for (a, b) in [(pu.val(), cu.val()), (pu.tan(), cu.tan())] {
for j in 0..8 {
assert!(
a.lane(j).hi.to_bits() == b.lane(j).hi.to_bits() && a.lane(j).lo.to_bits() == b.lane(j).lo.to_bits()
);
}
}
take_counts();
let _ = f(Counted(xd), Counted(yd));
let nf = take_counts();
assert_eq!(nf, [2, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0], "counts of f");
assert_ne!(nf, want, "control: f and g counted alike");
}
fn f_qd(x: QD, y: QD) -> (QD, QD, QD) {
let two = QD::from_f64(2.0);
let three = QD::from_f64(3.0);
let num = x * x * x + two * x * y;
let den = x + three;
let fx = ((three * x * x + two * y) * den - num) / (den * den);
let fy = (two * x) / den;
(num / den, fx, fy)
}
fn err_qd(a: QD, b: QD) -> f64 {
(a - b).to_f64().abs() / b.to_f64().abs().max(1.0)
}
fn qu_errors(dd_tangent: bool) -> f64 {
let mut r = Rng(27);
let mut worst = 0.0f64;
for _ in 0..300 {
let mut x: [QD; 8] =
std::array::from_fn(|_| QD::new(1.0 + 4.0 * r.unit(), r.sym() * 1e-17, r.sym() * 1e-34, r.sym() * 1e-51));
let mut y: [QD; 8] =
std::array::from_fn(|_| QD::new(r.sym() * 5.0, r.sym() * 1e-16, r.sym() * 1e-33, r.sym() * 1e-50));
let got: [Vec<QD>; 3] = if dd_tangent {
x = x.map(|v| QD::from_dd(v.to_dd()));
y = y.map(|v| QD::from_dd(v.to_dd()));
let d = |v: &[QD; 8]| dv::<Native>(&v.map(|q| q.to_dd()));
let one = DV::splat_f(1.0);
let gx = f(Du::from_vt(d(&x), one), Du::from_vt(d(&y), DV::zero()));
let gy = f(Du::from_vt(d(&x), DV::zero()), Du::from_vt(d(&y), one));
let l = |v: DV<Native>| (0..8).map(|j| QD::from_dd(v.lane(j))).collect::<Vec<_>>();
[l(gx.v), l(gx.d), l(gy.d)]
} else {
let (one, zero) = (QV::splat(QD::ONE), QV::zero());
let gx = f(
Qu {
v: QV::from_lanes(&x),
d: one,
},
Qu {
v: QV::from_lanes(&y),
d: zero,
},
);
let gy = f(
Qu {
v: QV::from_lanes(&x),
d: zero,
},
Qu {
v: QV::from_lanes(&y),
d: one,
},
);
[gx.v.lanes().to_vec(), gx.d.lanes().to_vec(), gy.d.lanes().to_vec()]
};
for j in 0..8 {
let (val, fx, fy) = f_qd(x[j], y[j]);
for (g, want) in [(got[0][j], val), (got[1][j], fx), (got[2][j], fy)] {
worst = worst.max(err_qd(g, want));
}
}
}
worst
}
#[test]
fn qu_gives_quad_double_partial_derivatives() {
let e = qu_errors(false);
println!("Qu: worst error {e:.1e}");
assert!(e < 1e-58, "{e:e}");
}
#[test]
fn control_du_tangents_fail_the_quad_double_check() {
let e = qu_errors(true);
println!("Du against the quad-double reference: worst error {e:.1e}");
assert!(e > 1e-40, "{e:e}");
}
#[test]
fn qu_value_is_qv_bitwise() {
let mut r = Rng(28);
let s = DD::ONE / DD::from_f64(3.0);
for _ in 0..200 {
let x: [DD; 8] = std::array::from_fn(|_| DD::new(2.0 + r.unit(), r.sym() * 1e-16));
let y: [DD; 8] = std::array::from_fn(|_| DD::new(1.0 + r.unit(), r.sym() * 1e-16));
let c: [DD; 8] = std::array::from_fn(|_| DD::new(1.0 + r.unit(), r.sym() * 1e-16));
let gq = g(QV::from_dv(dv(&x)), QV::from_dv(dv(&y)), dv(&c), s);
let gu = g(
Qu::from_vt(dv(&x), DV::splat_f(1.0)),
Qu::from_vt(dv(&y), DV::zero()),
dv(&c),
s,
);
for q in 0..4 {
let (a, b) = (gq.c[q].to_array(), gu.v.c[q].to_array());
assert!(
a.iter().zip(&b).all(|(u, v)| u.to_bits() == v.to_bits()),
"part {q}: {a:?} vs {b:?}"
);
}
}
}
fn v8_bits(v: V8<Native>) -> Vec<u64> {
let mut a = [0.0; 8];
v.store(&mut a);
a.map(f64::to_bits).to_vec()
}
fn dv_bits(v: DV<Native>) -> Vec<u64> {
[v8_bits(v.hi), v8_bits(v.lo)].concat()
}
fn qv_bits(v: QV) -> Vec<u64> {
v.c.iter().flat_map(|&c| v8_bits(c)).collect()
}
fn dual_outputs<T: Lane<S = Native>>(bits: fn(T) -> Vec<u64>, nudge: bool) -> Vec<Vec<u64>> {
let mut r = Rng(39);
let s = DD::ONE / DD::from_f64(7.0);
let mut w = vec![Vec::new(); 3];
for i in 0..20 {
let x: T = nudged_lane(&mut r, nudge && i == 0);
let y: T = random_lane(&mut r);
let c = dv(&std::array::from_fn(|_| r.dd(2)));
w[0].extend([bits(x * y), bits(x / y)].concat());
w[1].extend(bits(T::fin(T::acc1(T::accc(T::acc(T::acc0(), x, y), y, c), x))));
w[2].extend(bits(g(x, y, c, s)));
}
w
}
fn dual_mismatches<T: Lane<S = Native>>(name: &str, bits: fn(T) -> Vec<u64>, want: [u64; 3]) -> Vec<String> {
let names = ["product and quotient", "accumulator", "every Lane method"].map(|g| format!("{name} {g}"));
let names: Vec<&str> = names.iter().map(|n| n.as_str()).collect();
common::pin_mismatches(&names, &dual_outputs(bits, false), &dual_outputs(bits, true), &want)
}
#[test]
fn dual_outputs_are_pinned() {
let mut bad = Vec::new();
bad.extend(dual_mismatches::<Du>(
"Du",
|x| [dv_bits(x.v), dv_bits(x.d)].concat(),
[0x45f4_cdb7_5aab_2b9d, 0x95b1_ede5_43ef_f9a8, 0x4cf7_0a93_51e6_fe44],
));
bad.extend(dual_mismatches::<Dc>(
"Dc",
|x| [dv_bits(x.v), dv_bits(x.r), dv_bits(x.i)].concat(),
[0x0679_8a53_2b10_989d, 0xe25f_5c9f_100a_8228, 0x81ad_a1cb_48f2_dd44],
));
bad.extend(dual_mismatches::<Qu>(
"Qu",
|x| [qv_bits(x.v), qv_bits(x.d)].concat(),
[0x1c5f_166f_1ce5_153e, 0x81d2_45b1_a3d5_dd28, 0xe28f_da15_f617_9db8],
));
bad.extend(dual_mismatches::<F8>(
"F8",
|x| v8_bits(x.0),
[0x3ca4_595b_a224_e9dc, 0x261b_b538_7d87_4e60, 0x7528_5721_cae9_fae0],
));
bad.extend(dual_mismatches::<Fu>(
"Fu",
|x| [v8_bits(x.v), v8_bits(x.d)].concat(),
[0xad11_9f0e_d667_a92f, 0x6d85_92b9_6c62_0454, 0x09bb_5f64_1438_ffef],
));
bad.extend(dual_mismatches::<Dtn<3>>(
"Dtn<3>",
|x| [dv_bits(x.v), x.d.iter().flat_map(|&d| v8_bits(d)).collect()].concat(),
[0x3433_5025_b06d_fb92, 0x0de6_7438_5c5b_8d96, 0x31fa_4905_ffd7_fe56],
));
bad.extend(dual_mismatches::<Fun<3>>(
"Fun<3>",
|x| [v8_bits(x.v), x.d.iter().flat_map(|&d| v8_bits(d)).collect()].concat(),
[0xe100_30d7_742a_f188, 0x30bc_1854_d2a0_4e26, 0x097f_ce43_f176_0c58],
));
assert!(bad.is_empty(), "{bad:#?}");
}