Skip to content

Commit

Permalink
Add short division
Browse files Browse the repository at this point in the history
  • Loading branch information
Remco Bloemen committed Apr 1, 2019
1 parent f0c091b commit 1a554d1
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/u256.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{adc, mac, sbb};
use crate::utils::{adc, div_2_1, mac, sbb};
use std::cmp::{Ord, Ordering, PartialOrd};
use std::num::Wrapping;
use std::ops::{
Expand Down Expand Up @@ -93,6 +93,22 @@ impl U256 {
(U256::new(r0, r1, r2, r3), U256::new(r4, r5, r6, r7))
}

// Short division
// TODO: Can be computed in-place
pub fn divrem_u64(&self, rhs: u64) -> Option<(U256, u64)> {
if rhs == 0 {
None
} else {
// Knuth Algorithm S
// 4 by 1 division
let (q3, r) = div_2_1(self.c3, 0, rhs);
let (q2, r) = div_2_1(self.c2, r, rhs);
let (q1, r) = div_2_1(self.c1, r, rhs);
let (q0, r) = div_2_1(self.c0, r, rhs);
Some((U256::new(q0, q1, q2, q3), r))
}
}

pub fn mulmod(&self, rhs: &U256, modulus: &U256) -> U256 {
unimplemented!() // TODO
}
Expand Down Expand Up @@ -626,4 +642,14 @@ mod tests {
let (lo, _hi) = a.mul_full(&b);
r == lo
}

#[quickcheck]
#[test]
fn test_divrem_u64(a: U256, b: u64) -> bool {
match a.divrem_u64(b) {
None => b == 0,
Some((q, r)) => r < b && q * &U256::from(b) + &U256::from(r) == a,
}
}

}

0 comments on commit 1a554d1

Please sign in to comment.