From ea74b0a0dfc6c3444d4a71bf31031f57b6e43849 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Wed, 6 Mar 2024 22:25:25 +0100 Subject: [PATCH] Simplify bit operations by reusing assign impl (#817) --- math/src/unsigned_integer/element.rs | 30 +++++++++++----------------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/math/src/unsigned_integer/element.rs b/math/src/unsigned_integer/element.rs index 4aade932e..409587c35 100644 --- a/math/src/unsigned_integer/element.rs +++ b/math/src/unsigned_integer/element.rs @@ -322,14 +322,12 @@ impl ShrAssign for UnsignedInteger { impl BitAnd for UnsignedInteger { 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 } } @@ -345,14 +343,12 @@ impl BitAndAssign for UnsignedInteger { impl BitOr for UnsignedInteger { 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 } } @@ -369,14 +365,12 @@ impl BitOrAssign for UnsignedInteger { impl BitXor for UnsignedInteger { 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 } }