mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-26 23:45:30 +00:00
Auto merge of #85703 - clarfonthey:unchecked_shift, r=scottmcm
Add inherent unchecked_shl, unchecked_shr to integers Tracking issue: #85122. Adding more of these methods, since these are missing.
This commit is contained in:
commit
6166929741
@ -407,8 +407,15 @@ macro_rules! int_impl {
|
||||
}
|
||||
|
||||
/// Unchecked integer addition. Computes `self + rhs`, assuming overflow
|
||||
/// cannot occur. This results in undefined behavior when
|
||||
#[doc = concat!("`self + rhs > ", stringify!($SelfT), "::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`.")]
|
||||
/// cannot occur.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This results in undefined behavior when
|
||||
#[doc = concat!("`self + rhs > ", stringify!($SelfT), "::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`,")]
|
||||
/// i.e. when [`checked_add`] would return `None`.
|
||||
///
|
||||
#[doc = concat!("[`checked_add`]: ", stringify!($SelfT), "::checked_add")]
|
||||
#[unstable(
|
||||
feature = "unchecked_math",
|
||||
reason = "niche optimization path",
|
||||
@ -446,8 +453,15 @@ macro_rules! int_impl {
|
||||
}
|
||||
|
||||
/// Unchecked integer subtraction. Computes `self - rhs`, assuming overflow
|
||||
/// cannot occur. This results in undefined behavior when
|
||||
#[doc = concat!("`self - rhs > ", stringify!($SelfT), "::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`.")]
|
||||
/// cannot occur.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This results in undefined behavior when
|
||||
#[doc = concat!("`self - rhs > ", stringify!($SelfT), "::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`,")]
|
||||
/// i.e. when [`checked_sub`] would return `None`.
|
||||
///
|
||||
#[doc = concat!("[`checked_sub`]: ", stringify!($SelfT), "::checked_sub")]
|
||||
#[unstable(
|
||||
feature = "unchecked_math",
|
||||
reason = "niche optimization path",
|
||||
@ -485,8 +499,15 @@ macro_rules! int_impl {
|
||||
}
|
||||
|
||||
/// Unchecked integer multiplication. Computes `self * rhs`, assuming overflow
|
||||
/// cannot occur. This results in undefined behavior when
|
||||
#[doc = concat!("`self * rhs > ", stringify!($SelfT), "::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`.")]
|
||||
/// cannot occur.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This results in undefined behavior when
|
||||
#[doc = concat!("`self * rhs > ", stringify!($SelfT), "::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`,")]
|
||||
/// i.e. when [`checked_mul`] would return `None`.
|
||||
///
|
||||
#[doc = concat!("[`checked_mul`]: ", stringify!($SelfT), "::checked_mul")]
|
||||
#[unstable(
|
||||
feature = "unchecked_math",
|
||||
reason = "niche optimization path",
|
||||
@ -645,6 +666,31 @@ macro_rules! int_impl {
|
||||
if unlikely!(b) {None} else {Some(a)}
|
||||
}
|
||||
|
||||
/// Unchecked shift left. Computes `self << rhs`, assuming that
|
||||
/// `rhs` is less than the number of bits in `self`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This results in undefined behavior if `rhs` is larger than
|
||||
/// or equal to the number of bits in `self`,
|
||||
/// i.e. when [`checked_shl`] would return `None`.
|
||||
///
|
||||
#[doc = concat!("[`checked_shl`]: ", stringify!($SelfT), "::checked_shl")]
|
||||
#[unstable(
|
||||
feature = "unchecked_math",
|
||||
reason = "niche optimization path",
|
||||
issue = "85122",
|
||||
)]
|
||||
#[must_use = "this returns the result of the operation, \
|
||||
without modifying the original"]
|
||||
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
|
||||
#[inline(always)]
|
||||
pub const unsafe fn unchecked_shl(self, rhs: Self) -> Self {
|
||||
// SAFETY: the caller must uphold the safety contract for
|
||||
// `unchecked_shl`.
|
||||
unsafe { intrinsics::unchecked_shl(self, rhs) }
|
||||
}
|
||||
|
||||
/// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is
|
||||
/// larger than or equal to the number of bits in `self`.
|
||||
///
|
||||
@ -666,6 +712,31 @@ macro_rules! int_impl {
|
||||
if unlikely!(b) {None} else {Some(a)}
|
||||
}
|
||||
|
||||
/// Unchecked shift right. Computes `self >> rhs`, assuming that
|
||||
/// `rhs` is less than the number of bits in `self`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This results in undefined behavior if `rhs` is larger than
|
||||
/// or equal to the number of bits in `self`,
|
||||
/// i.e. when [`checked_shr`] would return `None`.
|
||||
///
|
||||
#[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")]
|
||||
#[unstable(
|
||||
feature = "unchecked_math",
|
||||
reason = "niche optimization path",
|
||||
issue = "85122",
|
||||
)]
|
||||
#[must_use = "this returns the result of the operation, \
|
||||
without modifying the original"]
|
||||
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
|
||||
#[inline(always)]
|
||||
pub const unsafe fn unchecked_shr(self, rhs: Self) -> Self {
|
||||
// SAFETY: the caller must uphold the safety contract for
|
||||
// `unchecked_shr`.
|
||||
unsafe { intrinsics::unchecked_shr(self, rhs) }
|
||||
}
|
||||
|
||||
/// Checked absolute value. Computes `self.abs()`, returning `None` if
|
||||
/// `self == MIN`.
|
||||
///
|
||||
|
@ -417,8 +417,15 @@ macro_rules! uint_impl {
|
||||
}
|
||||
|
||||
/// Unchecked integer addition. Computes `self + rhs`, assuming overflow
|
||||
/// cannot occur. This results in undefined behavior when
|
||||
#[doc = concat!("`self + rhs > ", stringify!($SelfT), "::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`.")]
|
||||
/// cannot occur.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This results in undefined behavior when
|
||||
#[doc = concat!("`self + rhs > ", stringify!($SelfT), "::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`,")]
|
||||
/// i.e. when [`checked_add`] would return `None`.
|
||||
///
|
||||
#[doc = concat!("[`checked_add`]: ", stringify!($SelfT), "::checked_add")]
|
||||
#[unstable(
|
||||
feature = "unchecked_math",
|
||||
reason = "niche optimization path",
|
||||
@ -456,8 +463,15 @@ macro_rules! uint_impl {
|
||||
}
|
||||
|
||||
/// Unchecked integer subtraction. Computes `self - rhs`, assuming overflow
|
||||
/// cannot occur. This results in undefined behavior when
|
||||
#[doc = concat!("`self - rhs > ", stringify!($SelfT), "::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`.")]
|
||||
/// cannot occur.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This results in undefined behavior when
|
||||
#[doc = concat!("`self - rhs > ", stringify!($SelfT), "::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`,")]
|
||||
/// i.e. when [`checked_sub`] would return `None`.
|
||||
///
|
||||
#[doc = concat!("[`checked_sub`]: ", stringify!($SelfT), "::checked_sub")]
|
||||
#[unstable(
|
||||
feature = "unchecked_math",
|
||||
reason = "niche optimization path",
|
||||
@ -495,8 +509,15 @@ macro_rules! uint_impl {
|
||||
}
|
||||
|
||||
/// Unchecked integer multiplication. Computes `self * rhs`, assuming overflow
|
||||
/// cannot occur. This results in undefined behavior when
|
||||
#[doc = concat!("`self * rhs > ", stringify!($SelfT), "::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`.")]
|
||||
/// cannot occur.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This results in undefined behavior when
|
||||
#[doc = concat!("`self * rhs > ", stringify!($SelfT), "::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`,")]
|
||||
/// i.e. when [`checked_mul`] would return `None`.
|
||||
///
|
||||
#[doc = concat!("[`checked_mul`]: ", stringify!($SelfT), "::checked_mul")]
|
||||
#[unstable(
|
||||
feature = "unchecked_math",
|
||||
reason = "niche optimization path",
|
||||
@ -655,6 +676,31 @@ macro_rules! uint_impl {
|
||||
if unlikely!(b) {None} else {Some(a)}
|
||||
}
|
||||
|
||||
/// Unchecked shift left. Computes `self << rhs`, assuming that
|
||||
/// `rhs` is less than the number of bits in `self`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This results in undefined behavior if `rhs` is larger than
|
||||
/// or equal to the number of bits in `self`,
|
||||
/// i.e. when [`checked_shl`] would return `None`.
|
||||
///
|
||||
#[doc = concat!("[`checked_shl`]: ", stringify!($SelfT), "::checked_shl")]
|
||||
#[unstable(
|
||||
feature = "unchecked_math",
|
||||
reason = "niche optimization path",
|
||||
issue = "85122",
|
||||
)]
|
||||
#[must_use = "this returns the result of the operation, \
|
||||
without modifying the original"]
|
||||
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
|
||||
#[inline(always)]
|
||||
pub const unsafe fn unchecked_shl(self, rhs: Self) -> Self {
|
||||
// SAFETY: the caller must uphold the safety contract for
|
||||
// `unchecked_shl`.
|
||||
unsafe { intrinsics::unchecked_shl(self, rhs) }
|
||||
}
|
||||
|
||||
/// Checked shift right. Computes `self >> rhs`, returning `None`
|
||||
/// if `rhs` is larger than or equal to the number of bits in `self`.
|
||||
///
|
||||
@ -676,6 +722,31 @@ macro_rules! uint_impl {
|
||||
if unlikely!(b) {None} else {Some(a)}
|
||||
}
|
||||
|
||||
/// Unchecked shift right. Computes `self >> rhs`, assuming that
|
||||
/// `rhs` is less than the number of bits in `self`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This results in undefined behavior if `rhs` is larger than
|
||||
/// or equal to the number of bits in `self`,
|
||||
/// i.e. when [`checked_shr`] would return `None`.
|
||||
///
|
||||
#[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")]
|
||||
#[unstable(
|
||||
feature = "unchecked_math",
|
||||
reason = "niche optimization path",
|
||||
issue = "85122",
|
||||
)]
|
||||
#[must_use = "this returns the result of the operation, \
|
||||
without modifying the original"]
|
||||
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
|
||||
#[inline(always)]
|
||||
pub const unsafe fn unchecked_shr(self, rhs: Self) -> Self {
|
||||
// SAFETY: the caller must uphold the safety contract for
|
||||
// `unchecked_shr`.
|
||||
unsafe { intrinsics::unchecked_shr(self, rhs) }
|
||||
}
|
||||
|
||||
/// Checked exponentiation. Computes `self.pow(exp)`, returning `None` if
|
||||
/// overflow occurred.
|
||||
///
|
||||
|
Loading…
Reference in New Issue
Block a user