Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishdaulatabad committed Apr 3, 2024
1 parent aae93ac commit 0e2edf3
Showing 1 changed file with 48 additions and 36 deletions.
84 changes: 48 additions & 36 deletions src/u512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,49 +417,61 @@ impl U512 {
}

fn div_internal(self, other: Self) -> Self {
let div = other;
let mut divisor = other;
let leading_zeros = divisor.leading_zeros();
divisor <<= leading_zeros - self.leading_zeros();

let mut value = self;
let mut quotient = Self::MIN;

while value >= div {
while value < divisor {
divisor >>= 1;
quotient <<= 1;
match self.cmp(&other) {
core::cmp::Ordering::Less => Self::ZERO,
core::cmp::Ordering::Equal => Self::ONE,
_ => {
let div = other;
let mut divisor = other;
let leading_zeros = divisor.leading_zeros();
divisor <<= leading_zeros - self.leading_zeros();

let mut value = self;
let mut quotient = Self::MIN;

while value >= div {
while value < divisor {
divisor >>= 1;
quotient <<= 1;
}

value -= divisor;
quotient = quotient.add_single(1);
}
let rem_offset = div.leading_zeros() - divisor.leading_zeros();

quotient << rem_offset
}

value -= divisor;
quotient = quotient.add_single(1);
}
let rem_offset = div.leading_zeros() - divisor.leading_zeros();
println!("Rem: {rem_offset}");

quotient << rem_offset
}

fn rem_internal(self, other: Self) -> Self {
let div = other;
let mut divisor = other;
let leading_zeros = divisor.leading_zeros();
divisor <<= leading_zeros - self.leading_zeros();

let mut value = self;
let mut quotient = U512::ZERO;

while value >= div {
while value < divisor {
divisor >>= 1;
quotient <<= 1;
match self.cmp(&other) {
core::cmp::Ordering::Less => self,
core::cmp::Ordering::Equal => Self::ZERO,
_ => {
let div = other;
let mut divisor = other;
let leading_zeros = divisor.leading_zeros();
divisor <<= leading_zeros - self.leading_zeros();

let mut value = self;
let mut quotient = Self::MIN;

while value >= div {
while value < divisor {
divisor >>= 1;
quotient <<= 1;
}

value -= divisor;
quotient = quotient.add_single(1);
}
let rem_offset = div.leading_zeros() - divisor.leading_zeros();

quotient << rem_offset
}

value -= divisor;
quotient = quotient.add_single(1);
}

value
}

pub fn div_single(self, divisor: u64) -> Self {
Expand Down

0 comments on commit 0e2edf3

Please sign in to comment.