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

Add inherent unchecked_shl, unchecked_shr to integers #85703

Merged
merged 1 commit into from
May 29, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
83 changes: 77 additions & 6 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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`.
///
Expand All @@ -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`.
///
Expand Down
83 changes: 77 additions & 6 deletions library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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`.
///
Expand All @@ -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.
///
Expand Down