@@ -21,6 +21,21 @@ macro_rules! try_opt {
2121 } ;
2222}
2323
24+ #[ cfg( bootstrap) ]
25+ macro_rules! unlikely {
26+ ( $e: expr) => {
27+ $e
28+ } ;
29+ }
30+
31+ #[ cfg( not( bootstrap) ) ]
32+ #[ allow_internal_unstable( const_likely) ]
33+ macro_rules! unlikely {
34+ ( $e: expr) => {
35+ intrinsics:: unlikely( $e)
36+ } ;
37+ }
38+
2439macro_rules! impl_nonzero_fmt {
2540 ( #[ $stability: meta] ( $( $Trait: ident ) ,+ ) for $Ty: ident ) => {
2641 $(
@@ -746,7 +761,7 @@ $EndFeature, "
746761 #[ inline]
747762 pub const fn checked_add( self , rhs: Self ) -> Option <Self > {
748763 let ( a, b) = self . overflowing_add( rhs) ;
749- if b { None } else { Some ( a) }
764+ if unlikely! ( b ) { None } else { Some ( a) }
750765 }
751766 }
752767
@@ -790,7 +805,7 @@ $EndFeature, "
790805 #[ inline]
791806 pub const fn checked_sub( self , rhs: Self ) -> Option <Self > {
792807 let ( a, b) = self . overflowing_sub( rhs) ;
793- if b { None } else { Some ( a) }
808+ if unlikely! ( b ) { None } else { Some ( a) }
794809 }
795810 }
796811
@@ -834,7 +849,7 @@ $EndFeature, "
834849 #[ inline]
835850 pub const fn checked_mul( self , rhs: Self ) -> Option <Self > {
836851 let ( a, b) = self . overflowing_mul( rhs) ;
837- if b { None } else { Some ( a) }
852+ if unlikely! ( b ) { None } else { Some ( a) }
838853 }
839854 }
840855
@@ -878,7 +893,7 @@ $EndFeature, "
878893 without modifying the original"]
879894 #[ inline]
880895 pub const fn checked_div( self , rhs: Self ) -> Option <Self > {
881- if rhs == 0 || ( self == Self :: MIN && rhs == -1 ) {
896+ if unlikely! ( rhs == 0 || ( self == Self :: MIN && rhs == -1 ) ) {
882897 None
883898 } else {
884899 // SAFETY: div by zero and by INT_MIN have been checked above
@@ -907,7 +922,7 @@ assert_eq!((1", stringify!($SelfT), ").checked_div_euclid(0), None);
907922 without modifying the original"]
908923 #[ inline]
909924 pub const fn checked_div_euclid( self , rhs: Self ) -> Option <Self > {
910- if rhs == 0 || ( self == Self :: MIN && rhs == -1 ) {
925+ if unlikely! ( rhs == 0 || ( self == Self :: MIN && rhs == -1 ) ) {
911926 None
912927 } else {
913928 Some ( self . div_euclid( rhs) )
@@ -936,7 +951,7 @@ $EndFeature, "
936951 without modifying the original"]
937952 #[ inline]
938953 pub const fn checked_rem( self , rhs: Self ) -> Option <Self > {
939- if rhs == 0 || ( self == Self :: MIN && rhs == -1 ) {
954+ if unlikely! ( rhs == 0 || ( self == Self :: MIN && rhs == -1 ) ) {
940955 None
941956 } else {
942957 // SAFETY: div by zero and by INT_MIN have been checked above
@@ -964,7 +979,7 @@ assert_eq!(", stringify!($SelfT), "::MIN.checked_rem_euclid(-1), None);
964979 without modifying the original"]
965980 #[ inline]
966981 pub const fn checked_rem_euclid( self , rhs: Self ) -> Option <Self > {
967- if rhs == 0 || ( self == Self :: MIN && rhs == -1 ) {
982+ if unlikely! ( rhs == 0 || ( self == Self :: MIN && rhs == -1 ) ) {
968983 None
969984 } else {
970985 Some ( self . rem_euclid( rhs) )
@@ -990,7 +1005,7 @@ $EndFeature, "
9901005 #[ inline]
9911006 pub const fn checked_neg( self ) -> Option <Self > {
9921007 let ( a, b) = self . overflowing_neg( ) ;
993- if b { None } else { Some ( a) }
1008+ if unlikely! ( b ) { None } else { Some ( a) }
9941009 }
9951010 }
9961011
@@ -1014,7 +1029,7 @@ $EndFeature, "
10141029 #[ inline]
10151030 pub const fn checked_shl( self , rhs: u32 ) -> Option <Self > {
10161031 let ( a, b) = self . overflowing_shl( rhs) ;
1017- if b { None } else { Some ( a) }
1032+ if unlikely! ( b ) { None } else { Some ( a) }
10181033 }
10191034 }
10201035
@@ -1038,7 +1053,7 @@ $EndFeature, "
10381053 #[ inline]
10391054 pub const fn checked_shr( self , rhs: u32 ) -> Option <Self > {
10401055 let ( a, b) = self . overflowing_shr( rhs) ;
1041- if b { None } else { Some ( a) }
1056+ if unlikely! ( b ) { None } else { Some ( a) }
10421057 }
10431058 }
10441059
@@ -1745,7 +1760,7 @@ $EndFeature, "
17451760 #[ must_use = "this returns the result of the operation, \
17461761 without modifying the original"]
17471762 pub const fn overflowing_div( self , rhs: Self ) -> ( Self , bool ) {
1748- if self == Self :: MIN && rhs == -1 {
1763+ if unlikely! ( self == Self :: MIN && rhs == -1 ) {
17491764 ( self , true )
17501765 } else {
17511766 ( self / rhs, false )
@@ -1778,7 +1793,7 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div_euclid(-1), (", stringi
17781793 #[ must_use = "this returns the result of the operation, \
17791794 without modifying the original"]
17801795 pub const fn overflowing_div_euclid( self , rhs: Self ) -> ( Self , bool ) {
1781- if self == Self :: MIN && rhs == -1 {
1796+ if unlikely! ( self == Self :: MIN && rhs == -1 ) {
17821797 ( self , true )
17831798 } else {
17841799 ( self . div_euclid( rhs) , false )
@@ -1812,7 +1827,7 @@ $EndFeature, "
18121827 #[ must_use = "this returns the result of the operation, \
18131828 without modifying the original"]
18141829 pub const fn overflowing_rem( self , rhs: Self ) -> ( Self , bool ) {
1815- if self == Self :: MIN && rhs == -1 {
1830+ if unlikely! ( self == Self :: MIN && rhs == -1 ) {
18161831 ( 0 , true )
18171832 } else {
18181833 ( self % rhs, false )
@@ -1845,7 +1860,7 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem_euclid(-1), (0, true));
18451860 without modifying the original"]
18461861 #[ inline]
18471862 pub const fn overflowing_rem_euclid( self , rhs: Self ) -> ( Self , bool ) {
1848- if self == Self :: MIN && rhs == -1 {
1863+ if unlikely! ( self == Self :: MIN && rhs == -1 ) {
18491864 ( 0 , true )
18501865 } else {
18511866 ( self . rem_euclid( rhs) , false )
@@ -1876,7 +1891,7 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($Self
18761891 #[ allow( unused_attributes) ]
18771892 #[ cfg_attr( bootstrap, allow_internal_unstable( const_if_match) ) ]
18781893 pub const fn overflowing_neg( self ) -> ( Self , bool ) {
1879- if self == Self :: MIN {
1894+ if unlikely! ( self == Self :: MIN ) {
18801895 ( Self :: MIN , true )
18811896 } else {
18821897 ( -self , false )
@@ -2988,7 +3003,7 @@ assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(3), None);", $EndFeat
29883003 #[ inline]
29893004 pub const fn checked_add( self , rhs: Self ) -> Option <Self > {
29903005 let ( a, b) = self . overflowing_add( rhs) ;
2991- if b { None } else { Some ( a) }
3006+ if unlikely! ( b ) { None } else { Some ( a) }
29923007 }
29933008 }
29943009
@@ -3030,7 +3045,7 @@ assert_eq!(0", stringify!($SelfT), ".checked_sub(1), None);", $EndFeature, "
30303045 #[ inline]
30313046 pub const fn checked_sub( self , rhs: Self ) -> Option <Self > {
30323047 let ( a, b) = self . overflowing_sub( rhs) ;
3033- if b { None } else { Some ( a) }
3048+ if unlikely! ( b ) { None } else { Some ( a) }
30343049 }
30353050 }
30363051
@@ -3072,7 +3087,7 @@ assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(2), None);", $EndFeature, "
30723087 #[ inline]
30733088 pub const fn checked_mul( self , rhs: Self ) -> Option <Self > {
30743089 let ( a, b) = self . overflowing_mul( rhs) ;
3075- if b { None } else { Some ( a) }
3090+ if unlikely! ( b ) { None } else { Some ( a) }
30763091 }
30773092 }
30783093
@@ -3113,11 +3128,12 @@ assert_eq!(1", stringify!($SelfT), ".checked_div(0), None);", $EndFeature, "
31133128 without modifying the original"]
31143129 #[ inline]
31153130 pub const fn checked_div( self , rhs: Self ) -> Option <Self > {
3116- match rhs {
3117- 0 => None ,
3131+ if unlikely!( rhs == 0 ) {
3132+ None
3133+ } else {
31183134 // SAFETY: div by zero has been checked above and unsigned types have no other
31193135 // failure modes for division
3120- rhs => Some ( unsafe { intrinsics:: unchecked_div( self , rhs) } ) ,
3136+ Some ( unsafe { intrinsics:: unchecked_div( self , rhs) } )
31213137 }
31223138 }
31233139 }
@@ -3140,7 +3156,7 @@ assert_eq!(1", stringify!($SelfT), ".checked_div_euclid(0), None);
31403156 without modifying the original"]
31413157 #[ inline]
31423158 pub const fn checked_div_euclid( self , rhs: Self ) -> Option <Self > {
3143- if rhs == 0 {
3159+ if unlikely! ( rhs == 0 ) {
31443160 None
31453161 } else {
31463162 Some ( self . div_euclid( rhs) )
@@ -3167,7 +3183,7 @@ assert_eq!(5", stringify!($SelfT), ".checked_rem(0), None);", $EndFeature, "
31673183 without modifying the original"]
31683184 #[ inline]
31693185 pub const fn checked_rem( self , rhs: Self ) -> Option <Self > {
3170- if rhs == 0 {
3186+ if unlikely! ( rhs == 0 ) {
31713187 None
31723188 } else {
31733189 // SAFETY: div by zero has been checked above and unsigned types have no other
@@ -3195,7 +3211,7 @@ assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(0), None);
31953211 without modifying the original"]
31963212 #[ inline]
31973213 pub const fn checked_rem_euclid( self , rhs: Self ) -> Option <Self > {
3198- if rhs == 0 {
3214+ if unlikely! ( rhs == 0 ) {
31993215 None
32003216 } else {
32013217 Some ( self . rem_euclid( rhs) )
@@ -3222,7 +3238,7 @@ assert_eq!(1", stringify!($SelfT), ".checked_neg(), None);", $EndFeature, "
32223238 #[ inline]
32233239 pub const fn checked_neg( self ) -> Option <Self > {
32243240 let ( a, b) = self . overflowing_neg( ) ;
3225- if b { None } else { Some ( a) }
3241+ if unlikely! ( b ) { None } else { Some ( a) }
32263242 }
32273243 }
32283244
@@ -3245,7 +3261,7 @@ assert_eq!(0x10", stringify!($SelfT), ".checked_shl(129), None);", $EndFeature,
32453261 #[ inline]
32463262 pub const fn checked_shl( self , rhs: u32 ) -> Option <Self > {
32473263 let ( a, b) = self . overflowing_shl( rhs) ;
3248- if b { None } else { Some ( a) }
3264+ if unlikely! ( b ) { None } else { Some ( a) }
32493265 }
32503266 }
32513267
@@ -3268,7 +3284,7 @@ assert_eq!(0x10", stringify!($SelfT), ".checked_shr(129), None);", $EndFeature,
32683284 #[ inline]
32693285 pub const fn checked_shr( self , rhs: u32 ) -> Option <Self > {
32703286 let ( a, b) = self . overflowing_shr( rhs) ;
3271- if b { None } else { Some ( a) }
3287+ if unlikely! ( b ) { None } else { Some ( a) }
32723288 }
32733289 }
32743290
0 commit comments