Skip to content

Commit

Permalink
Simplify bit operations by reusing assign impl (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Mar 6, 2024
1 parent 774756c commit ea74b0a
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions math/src/unsigned_integer/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,12 @@ impl<const NUM_LIMBS: usize> ShrAssign<usize> for UnsignedInteger<NUM_LIMBS> {
impl<const NUM_LIMBS: usize> BitAnd for UnsignedInteger<NUM_LIMBS> {
type Output = Self;

#[inline(always)]
fn bitand(self, rhs: Self) -> Self::Output {
let Self { mut limbs } = self;

for (a_i, b_i) in limbs.iter_mut().zip(rhs.limbs.iter()) {
*a_i &= b_i;
}
Self { limbs }
let mut result = self;
result &= rhs;
result
}
}

Expand All @@ -345,14 +343,12 @@ impl<const NUM_LIMBS: usize> BitAndAssign for UnsignedInteger<NUM_LIMBS> {
impl<const NUM_LIMBS: usize> BitOr for UnsignedInteger<NUM_LIMBS> {
type Output = Self;

#[inline(always)]
fn bitor(self, rhs: Self) -> Self::Output {
let Self { mut limbs } = self;

for (a_i, b_i) in limbs.iter_mut().zip(rhs.limbs.iter()) {
*a_i |= b_i;
}
Self { limbs }
let mut result = self;
result |= rhs;
result
}
}

Expand All @@ -369,14 +365,12 @@ impl<const NUM_LIMBS: usize> BitOrAssign for UnsignedInteger<NUM_LIMBS> {
impl<const NUM_LIMBS: usize> BitXor for UnsignedInteger<NUM_LIMBS> {
type Output = Self;

#[inline(always)]
fn bitxor(self, rhs: Self) -> Self::Output {
let Self { mut limbs } = self;

for (a_i, b_i) in limbs.iter_mut().zip(rhs.limbs.iter()) {
*a_i ^= b_i;
}
Self { limbs }
let mut result = self;
result ^= rhs;
result
}
}

Expand Down

0 comments on commit ea74b0a

Please sign in to comment.