Skip to content

Commit c0b3ba1

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

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

library/core/src/num/int_macros.rs

+40
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,46 @@ macro_rules! int_impl {
898898
}
899899
}
900900

901+
/// Unchecked integer division. Computes `self / rhs`.
902+
///
903+
/// Calling `x.unchecked_div(y)` is semantically equivalent to calling
904+
/// `x.`[`checked_div`]`(y).`[`unwrap_unchecked`]`()`.
905+
///
906+
/// If you're just trying to avoid the panic in debug mode, then **do not**
907+
/// use this. Instead, you're looking for [`wrapping_div`].
908+
///
909+
/// # Safety
910+
///
911+
/// This results in undefined behavior when
912+
#[doc = concat!("`rhs == 0` or (`self == ", stringify!($SelfT), "::MIN` and `rhs == -1`)")]
913+
/// i.e. when [`checked_div`] would return `None`.
914+
///
915+
/// [`unwrap_unchecked`]: option/enum.Option.html#method.unwrap_unchecked
916+
#[doc = concat!("[`checked_div`]: ", stringify!($SelfT), "::checked_div")]
917+
#[doc = concat!("[`wrapping_div`]: ", stringify!($SelfT), "::wrapping_div")]
918+
#[unstable(
919+
feature = "unchecked_div_rem",
920+
reason = "consistency with other unchecked_* functions",
921+
issue = "136716",
922+
)]
923+
#[must_use = "this returns the result of the operation, \
924+
without modifying the original"]
925+
#[inline(always)]
926+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
927+
pub const unsafe fn unchecked_div(self, rhs: Self) -> Self {
928+
assert_unsafe_precondition!(
929+
check_language_ub,
930+
concat!(stringify!($SelfT), "::unchecked_div cannot overflow or accept rhs as 0"),
931+
(
932+
lhs: $SelfT = self,
933+
rhs: $SelfT = rhs
934+
) => !lhs.overflowing_div(rhs).1 || !(rhs == 0),
935+
);
936+
937+
// SAFETY: this is guaranteed to be safe by the caller.
938+
unsafe { intrinsics::unchecked_div(self, rhs) }
939+
}
940+
901941
/// Strict integer division. Computes `self / rhs`, panicking
902942
/// if overflow occurred.
903943
///

library/core/src/num/uint_macros.rs

+39
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,45 @@ macro_rules! uint_impl {
10171017
}
10181018
}
10191019

1020+
/// Unchecked integer division. Computes `self / rhs`.
1021+
///
1022+
/// Calling `x.unchecked_div(y)` is semantically equivalent to calling
1023+
/// `x.`[`checked_div`]`(y).`[`unwrap_unchecked`]`()`.
1024+
///
1025+
/// If you're just trying to avoid the panic in debug mode, then **do not**
1026+
/// use this. Instead, you're looking for [`wrapping_div`].
1027+
///
1028+
/// # Safety
1029+
///
1030+
/// This results in undefined behavior when
1031+
#[doc = concat!("`rhs == 0`")]
1032+
/// i.e. when [`checked_div`] would return `None`.
1033+
///
1034+
/// [`unwrap_unchecked`]: option/enum.Option.html#method.unwrap_unchecked
1035+
#[doc = concat!("[`checked_div`]: ", stringify!($SelfT), "::checked_div")]
1036+
#[doc = concat!("[`wrapping_div`]: ", stringify!($SelfT), "::wrapping_div")]
1037+
#[unstable(
1038+
feature = "unchecked_div_rem",
1039+
reason = "consistency with other unchecked_* functions",
1040+
issue = "136716",
1041+
)]
1042+
#[must_use = "this returns the result of the operation, \
1043+
without modifying the original"]
1044+
#[inline(always)]
1045+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1046+
pub const unsafe fn unchecked_div(self, rhs: Self) -> Self {
1047+
assert_unsafe_precondition!(
1048+
check_language_ub,
1049+
concat!(stringify!($SelfT), "::unchecked_div cannot accept rhs as zero"),
1050+
(
1051+
rhs: $SelfT = rhs
1052+
) => !(rhs == 0),
1053+
);
1054+
1055+
// SAFETY: this is guaranteed to be safe by the caller.
1056+
unsafe { intrinsics::unchecked_div(self, rhs) }
1057+
}
1058+
10201059
/// Strict integer division. Computes `self / rhs`.
10211060
///
10221061
/// Strict division on unsigned types is just normal division. There's no

0 commit comments

Comments
 (0)