use super::qint::Qint;
const LOW64: u128 = u64::MAX as u128;
#[inline(always)]
fn p(x: u64, y: u64) -> u128 {
(x as u128) * (y as u128)
}
#[inline(always)]
fn carry(sum: (u128, bool)) -> (u128, u128) {
(sum.0, u128::from(sum.1))
}
#[inline(always)]
fn finish(t6: u128, low: u128, a: Qint, b: Qint) -> Qint {
let ex = u64::from(t6 >> 127 == 0);
let (rh, rl) = if ex == 1 {
((t6 << 1) | (low >> 127), low << 1)
} else {
(t6, low)
};
Qint::from_halves(rh, rl, a.ex + b.ex + 1 - ex as i64, a.sgn ^ b.sgn)
}
#[inline(always)]
fn assemble(r33: u128, t5: u128, c5: u128, t4: u128, c4: u128, a: Qint, b: Qint) -> Qint {
let t6 = r33.wrapping_add((c5 << 64) | (t5 >> 64)).wrapping_add(c4);
finish(t6, (t5 << 64) | (t4 & LOW64), a, b)
}
impl Qint {
pub fn mul(a: Qint, b: Qint) -> Qint {
let r33 = p(a.hh, b.hh);
let r32 = p(a.hh, b.hl);
let r23 = p(a.hl, b.hh);
let r31 = p(a.hh, b.lh);
let r13 = p(a.lh, b.hh);
let r22 = p(a.hl, b.hl);
let r30 = p(a.hh, b.ll);
let r03 = p(a.ll, b.hh);
let r21 = p(a.hl, b.lh);
let r12 = p(a.lh, b.hl);
let t3 = (r12 >> 64) + (r21 >> 64) + (r03 >> 64) + (r30 >> 64);
let (t4, mut c4) = carry(r22.overflowing_add(t3));
let (t4, c) = carry(r13.overflowing_add(t4));
c4 += c;
let (t4, c) = carry(r31.overflowing_add(t4));
c4 += c;
let (t5, mut c5) = carry(r23.overflowing_add(t4 >> 64));
let (t5, c) = carry(r32.overflowing_add(t5));
c5 += c;
assemble(r33, t5, c5, t4, c4, a, b)
}
pub fn mul_33(a: Qint, b: Qint) -> Qint {
let r33 = p(a.hh, b.hh);
let r32 = p(a.hh, b.hl);
let r23 = p(a.hl, b.hh);
let r31 = p(a.hh, b.lh);
let r13 = p(a.lh, b.hh);
let r22 = p(a.hl, b.hl);
let r21 = p(a.hl, b.lh);
let r12 = p(a.lh, b.hl);
let t3 = (r12 >> 64) + (r21 >> 64);
let (t4, mut c4) = carry(r22.overflowing_add(t3));
let (t4, c) = carry(r13.overflowing_add(t4));
c4 += c;
let (t4, c) = carry(r31.overflowing_add(t4));
c4 += c;
let (t5, mut c5) = carry(r23.overflowing_add(t4 >> 64));
let (t5, c) = carry(r32.overflowing_add(t5));
c5 += c;
assemble(r33, t5, c5, t4, c4, a, b)
}
pub fn mul_41(a: Qint, b: Qint) -> Qint {
let r33 = p(a.hh, b.hh);
let r23 = p(a.hl, b.hh);
let r13 = p(a.lh, b.hh);
let r03 = p(a.ll, b.hh);
let (t4, c4) = carry(r13.overflowing_add(r03 >> 64));
let (t5, c5) = carry(r23.overflowing_add(t4 >> 64));
assemble(r33, t5, c5, t4, c4, a, b)
}
pub fn mul_31(a: Qint, b: Qint) -> Qint {
let r33 = p(a.hh, b.hh);
let r23 = p(a.hl, b.hh);
let t4 = p(a.lh, b.hh);
let (t5, c5) = carry(r23.overflowing_add(t4 >> 64));
assemble(r33, t5, c5, t4, 0, a, b)
}
pub fn mul_22(a: Qint, b: Qint) -> Qint {
let r33 = p(a.hh, b.hh);
let r32 = p(a.hh, b.hl);
let r23 = p(a.hl, b.hh);
let t4 = p(a.hl, b.hl);
let (t5, mut c5) = carry(r23.overflowing_add(t4 >> 64));
let (t5, c) = carry(r32.overflowing_add(t5));
c5 += c;
assemble(r33, t5, c5, t4, 0, a, b)
}
pub fn mul_21(a: Qint, b: Qint) -> Qint {
let r33 = p(a.hh, b.hh);
let r23 = p(a.hl, b.hh);
finish(r33.wrapping_add(r23 >> 64), r23 << 64, a, b)
}
pub fn mul_11(a: Qint, b: Qint) -> Qint {
finish(p(a.hh, b.hh), 0, a, b)
}
pub fn mul_int(b: i64, a: Qint) -> Qint {
if b == 0 {
return Qint::ZERO;
}
let sgn = u64::from(b < 0) ^ a.sgn;
let mut c = b.unsigned_abs();
if c == 1 {
return Qint { sgn, ..a };
}
let k = c.leading_zeros();
c <<= k;
let mut ex = a.ex + 64 - k as i64;
let mut t3 = p(a.hh, c);
let t2 = p(a.hl, c);
let t1 = p(a.lh, c);
let t0 = p(a.ll, c);
let (t1, cy) = carry((t0 >> 64).overflowing_add(t1));
let (t2, cy) = carry(((cy << 64) | (t1 >> 64)).overflowing_add(t2));
t3 = t3.wrapping_add((cy << 64) | (t2 >> 64));
let low = (t2 << 64) | (t1 & LOW64);
let (rh, rl) = if (t3 >> 64) as u64 >> 63 == 0 {
ex -= 1;
((t3 << 1) | (low >> 127), low << 1)
} else {
(t3, low)
};
Qint::from_halves(rh, rl, ex, sgn)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn q(x: f64) -> Qint {
Qint::from_f64(x)
}
#[test]
fn test_products_match_binary64_on_short_operands() {
for (x, y) in [(1.5, -0.25), (3.0, 7.0), (0.1, 10.0), (-1.0e100, 1.0e-200)] {
for product in [
Qint::mul(q(x), q(y)),
Qint::mul_33(q(x), q(y)),
Qint::mul_41(q(x), q(y)),
Qint::mul_31(q(x), q(y)),
Qint::mul_22(q(x), q(y)),
Qint::mul_21(q(x), q(y)),
Qint::mul_11(q(x), q(y)),
] {
assert_eq!(product.to_f64(), x * y, "{x} * {y}");
}
}
assert_eq!(Qint::mul_int(-1074, q(1.5)).to_f64(), -1611.0);
assert_eq!(Qint::mul_int(1, q(-0.75)).to_f64(), -0.75);
assert_eq!(Qint::mul_int(4_399_162, q(0.5)).to_f64(), 2_199_581.0);
assert_eq!(Qint::mul_int(0, q(2.0)), Qint::ZERO);
}
#[test]
fn test_full_product_keeps_low_limbs() {
let a = Qint::from_halves(1 << 127, 1 << 63, 0, 0);
let r = Qint::mul(a, a);
assert_eq!((r.rh(), r.rl(), r.ex), (1 << 127, 1 << 64, 0));
}
}