@@ -1085,6 +1085,83 @@ macro_rules! int_impl {
10851085 if unlikely!( b) { overflow_panic:: rem( ) } else { a }
10861086 }
10871087
1088+ /// Checked integer division without remainder. Computes `self / rhs`,
1089+ /// returning `None` if `rhs == 0`, the division results in overflow,
1090+ /// or remainder is not zero.
1091+ ///
1092+ /// # Examples
1093+ ///
1094+ /// Basic usage:
1095+ ///
1096+ /// ```
1097+ /// #![feature(norem_div)]
1098+ #[ doc = concat!( "assert_eq!((" , stringify!( $SelfT) , "::MIN + 1).checked_norem_div(-1), Some(" , stringify!( $Max) , "));" ) ]
1099+ #[ doc = concat!( "assert_eq!((-5" , stringify!( $SelfT) , ").checked_norem_div(2), None);" ) ]
1100+ #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MIN.checked_norem_div(-1), None);" ) ]
1101+ #[ doc = concat!( "assert_eq!((1" , stringify!( $SelfT) , ").checked_norem_div(0), None);" ) ]
1102+ /// ```
1103+ #[ unstable(
1104+ feature = "norem_div" ,
1105+ issue = "1" ,
1106+ ) ]
1107+ #[ must_use = "this returns the result of the operation, \
1108+ without modifying the original"]
1109+ #[ inline]
1110+ pub const fn checked_norem_div( self , rhs: Self ) -> Option <Self > {
1111+ if unlikely!( rhs == 0 || ( ( self == Self :: MIN ) && ( rhs == -1 ) ) ) {
1112+ None
1113+ } else {
1114+ // SAFETY: div by zero and by INT_MIN have been checked above
1115+ unsafe {
1116+ if unlikely!( intrinsics:: unchecked_rem( self , rhs) != 0 ) {
1117+ None
1118+ } else {
1119+ Some ( intrinsics:: unchecked_div( self , rhs) )
1120+ }
1121+ }
1122+ }
1123+ }
1124+
1125+ /// Checked integer division without remainder. Computes `self / rhs`.
1126+ ///
1127+ /// # Panics
1128+ ///
1129+ /// This function will panic if `rhs == 0`, the division results in overflow,
1130+ /// or remainder is not zero.
1131+ ///
1132+ /// # Examples
1133+ ///
1134+ /// Basic usage:
1135+ ///
1136+ /// ```
1137+ /// #![feature(norem_div)]
1138+ #[ doc = concat!( "assert_eq!(64" , stringify!( $SelfT) , ".norem_div(2), 32);" ) ]
1139+ #[ doc = concat!( "assert_eq!(64" , stringify!( $SelfT) , ".norem_div(32), 2);" ) ]
1140+ #[ doc = concat!( "assert_eq!((" , stringify!( $SelfT) , "::MIN + 1).norem_div(-1), " , stringify!( $Max) , ");" ) ]
1141+ /// ```
1142+ ///
1143+ /// ```should_panic
1144+ /// #![feature(norem_div)]
1145+ #[ doc = concat!( "let _ = 65" , stringify!( $SelfT) , ".norem_div(2);" ) ]
1146+ /// ```
1147+ /// ```should_panic
1148+ /// #![feature(norem_div)]
1149+ #[ doc = concat!( "let _ = " , stringify!( $SelfT) , "::MIN.norem_div(-1);" ) ]
1150+ /// ```
1151+ #[ unstable(
1152+ feature = "norem_div" ,
1153+ issue = "1" ,
1154+ ) ]
1155+ #[ must_use = "this returns the result of the operation, \
1156+ without modifying the original"]
1157+ #[ inline]
1158+ pub const fn norem_div( self , rhs: Self ) -> Self {
1159+ match self . checked_norem_div( rhs) {
1160+ Some ( v) => v,
1161+ None => panic!( "Failed to divide without remainder" ) ,
1162+ }
1163+ }
1164+
10881165 /// Checked negation. Computes `-self`, returning `None` if `self == MIN`.
10891166 ///
10901167 /// # Examples
0 commit comments