Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: more Shl+Shr impls #328

Merged
merged 5 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 60 additions & 86 deletions src/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,105 +548,79 @@ impl_bit_op!(BitOr, bitor, BitOrAssign, bitor_assign);
impl_bit_op!(BitAnd, bitand, BitAndAssign, bitand_assign);
impl_bit_op!(BitXor, bitxor, BitXorAssign, bitxor_assign);

impl<const BITS: usize, const LIMBS: usize> ShlAssign<usize> for Uint<BITS, LIMBS> {
#[inline(always)]
fn shl_assign(&mut self, rhs: usize) {
*self = self.wrapping_shl(rhs);
}
}

impl<const BITS: usize, const LIMBS: usize> ShlAssign<&usize> for Uint<BITS, LIMBS> {
#[inline(always)]
fn shl_assign(&mut self, rhs: &usize) {
*self = self.wrapping_shl(*rhs);
}
}

impl<const BITS: usize, const LIMBS: usize> Shl<usize> for Uint<BITS, LIMBS> {
type Output = Self;

#[inline(always)]
fn shl(self, rhs: usize) -> Self {
self.wrapping_shl(rhs)
}
}

impl<const BITS: usize, const LIMBS: usize> Shl<usize> for &Uint<BITS, LIMBS> {
type Output = Uint<BITS, LIMBS>;

#[inline(always)]
fn shl(self, rhs: usize) -> Self::Output {
self.wrapping_shl(rhs)
}
}
macro_rules! impl_shift {
(@main $u:ty) => {
impl<const BITS: usize, const LIMBS: usize> Shl<$u> for Uint<BITS, LIMBS> {
type Output = Self;

impl<const BITS: usize, const LIMBS: usize> Shl<&usize> for Uint<BITS, LIMBS> {
type Output = Self;
#[inline(always)]
fn shl(self, rhs: $u) -> Self::Output {
self.wrapping_shl(rhs as usize)
}
}

#[inline(always)]
fn shl(self, rhs: &usize) -> Self {
self.wrapping_shl(*rhs)
}
}
impl<const BITS: usize, const LIMBS: usize> Shr<$u> for Uint<BITS, LIMBS> {
type Output = Self;

impl<const BITS: usize, const LIMBS: usize> Shl<&usize> for &Uint<BITS, LIMBS> {
type Output = Uint<BITS, LIMBS>;
#[inline(always)]
fn shr(self, rhs: $u) -> Self::Output {
self.wrapping_shr(rhs as usize)
}
}
};

#[inline(always)]
fn shl(self, rhs: &usize) -> Self::Output {
self.wrapping_shl(*rhs)
}
}
(@ref $u:ty) => {
impl<const BITS: usize, const LIMBS: usize> Shl<&$u> for Uint<BITS, LIMBS> {
type Output = Self;

impl<const BITS: usize, const LIMBS: usize> ShrAssign<usize> for Uint<BITS, LIMBS> {
#[inline(always)]
fn shr_assign(&mut self, rhs: usize) {
*self = self.wrapping_shr(rhs);
}
}

impl<const BITS: usize, const LIMBS: usize> ShrAssign<&usize> for Uint<BITS, LIMBS> {
#[inline(always)]
fn shr_assign(&mut self, rhs: &usize) {
*self = self.wrapping_shr(*rhs);
}
}
#[inline(always)]
fn shl(self, rhs: &$u) -> Self::Output {
<Self>::shl(self, *rhs)
}
}

impl<const BITS: usize, const LIMBS: usize> Shr<usize> for Uint<BITS, LIMBS> {
type Output = Self;
impl<const BITS: usize, const LIMBS: usize> Shr<&$u> for Uint<BITS, LIMBS> {
type Output = Self;

#[inline(always)]
fn shr(self, rhs: usize) -> Self {
self.wrapping_shr(rhs)
}
}
#[inline(always)]
fn shr(self, rhs: &$u) -> Self::Output {
<Self>::shr(self, *rhs)
}
}
};

impl<const BITS: usize, const LIMBS: usize> Shr<usize> for &Uint<BITS, LIMBS> {
type Output = Uint<BITS, LIMBS>;
(@assign $u:ty) => {
impl<const BITS: usize, const LIMBS: usize> ShlAssign<$u> for Uint<BITS, LIMBS> {
#[allow(clippy::inline_always)]
#[inline(always)]
fn shl_assign(&mut self, rhs: $u) {
*self = *self << rhs;
}
}

#[inline(always)]
fn shr(self, rhs: usize) -> Self::Output {
self.wrapping_shr(rhs)
}
}
impl<const BITS: usize, const LIMBS: usize> ShrAssign<$u> for Uint<BITS, LIMBS> {
#[allow(clippy::inline_always)]
#[inline(always)]
fn shr_assign(&mut self, rhs: $u) {
*self = *self >> rhs;
}
}
};

impl<const BITS: usize, const LIMBS: usize> Shr<&usize> for Uint<BITS, LIMBS> {
type Output = Self;
($u:ty) => {
impl_shift!(@main $u);
impl_shift!(@ref $u);
impl_shift!(@assign $u);
impl_shift!(@assign &$u);
};

#[inline(always)]
fn shr(self, rhs: &usize) -> Self {
self.wrapping_shr(*rhs)
}
($u:ty, $($tail:ty),*) => {
impl_shift!($u);
impl_shift!($($tail),*);
};
}

impl<const BITS: usize, const LIMBS: usize> Shr<&usize> for &Uint<BITS, LIMBS> {
type Output = Uint<BITS, LIMBS>;

#[inline(always)]
fn shr(self, rhs: &usize) -> Self::Output {
self.wrapping_shr(*rhs)
}
}
impl_shift!(usize, u8, u16, u32, i8, i16, i32, isize);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

u64/i64?


#[cfg(test)]
mod tests {
Expand Down
20 changes: 0 additions & 20 deletions src/support/num_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,6 @@ impl<const BITS: usize, const LIMBS: usize> CheckedRem for Uint<BITS, LIMBS> {
}
}

// TODO: Move out of support.
impl<const BITS: usize, const LIMBS: usize> Shl<u32> for Uint<BITS, LIMBS> {
type Output = Self;

#[inline(always)]
fn shl(self, rhs: u32) -> Self::Output {
<Self>::shl(self, rhs as usize)
}
}

// TODO: Move out of support lib into.
impl<const BITS: usize, const LIMBS: usize> Shr<u32> for Uint<BITS, LIMBS> {
type Output = Self;

#[inline(always)]
fn shr(self, rhs: u32) -> Self::Output {
<Self>::shr(self, rhs as usize)
}
}

impl<const BITS: usize, const LIMBS: usize> CheckedShl for Uint<BITS, LIMBS> {
#[inline(always)]
fn checked_shl(&self, other: u32) -> Option<Self> {
Expand Down