pub fn div_rem<const N: usize>(numerator: &[u64; N], divisor: &[u64; N]) -> ([u64; N], [u64; N]) {
let numerator_bits = bits(numerator);
let divisor_bits = bits(divisor);
assert_ne!(divisor_bits, 0, "division by zero");
if numerator_bits < divisor_bits {
return ([0; N], *numerator);
}
if divisor_bits <= 64 {
return div_rem_small(numerator, divisor[0]);
}
let numerator_words = (numerator_bits + 63) / 64;
let divisor_words = (divisor_bits + 63) / 64;
let n = divisor_words;
let m = numerator_words - divisor_words;
div_rem_knuth(numerator, divisor, n, m)
}
fn bits(arr: &[u64]) -> usize {
for (idx, v) in arr.iter().enumerate().rev() {
if *v > 0 {
return 64 - v.leading_zeros() as usize + 64 * idx;
}
}
0
}
fn div_rem_small<const N: usize>(numerator: &[u64; N], divisor: u64) -> ([u64; N], [u64; N]) {
let mut rem = 0u64;
let mut numerator = *numerator;
numerator.iter_mut().rev().for_each(|d| {
let (q, r) = div_rem_word(rem, *d, divisor);
*d = q;
rem = r;
});
let mut rem_padded = [0; N];
rem_padded[0] = rem;
(numerator, rem_padded)
}
fn div_rem_knuth<const N: usize>(
numerator: &[u64; N],
divisor: &[u64; N],
n: usize,
m: usize,
) -> ([u64; N], [u64; N]) {
assert!(n + m <= N);
let shift = divisor[n - 1].leading_zeros();
let divisor = shl_word(divisor, shift);
let mut numerator = full_shl(numerator, shift);
let b0 = divisor[n - 1];
let b1 = divisor[n - 2];
let mut q = [0; N];
for j in (0..=m).rev() {
let a0 = numerator[j + n];
let a1 = numerator[j + n - 1];
let mut q_hat = if a0 < b0 {
let (mut q_hat, mut r_hat) = div_rem_word(a0, a1, b0);
let a2 = numerator[j + n - 2];
loop {
let r = u128::from(q_hat) * u128::from(b1);
let (lo, hi) = (r as u64, (r >> 64) as u64);
if (hi, lo) <= (r_hat, a2) {
break;
}
q_hat -= 1;
let (new_r_hat, overflow) = r_hat.overflowing_add(b0);
r_hat = new_r_hat;
if overflow {
break;
}
}
q_hat
} else {
u64::MAX
};
let q_hat_v = full_mul_u64(&divisor, q_hat);
let c = sub_assign(&mut numerator[j..], &q_hat_v[..n + 1]);
if c {
q_hat -= 1;
let c = add_assign(&mut numerator[j..], &divisor[..n]);
numerator[j + n] = numerator[j + n].wrapping_add(u64::from(c));
}
q[j] = q_hat;
}
let remainder = full_shr(&numerator, shift);
(q, remainder)
}
fn div_rem_word(hi: u64, lo: u64, divisor: u64) -> (u64, u64) {
debug_assert!(hi < divisor);
debug_assert_ne!(divisor, 0);
#[cfg(all(target_arch = "x86_64", not(miri)))]
unsafe {
let mut quot = lo;
let mut rem = hi;
std::arch::asm!(
"div {divisor}",
divisor = in(reg) divisor,
inout("rax") quot,
inout("rdx") rem,
options(pure, nomem, nostack)
);
(quot, rem)
}
#[cfg(any(not(target_arch = "x86_64"), miri))]
{
let x = (u128::from(hi) << 64) + u128::from(lo);
let y = u128::from(divisor);
((x / y) as u64, (x % y) as u64)
}
}
fn add_assign(a: &mut [u64], b: &[u64]) -> bool {
binop_slice(a, b, u64::overflowing_add)
}
fn sub_assign(a: &mut [u64], b: &[u64]) -> bool {
binop_slice(a, b, u64::overflowing_sub)
}
fn binop_slice(a: &mut [u64], b: &[u64], binop: impl Fn(u64, u64) -> (u64, bool) + Copy) -> bool {
let mut c = false;
a.iter_mut().zip(b.iter()).for_each(|(x, y)| {
let (res1, overflow1) = y.overflowing_add(u64::from(c));
let (res2, overflow2) = binop(*x, res1);
*x = res2;
c = overflow1 || overflow2;
});
c
}
fn full_mul_u64<const N: usize>(a: &[u64; N], b: u64) -> ArrayPlusOne<u64, N> {
let mut carry = 0;
let mut out = [0; N];
out.iter_mut().zip(a).for_each(|(o, v)| {
let r = *v as u128 * b as u128 + carry as u128;
*o = r as u64;
carry = (r >> 64) as u64;
});
ArrayPlusOne(out, carry)
}
fn shl_word<const N: usize>(v: &[u64; N], shift: u32) -> [u64; N] {
full_shl(v, shift).0
}
fn full_shl<const N: usize>(v: &[u64; N], shift: u32) -> ArrayPlusOne<u64, N> {
debug_assert!(shift < 64);
if shift == 0 {
return ArrayPlusOne(*v, 0);
}
let mut out = [0u64; N];
out[0] = v[0] << shift;
for i in 1..N {
out[i] = (v[i - 1] >> (64 - shift)) | (v[i] << shift)
}
let carry = v[N - 1] >> (64 - shift);
ArrayPlusOne(out, carry)
}
fn full_shr<const N: usize>(a: &ArrayPlusOne<u64, N>, shift: u32) -> [u64; N] {
debug_assert!(shift < 64);
if shift == 0 {
return a.0;
}
let mut out = [0; N];
for i in 0..N - 1 {
out[i] = (a[i] >> shift) | (a[i + 1] << (64 - shift))
}
out[N - 1] = a[N - 1] >> shift;
out
}
#[repr(C)]
struct ArrayPlusOne<T, const N: usize>([T; N], T);
impl<T, const N: usize> std::ops::Deref for ArrayPlusOne<T, N> {
type Target = [T];
#[inline]
fn deref(&self) -> &Self::Target {
let x = self as *const Self;
unsafe { std::slice::from_raw_parts(x as *const T, N + 1) }
}
}
impl<T, const N: usize> std::ops::DerefMut for ArrayPlusOne<T, N> {
fn deref_mut(&mut self) -> &mut Self::Target {
let x = self as *mut Self;
unsafe { std::slice::from_raw_parts_mut(x as *mut T, N + 1) }
}
}