Skip to content

Commit

Permalink
Bit shifting for unsigned integer doubling (#868)
Browse files Browse the repository at this point in the history
* bit-shifting for unsigned integer doubling

* name quirk

* clippy

* clippy fix

---------

Co-authored-by: Diego K <[email protected]>
Co-authored-by: Mario Rugiero <[email protected]>
  • Loading branch information
3 people authored Aug 15, 2024
1 parent 9395981 commit 3925b01
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions math/src/unsigned_integer/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,22 @@ impl<const NUM_LIMBS: usize> UnsignedInteger<NUM_LIMBS> {
(UnsignedInteger { limbs }, carry > 0)
}

/// Returns the double of `self`.
pub fn double(a: &UnsignedInteger<NUM_LIMBS>) -> (UnsignedInteger<NUM_LIMBS>, bool) {
Self::add(a, a)
let mut cloned = *a;
let overflow = cloned.double_in_place();
(cloned, overflow)
}

pub fn double_in_place(&mut self) -> bool {
let mut msb_of_previous_limb = 0;
for i in (0..NUM_LIMBS).rev() {
let limb_ref = &mut self.limbs[i];
let msb_of_current_limb = *limb_ref >> 63;
*limb_ref <<= 1;
*limb_ref |= msb_of_previous_limb;
msb_of_previous_limb = msb_of_current_limb;
}
msb_of_previous_limb != 0
}

/// Multi-precision subtraction.
Expand Down

0 comments on commit 3925b01

Please sign in to comment.