Rollup merge of #129377 - chorman0773:unbounded-shifts-impl, r=scottmcm

Add implementations for `unbounded_shl`/`unbounded_shr`

Tracking Issue: https://github.com/rust-lang/rust/issues/129375

This implements `unbounded_shl` and `unbounded_shr` under the feature gate `unbounded_shifts`
This commit is contained in:
Matthias Krüger 2024-08-26 17:25:31 +02:00 committed by GitHub
commit 68aff290fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 118 additions and 0 deletions

View File

@ -1312,6 +1312,34 @@ pub const fn strict_shl(self, rhs: u32) -> Self {
} }
} }
/// Unbounded shift left. Computes `self << rhs`, without bounding the value of `rhs`
///
/// If `rhs` is larger or equal to the number of bits in `self`,
/// the entire value is shifted out, and `0` is returned.
///
/// # Examples
///
/// Basic usage:
/// ```
/// #![feature(unbounded_shifts)]
#[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".unbounded_shl(4), 0x10);")]
#[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".unbounded_shl(129), 0);")]
/// ```
#[unstable(feature = "unbounded_shifts", issue = "129375")]
#[rustc_const_unstable(feature = "const_unbounded_shifts", issue = "129375")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn unbounded_shl(self, rhs: u32) -> $SelfT{
if rhs < Self::BITS {
// SAFETY:
// rhs is just checked to be in-range above
unsafe { self.unchecked_shl(rhs) }
} else {
0
}
}
/// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is /// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is
/// larger than or equal to the number of bits in `self`. /// larger than or equal to the number of bits in `self`.
/// ///
@ -1410,6 +1438,40 @@ pub const fn strict_shr(self, rhs: u32) -> Self {
} }
} }
/// Unbounded shift right. Computes `self >> rhs`, without bounding the value of `rhs`
///
/// If `rhs` is larger or equal to the number of bits in `self`,
/// the entire value is shifted out, which yields `0` for a positive number,
/// and `-1` for a negative number.
///
/// # Examples
///
/// Basic usage:
/// ```
/// #![feature(unbounded_shifts)]
#[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".unbounded_shr(4), 0x1);")]
#[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".unbounded_shr(129), 0);")]
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.unbounded_shr(129), -1);")]
/// ```
#[unstable(feature = "unbounded_shifts", issue = "129375")]
#[rustc_const_unstable(feature = "const_unbounded_shifts", issue = "129375")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn unbounded_shr(self, rhs: u32) -> $SelfT{
if rhs < Self::BITS {
// SAFETY:
// rhs is just checked to be in-range above
unsafe { self.unchecked_shr(rhs) }
} else {
// A shift by `Self::BITS-1` suffices for signed integers, because the sign bit is copied for each of the shifted bits.
// SAFETY:
// `Self::BITS-1` is guaranteed to be less than `Self::BITS`
unsafe { self.unchecked_shr(Self::BITS - 1) }
}
}
/// Checked absolute value. Computes `self.abs()`, returning `None` if /// Checked absolute value. Computes `self.abs()`, returning `None` if
/// `self == MIN`. /// `self == MIN`.
/// ///

View File

@ -1501,6 +1501,34 @@ pub const fn strict_shl(self, rhs: u32) -> Self {
} }
} }
/// Unbounded shift left. Computes `self << rhs`, without bounding the value of `rhs`
///
/// If `rhs` is larger or equal to the number of bits in `self`,
/// the entire value is shifted out, and `0` is returned.
///
/// # Examples
///
/// Basic usage:
/// ```
/// #![feature(unbounded_shifts)]
#[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".unbounded_shl(4), 0x10);")]
#[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".unbounded_shl(129), 0);")]
/// ```
#[unstable(feature = "unbounded_shifts", issue = "129375")]
#[rustc_const_unstable(feature = "const_unbounded_shifts", issue = "129375")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn unbounded_shl(self, rhs: u32) -> $SelfT{
if rhs < Self::BITS {
// SAFETY:
// rhs is just checked to be in-range above
unsafe { self.unchecked_shl(rhs) }
} else {
0
}
}
/// Checked shift right. Computes `self >> rhs`, returning `None` /// Checked shift right. Computes `self >> rhs`, returning `None`
/// if `rhs` is larger than or equal to the number of bits in `self`. /// if `rhs` is larger than or equal to the number of bits in `self`.
/// ///
@ -1599,6 +1627,34 @@ pub const fn strict_shr(self, rhs: u32) -> Self {
} }
} }
/// Unbounded shift right. Computes `self >> rhs`, without bounding the value of `rhs`
///
/// If `rhs` is larger or equal to the number of bits in `self`,
/// the entire value is shifted out, and `0` is returned.
///
/// # Examples
///
/// Basic usage:
/// ```
/// #![feature(unbounded_shifts)]
#[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".unbounded_shr(4), 0x1);")]
#[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".unbounded_shr(129), 0);")]
/// ```
#[unstable(feature = "unbounded_shifts", issue = "129375")]
#[rustc_const_unstable(feature = "const_unbounded_shifts", issue = "129375")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn unbounded_shr(self, rhs: u32) -> $SelfT{
if rhs < Self::BITS {
// SAFETY:
// rhs is just checked to be in-range above
unsafe { self.unchecked_shr(rhs) }
} else {
0
}
}
/// Checked exponentiation. Computes `self.pow(exp)`, returning `None` if /// Checked exponentiation. Computes `self.pow(exp)`, returning `None` if
/// overflow occurred. /// overflow occurred.
/// ///