@@ -21,6 +21,21 @@ macro_rules! try_opt {
21
21
} ;
22
22
}
23
23
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
+
24
39
macro_rules! impl_nonzero_fmt {
25
40
( #[ $stability: meta] ( $( $Trait: ident ) ,+ ) for $Ty: ident ) => {
26
41
$(
@@ -746,7 +761,7 @@ $EndFeature, "
746
761
#[ inline]
747
762
pub const fn checked_add( self , rhs: Self ) -> Option <Self > {
748
763
let ( a, b) = self . overflowing_add( rhs) ;
749
- if b { None } else { Some ( a) }
764
+ if unlikely! ( b ) { None } else { Some ( a) }
750
765
}
751
766
}
752
767
@@ -790,7 +805,7 @@ $EndFeature, "
790
805
#[ inline]
791
806
pub const fn checked_sub( self , rhs: Self ) -> Option <Self > {
792
807
let ( a, b) = self . overflowing_sub( rhs) ;
793
- if b { None } else { Some ( a) }
808
+ if unlikely! ( b ) { None } else { Some ( a) }
794
809
}
795
810
}
796
811
@@ -834,7 +849,7 @@ $EndFeature, "
834
849
#[ inline]
835
850
pub const fn checked_mul( self , rhs: Self ) -> Option <Self > {
836
851
let ( a, b) = self . overflowing_mul( rhs) ;
837
- if b { None } else { Some ( a) }
852
+ if unlikely! ( b ) { None } else { Some ( a) }
838
853
}
839
854
}
840
855
@@ -878,7 +893,7 @@ $EndFeature, "
878
893
without modifying the original"]
879
894
#[ inline]
880
895
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 ) ) {
882
897
None
883
898
} else {
884
899
// 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);
907
922
without modifying the original"]
908
923
#[ inline]
909
924
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 ) ) {
911
926
None
912
927
} else {
913
928
Some ( self . div_euclid( rhs) )
@@ -936,7 +951,7 @@ $EndFeature, "
936
951
without modifying the original"]
937
952
#[ inline]
938
953
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 ) ) {
940
955
None
941
956
} else {
942
957
// 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);
964
979
without modifying the original"]
965
980
#[ inline]
966
981
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 ) ) {
968
983
None
969
984
} else {
970
985
Some ( self . rem_euclid( rhs) )
@@ -990,7 +1005,7 @@ $EndFeature, "
990
1005
#[ inline]
991
1006
pub const fn checked_neg( self ) -> Option <Self > {
992
1007
let ( a, b) = self . overflowing_neg( ) ;
993
- if b { None } else { Some ( a) }
1008
+ if unlikely! ( b ) { None } else { Some ( a) }
994
1009
}
995
1010
}
996
1011
@@ -1014,7 +1029,7 @@ $EndFeature, "
1014
1029
#[ inline]
1015
1030
pub const fn checked_shl( self , rhs: u32 ) -> Option <Self > {
1016
1031
let ( a, b) = self . overflowing_shl( rhs) ;
1017
- if b { None } else { Some ( a) }
1032
+ if unlikely! ( b ) { None } else { Some ( a) }
1018
1033
}
1019
1034
}
1020
1035
@@ -1038,7 +1053,7 @@ $EndFeature, "
1038
1053
#[ inline]
1039
1054
pub const fn checked_shr( self , rhs: u32 ) -> Option <Self > {
1040
1055
let ( a, b) = self . overflowing_shr( rhs) ;
1041
- if b { None } else { Some ( a) }
1056
+ if unlikely! ( b ) { None } else { Some ( a) }
1042
1057
}
1043
1058
}
1044
1059
@@ -1745,7 +1760,7 @@ $EndFeature, "
1745
1760
#[ must_use = "this returns the result of the operation, \
1746
1761
without modifying the original"]
1747
1762
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 ) {
1749
1764
( self , true )
1750
1765
} else {
1751
1766
( self / rhs, false )
@@ -1778,7 +1793,7 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div_euclid(-1), (", stringi
1778
1793
#[ must_use = "this returns the result of the operation, \
1779
1794
without modifying the original"]
1780
1795
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 ) {
1782
1797
( self , true )
1783
1798
} else {
1784
1799
( self . div_euclid( rhs) , false )
@@ -1812,7 +1827,7 @@ $EndFeature, "
1812
1827
#[ must_use = "this returns the result of the operation, \
1813
1828
without modifying the original"]
1814
1829
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 ) {
1816
1831
( 0 , true )
1817
1832
} else {
1818
1833
( self % rhs, false )
@@ -1845,7 +1860,7 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem_euclid(-1), (0, true));
1845
1860
without modifying the original"]
1846
1861
#[ inline]
1847
1862
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 ) {
1849
1864
( 0 , true )
1850
1865
} else {
1851
1866
( self . rem_euclid( rhs) , false )
@@ -1876,7 +1891,7 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($Self
1876
1891
#[ allow( unused_attributes) ]
1877
1892
#[ cfg_attr( bootstrap, allow_internal_unstable( const_if_match) ) ]
1878
1893
pub const fn overflowing_neg( self ) -> ( Self , bool ) {
1879
- if self == Self :: MIN {
1894
+ if unlikely! ( self == Self :: MIN ) {
1880
1895
( Self :: MIN , true )
1881
1896
} else {
1882
1897
( -self , false )
@@ -2988,7 +3003,7 @@ assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(3), None);", $EndFeat
2988
3003
#[ inline]
2989
3004
pub const fn checked_add( self , rhs: Self ) -> Option <Self > {
2990
3005
let ( a, b) = self . overflowing_add( rhs) ;
2991
- if b { None } else { Some ( a) }
3006
+ if unlikely! ( b ) { None } else { Some ( a) }
2992
3007
}
2993
3008
}
2994
3009
@@ -3030,7 +3045,7 @@ assert_eq!(0", stringify!($SelfT), ".checked_sub(1), None);", $EndFeature, "
3030
3045
#[ inline]
3031
3046
pub const fn checked_sub( self , rhs: Self ) -> Option <Self > {
3032
3047
let ( a, b) = self . overflowing_sub( rhs) ;
3033
- if b { None } else { Some ( a) }
3048
+ if unlikely! ( b ) { None } else { Some ( a) }
3034
3049
}
3035
3050
}
3036
3051
@@ -3072,7 +3087,7 @@ assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(2), None);", $EndFeature, "
3072
3087
#[ inline]
3073
3088
pub const fn checked_mul( self , rhs: Self ) -> Option <Self > {
3074
3089
let ( a, b) = self . overflowing_mul( rhs) ;
3075
- if b { None } else { Some ( a) }
3090
+ if unlikely! ( b ) { None } else { Some ( a) }
3076
3091
}
3077
3092
}
3078
3093
@@ -3113,11 +3128,12 @@ assert_eq!(1", stringify!($SelfT), ".checked_div(0), None);", $EndFeature, "
3113
3128
without modifying the original"]
3114
3129
#[ inline]
3115
3130
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 {
3118
3134
// SAFETY: div by zero has been checked above and unsigned types have no other
3119
3135
// failure modes for division
3120
- rhs => Some ( unsafe { intrinsics:: unchecked_div( self , rhs) } ) ,
3136
+ Some ( unsafe { intrinsics:: unchecked_div( self , rhs) } )
3121
3137
}
3122
3138
}
3123
3139
}
@@ -3140,7 +3156,7 @@ assert_eq!(1", stringify!($SelfT), ".checked_div_euclid(0), None);
3140
3156
without modifying the original"]
3141
3157
#[ inline]
3142
3158
pub const fn checked_div_euclid( self , rhs: Self ) -> Option <Self > {
3143
- if rhs == 0 {
3159
+ if unlikely! ( rhs == 0 ) {
3144
3160
None
3145
3161
} else {
3146
3162
Some ( self . div_euclid( rhs) )
@@ -3167,7 +3183,7 @@ assert_eq!(5", stringify!($SelfT), ".checked_rem(0), None);", $EndFeature, "
3167
3183
without modifying the original"]
3168
3184
#[ inline]
3169
3185
pub const fn checked_rem( self , rhs: Self ) -> Option <Self > {
3170
- if rhs == 0 {
3186
+ if unlikely! ( rhs == 0 ) {
3171
3187
None
3172
3188
} else {
3173
3189
// 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);
3195
3211
without modifying the original"]
3196
3212
#[ inline]
3197
3213
pub const fn checked_rem_euclid( self , rhs: Self ) -> Option <Self > {
3198
- if rhs == 0 {
3214
+ if unlikely! ( rhs == 0 ) {
3199
3215
None
3200
3216
} else {
3201
3217
Some ( self . rem_euclid( rhs) )
@@ -3222,7 +3238,7 @@ assert_eq!(1", stringify!($SelfT), ".checked_neg(), None);", $EndFeature, "
3222
3238
#[ inline]
3223
3239
pub const fn checked_neg( self ) -> Option <Self > {
3224
3240
let ( a, b) = self . overflowing_neg( ) ;
3225
- if b { None } else { Some ( a) }
3241
+ if unlikely! ( b ) { None } else { Some ( a) }
3226
3242
}
3227
3243
}
3228
3244
@@ -3245,7 +3261,7 @@ assert_eq!(0x10", stringify!($SelfT), ".checked_shl(129), None);", $EndFeature,
3245
3261
#[ inline]
3246
3262
pub const fn checked_shl( self , rhs: u32 ) -> Option <Self > {
3247
3263
let ( a, b) = self . overflowing_shl( rhs) ;
3248
- if b { None } else { Some ( a) }
3264
+ if unlikely! ( b ) { None } else { Some ( a) }
3249
3265
}
3250
3266
}
3251
3267
@@ -3268,7 +3284,7 @@ assert_eq!(0x10", stringify!($SelfT), ".checked_shr(129), None);", $EndFeature,
3268
3284
#[ inline]
3269
3285
pub const fn checked_shr( self , rhs: u32 ) -> Option <Self > {
3270
3286
let ( a, b) = self . overflowing_shr( rhs) ;
3271
- if b { None } else { Some ( a) }
3287
+ if unlikely! ( b ) { None } else { Some ( a) }
3272
3288
}
3273
3289
}
3274
3290
0 commit comments