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

Implement saturating_shl and saturating_shr on ints #103441

Closed
Closed
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
72 changes: 72 additions & 0 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,78 @@ macro_rules! int_impl {
intrinsics::saturating_sub(0, self)
}

/// Panic-free bitwise shift-left; saturates to `MIN` or `MAX` instead of wrapping around.
///
/// Note that this is *not* the same as a rotate-left; the RHS of a saturating shift-left is restricted to
/// the range of the type, rather than the bits shifted out of the LHS being returned to the other end.
/// The primitive integer types all implement a [`rotate_left`](Self::rotate_left) function,
/// which may be what you want instead.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(saturating_bit_shifts)]
#[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS - 2), 1_", stringify!($SelfT), " << ", stringify!($SelfT), "::BITS - 2);")]
#[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS - 1), ", stringify!($SelfT), "::MAX);")]
#[doc = concat!("assert_eq!(-1_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS - 2), -1_", stringify!($SelfT), " << ", stringify!($SelfT), "::BITS - 2);")]
#[doc = concat!("assert_eq!(-1_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS - 1), ", stringify!($SelfT), "::MIN);")]
#[doc = concat!("assert_eq!(-1_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS), ", stringify!($SelfT), "::MIN);")]
/// ```
#[unstable(feature = "saturating_bit_shifts", issue = "103440")]
#[rustc_const_unstable(feature = "saturating_bit_shifts", issue = "103440")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn saturating_shl(self, rhs: u32) -> Self {
if rhs == 0 {
self
} else {
// leading zeros ignoring first bit (which indicates negative values)
let leading_zeros = (self << 1).leading_zeros();
let leading_ones = (self << 1).leading_ones();

// would overflow => MIN / MAX depending on whether the value is negative or not
if self >= 0 && leading_zeros < rhs {
<$SelfT>::MAX
} else if self < 0 && leading_ones < rhs {
<$SelfT>::MIN
} else {
// normal shift left
self << rhs
}
}
}

/// Panic-free bitwise shift-right; yields `0` where the shift exceeds the bitwidth of the type.
///
/// Note that this is *not* the same as a rotate-right; the RHS of a saturating shift-right is restricted
/// to the range of the type, rather than the bits shifted out of the LHS being returned to the other
/// end. The primitive integer types all implement a [`rotate_right`](Self::rotate_right) function,
/// which may be what you want instead.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(saturating_bit_shifts)]
#[doc = concat!("assert_eq!(0b1000_0000_u8.saturating_shr(u8::BITS), 0b0000_0000_u8);")]
/// ```
#[unstable(feature = "saturating_bit_shifts", issue = "103440")]
#[rustc_const_unstable(feature = "saturating_bit_shifts", issue = "103440")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn saturating_shr(self, rhs: u32) -> Self {
if rhs >= <$SelfT>::BITS {
0
Copy link
Contributor

Choose a reason for hiding this comment

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

Is 0 the correct value here? Shouldn't it do a sign extend?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I am already working on an alternative solution with that in mind, but ./x.py test takes its time this time.

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this can just be self >> rhs.min(<$SelfT>::BITS) as the whole implementation, for both signed and unsigned even.

} else {
self >> rhs
}
}

/// Saturating absolute value. Computes `self.abs()`, returning `MAX` if `self ==
/// MIN` instead of overflowing.
///
Expand Down
57 changes: 57 additions & 0 deletions library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,63 @@ macro_rules! uint_impl {
}
}

/// Panic-free bitwise shift-left; saturates `MAX` instead of wrapping around.
///
/// Note that this is *not* the same as a rotate-left; the RHS of a saturating shift-left is restricted to
/// the range of the type, rather than the bits shifted out of the LHS being returned to the other end.
/// The primitive integer types all implement a [`rotate_left`](Self::rotate_left) function,
/// which may be what you want instead.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(saturating_bit_shifts)]
#[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS - 1), 1_", stringify!($SelfT), " << ", stringify!($SelfT), "::BITS - 1);")]
#[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS), ", stringify!($SelfT), "::MAX);")]
/// ```
#[unstable(feature = "saturating_bit_shifts", issue = "103440")]
#[rustc_const_unstable(feature = "saturating_bit_shifts", issue = "103440")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn saturating_shl(self, rhs: u32) -> Self {
if rhs > self.leading_zeros() {
<$SelfT>::MAX
} else {
self << rhs
}
}

/// Panic-free bitwise shift-right; yields `0` where the shift exceeds the bitwidth of the type.
///
/// Note that this is *not* the same as a rotate-right; the RHS of a saturating shift-right is restricted
/// to the range of the type, rather than the bits shifted out of the LHS being returned to the other
/// end. The primitive integer types all implement a [`rotate_right`](Self::rotate_right) function,
/// which may be what you want instead.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(saturating_bit_shifts)]
#[doc = concat!("assert_eq!(0b1000_0000_u8.saturating_shr(u8::BITS), 0b0000_0000_u8);")]
/// ```
#[unstable(feature = "saturating_bit_shifts", issue = "103440")]
#[rustc_const_unstable(feature = "saturating_bit_shifts", issue = "103440")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn saturating_shr(self, rhs: u32) -> Self {
if rhs >= <$SelfT>::BITS {
0
} else {
self >> rhs
}
}

/// Wrapping (modular) addition. Computes `self + rhs`,
/// wrapping around at the boundary of the type.
///
Expand Down