Skip to content

Commit df83987

Browse files
added unchecked_div for signed and unsigned types
1 parent e0be1a0 commit df83987

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

library/core/src/num/int_macros.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,39 @@ macro_rules! int_impl {
898898
}
899899
}
900900

901+
/// Unchecked integer division. Computes `self / rhs`, assuming `rhs != 0` and
902+
/// overflow cannot occur.
903+
///
904+
/// Calling `x.unchecked_div(y)` is semantically equivalent to calling
905+
/// `x.`[`checked_div`]`(y).`[`unwrap_unchecked`]`()`.
906+
///
907+
/// # Safety
908+
///
909+
/// This results in undefined behavior when `rhs == 0` or
910+
#[doc = concat!("(`self == ", stringify!($SelfT), "::MIN` and `rhs == -1`),")]
911+
/// i.e. when [`checked_div`] would return `None`.
912+
///
913+
/// [`unwrap_unchecked`]: option/enum.Option.html#method.unwrap_unchecked
914+
#[doc = concat!("[`checked_div`]: ", stringify!($SelfT), "::checked_div")]
915+
#[doc = concat!("[`wrapping_div`]: ", stringify!($SelfT), "::wrapping_div")]
916+
#[unstable(feature = "unchecked_div_rem", issue = "136716")]
917+
#[must_use = "this returns the result of the operation, \
918+
without modifying the original"]
919+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
920+
pub const unsafe fn unchecked_div(self, rhs: Self) -> Self {
921+
assert_unsafe_precondition!(
922+
check_language_ub,
923+
concat!(stringify!($SelfT), "::unchecked_div cannot overflow or divide by zero"),
924+
(
925+
lhs: $SelfT = self,
926+
rhs: $SelfT = rhs
927+
) => rhs != 0 && !lhs.overflowing_div(rhs).1,
928+
);
929+
930+
// SAFETY: this is guaranteed to be safe by the caller.
931+
unsafe { intrinsics::unchecked_div(self, rhs) }
932+
}
933+
901934
/// Strict integer division. Computes `self / rhs`, panicking
902935
/// if overflow occurred.
903936
///

library/core/src/num/uint_macros.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,36 @@ macro_rules! uint_impl {
10171017
}
10181018
}
10191019

1020+
/// Unchecked integer division. Computes `self / rhs`, assuming `rhs` is not zero.
1021+
///
1022+
/// Calling `x.unchecked_div(y)` is semantically equivalent to calling
1023+
/// `x.`[`checked_div`]`(y).`[`unwrap_unchecked`]`()`.
1024+
///
1025+
/// # Safety
1026+
///
1027+
/// This results in undefined behavior when `rhs == 0`
1028+
/// i.e. when [`checked_div`] would return `None`.
1029+
///
1030+
/// [`unwrap_unchecked`]: option/enum.Option.html#method.unwrap_unchecked
1031+
#[doc = concat!("[`checked_div`]: ", stringify!($SelfT), "::checked_div")]
1032+
#[doc = concat!("[`wrapping_div`]: ", stringify!($SelfT), "::wrapping_div")]
1033+
#[unstable(feature = "unchecked_div_rem", issue = "136716")]
1034+
#[must_use = "this returns the result of the operation, \
1035+
without modifying the original"]
1036+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1037+
pub const unsafe fn unchecked_div(self, rhs: Self) -> Self {
1038+
assert_unsafe_precondition!(
1039+
check_language_ub,
1040+
concat!(stringify!($SelfT), "::unchecked_div cannot divide by zero"),
1041+
(
1042+
rhs: $SelfT = rhs
1043+
) => rhs != 0,
1044+
);
1045+
1046+
// SAFETY: this is guaranteed to be safe by the caller.
1047+
unsafe { intrinsics::unchecked_div(self, rhs) }
1048+
}
1049+
10201050
/// Strict integer division. Computes `self / rhs`.
10211051
///
10221052
/// Strict division on unsigned types is just normal division. There's no

0 commit comments

Comments
 (0)