@@ -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
$(
@@ -745,7 +760,7 @@ $EndFeature, "
745
760
#[ inline]
746
761
pub const fn checked_add( self , rhs: Self ) -> Option <Self > {
747
762
let ( a, b) = self . overflowing_add( rhs) ;
748
- if b { None } else { Some ( a) }
763
+ if unlikely! ( b ) { None } else { Some ( a) }
749
764
}
750
765
}
751
766
@@ -787,7 +802,7 @@ $EndFeature, "
787
802
#[ inline]
788
803
pub const fn checked_sub( self , rhs: Self ) -> Option <Self > {
789
804
let ( a, b) = self . overflowing_sub( rhs) ;
790
- if b { None } else { Some ( a) }
805
+ if unlikely! ( b ) { None } else { Some ( a) }
791
806
}
792
807
}
793
808
@@ -829,7 +844,7 @@ $EndFeature, "
829
844
#[ inline]
830
845
pub const fn checked_mul( self , rhs: Self ) -> Option <Self > {
831
846
let ( a, b) = self . overflowing_mul( rhs) ;
832
- if b { None } else { Some ( a) }
847
+ if unlikely! ( b ) { None } else { Some ( a) }
833
848
}
834
849
}
835
850
@@ -871,7 +886,7 @@ $EndFeature, "
871
886
without modifying the original"]
872
887
#[ inline]
873
888
pub const fn checked_div( self , rhs: Self ) -> Option <Self > {
874
- if rhs == 0 || ( self == Self :: MIN && rhs == -1 ) {
889
+ if unlikely! ( rhs == 0 || ( self == Self :: MIN && rhs == -1 ) ) {
875
890
None
876
891
} else {
877
892
// SAFETY: div by zero and by INT_MIN have been checked above
@@ -900,7 +915,7 @@ assert_eq!((1", stringify!($SelfT), ").checked_div_euclid(0), None);
900
915
without modifying the original"]
901
916
#[ inline]
902
917
pub const fn checked_div_euclid( self , rhs: Self ) -> Option <Self > {
903
- if rhs == 0 || ( self == Self :: MIN && rhs == -1 ) {
918
+ if unlikely! ( rhs == 0 || ( self == Self :: MIN && rhs == -1 ) ) {
904
919
None
905
920
} else {
906
921
Some ( self . div_euclid( rhs) )
@@ -929,7 +944,7 @@ $EndFeature, "
929
944
without modifying the original"]
930
945
#[ inline]
931
946
pub const fn checked_rem( self , rhs: Self ) -> Option <Self > {
932
- if rhs == 0 || ( self == Self :: MIN && rhs == -1 ) {
947
+ if unlikely! ( rhs == 0 || ( self == Self :: MIN && rhs == -1 ) ) {
933
948
None
934
949
} else {
935
950
// SAFETY: div by zero and by INT_MIN have been checked above
@@ -957,7 +972,7 @@ assert_eq!(", stringify!($SelfT), "::MIN.checked_rem_euclid(-1), None);
957
972
without modifying the original"]
958
973
#[ inline]
959
974
pub const fn checked_rem_euclid( self , rhs: Self ) -> Option <Self > {
960
- if rhs == 0 || ( self == Self :: MIN && rhs == -1 ) {
975
+ if unlikely! ( rhs == 0 || ( self == Self :: MIN && rhs == -1 ) ) {
961
976
None
962
977
} else {
963
978
Some ( self . rem_euclid( rhs) )
@@ -983,7 +998,7 @@ $EndFeature, "
983
998
#[ inline]
984
999
pub const fn checked_neg( self ) -> Option <Self > {
985
1000
let ( a, b) = self . overflowing_neg( ) ;
986
- if b { None } else { Some ( a) }
1001
+ if unlikely! ( b ) { None } else { Some ( a) }
987
1002
}
988
1003
}
989
1004
@@ -1007,7 +1022,7 @@ $EndFeature, "
1007
1022
#[ inline]
1008
1023
pub const fn checked_shl( self , rhs: u32 ) -> Option <Self > {
1009
1024
let ( a, b) = self . overflowing_shl( rhs) ;
1010
- if b { None } else { Some ( a) }
1025
+ if unlikely! ( b ) { None } else { Some ( a) }
1011
1026
}
1012
1027
}
1013
1028
@@ -1031,7 +1046,7 @@ $EndFeature, "
1031
1046
#[ inline]
1032
1047
pub const fn checked_shr( self , rhs: u32 ) -> Option <Self > {
1033
1048
let ( a, b) = self . overflowing_shr( rhs) ;
1034
- if b { None } else { Some ( a) }
1049
+ if unlikely! ( b ) { None } else { Some ( a) }
1035
1050
}
1036
1051
}
1037
1052
@@ -1738,7 +1753,7 @@ $EndFeature, "
1738
1753
#[ must_use = "this returns the result of the operation, \
1739
1754
without modifying the original"]
1740
1755
pub const fn overflowing_div( self , rhs: Self ) -> ( Self , bool ) {
1741
- if self == Self :: MIN && rhs == -1 {
1756
+ if unlikely! ( self == Self :: MIN && rhs == -1 ) {
1742
1757
( self , true )
1743
1758
} else {
1744
1759
( self / rhs, false )
@@ -1771,7 +1786,7 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div_euclid(-1), (", stringi
1771
1786
#[ must_use = "this returns the result of the operation, \
1772
1787
without modifying the original"]
1773
1788
pub const fn overflowing_div_euclid( self , rhs: Self ) -> ( Self , bool ) {
1774
- if self == Self :: MIN && rhs == -1 {
1789
+ if unlikely! ( self == Self :: MIN && rhs == -1 ) {
1775
1790
( self , true )
1776
1791
} else {
1777
1792
( self . div_euclid( rhs) , false )
@@ -1805,7 +1820,7 @@ $EndFeature, "
1805
1820
#[ must_use = "this returns the result of the operation, \
1806
1821
without modifying the original"]
1807
1822
pub const fn overflowing_rem( self , rhs: Self ) -> ( Self , bool ) {
1808
- if self == Self :: MIN && rhs == -1 {
1823
+ if unlikely! ( self == Self :: MIN && rhs == -1 ) {
1809
1824
( 0 , true )
1810
1825
} else {
1811
1826
( self % rhs, false )
@@ -1838,7 +1853,7 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem_euclid(-1), (0, true));
1838
1853
without modifying the original"]
1839
1854
#[ inline]
1840
1855
pub const fn overflowing_rem_euclid( self , rhs: Self ) -> ( Self , bool ) {
1841
- if self == Self :: MIN && rhs == -1 {
1856
+ if unlikely! ( self == Self :: MIN && rhs == -1 ) {
1842
1857
( 0 , true )
1843
1858
} else {
1844
1859
( self . rem_euclid( rhs) , false )
@@ -1869,7 +1884,7 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($Self
1869
1884
#[ allow( unused_attributes) ]
1870
1885
#[ cfg_attr( bootstrap, allow_internal_unstable( const_if_match) ) ]
1871
1886
pub const fn overflowing_neg( self ) -> ( Self , bool ) {
1872
- if self == Self :: MIN {
1887
+ if unlikely! ( self == Self :: MIN ) {
1873
1888
( Self :: MIN , true )
1874
1889
} else {
1875
1890
( -self , false )
@@ -2981,7 +2996,7 @@ assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(3), None);", $EndFeat
2981
2996
#[ inline]
2982
2997
pub const fn checked_add( self , rhs: Self ) -> Option <Self > {
2983
2998
let ( a, b) = self . overflowing_add( rhs) ;
2984
- if b { None } else { Some ( a) }
2999
+ if unlikely! ( b ) { None } else { Some ( a) }
2985
3000
}
2986
3001
}
2987
3002
@@ -3021,7 +3036,7 @@ assert_eq!(0", stringify!($SelfT), ".checked_sub(1), None);", $EndFeature, "
3021
3036
#[ inline]
3022
3037
pub const fn checked_sub( self , rhs: Self ) -> Option <Self > {
3023
3038
let ( a, b) = self . overflowing_sub( rhs) ;
3024
- if b { None } else { Some ( a) }
3039
+ if unlikely! ( b ) { None } else { Some ( a) }
3025
3040
}
3026
3041
}
3027
3042
@@ -3061,7 +3076,7 @@ assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(2), None);", $EndFeature, "
3061
3076
#[ inline]
3062
3077
pub const fn checked_mul( self , rhs: Self ) -> Option <Self > {
3063
3078
let ( a, b) = self . overflowing_mul( rhs) ;
3064
- if b { None } else { Some ( a) }
3079
+ if unlikely! ( b ) { None } else { Some ( a) }
3065
3080
}
3066
3081
}
3067
3082
@@ -3100,11 +3115,12 @@ assert_eq!(1", stringify!($SelfT), ".checked_div(0), None);", $EndFeature, "
3100
3115
without modifying the original"]
3101
3116
#[ inline]
3102
3117
pub const fn checked_div( self , rhs: Self ) -> Option <Self > {
3103
- match rhs {
3104
- 0 => None ,
3118
+ if unlikely!( rhs == 0 ) {
3119
+ None
3120
+ } else {
3105
3121
// SAFETY: div by zero has been checked above and unsigned types have no other
3106
3122
// failure modes for division
3107
- rhs => Some ( unsafe { intrinsics:: unchecked_div( self , rhs) } ) ,
3123
+ Some ( unsafe { intrinsics:: unchecked_div( self , rhs) } )
3108
3124
}
3109
3125
}
3110
3126
}
@@ -3127,7 +3143,7 @@ assert_eq!(1", stringify!($SelfT), ".checked_div_euclid(0), None);
3127
3143
without modifying the original"]
3128
3144
#[ inline]
3129
3145
pub const fn checked_div_euclid( self , rhs: Self ) -> Option <Self > {
3130
- if rhs == 0 {
3146
+ if unlikely! ( rhs == 0 ) {
3131
3147
None
3132
3148
} else {
3133
3149
Some ( self . div_euclid( rhs) )
@@ -3154,7 +3170,7 @@ assert_eq!(5", stringify!($SelfT), ".checked_rem(0), None);", $EndFeature, "
3154
3170
without modifying the original"]
3155
3171
#[ inline]
3156
3172
pub const fn checked_rem( self , rhs: Self ) -> Option <Self > {
3157
- if rhs == 0 {
3173
+ if unlikely! ( rhs == 0 ) {
3158
3174
None
3159
3175
} else {
3160
3176
// SAFETY: div by zero has been checked above and unsigned types have no other
@@ -3182,7 +3198,7 @@ assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(0), None);
3182
3198
without modifying the original"]
3183
3199
#[ inline]
3184
3200
pub const fn checked_rem_euclid( self , rhs: Self ) -> Option <Self > {
3185
- if rhs == 0 {
3201
+ if unlikely! ( rhs == 0 ) {
3186
3202
None
3187
3203
} else {
3188
3204
Some ( self . rem_euclid( rhs) )
@@ -3209,7 +3225,7 @@ assert_eq!(1", stringify!($SelfT), ".checked_neg(), None);", $EndFeature, "
3209
3225
#[ inline]
3210
3226
pub const fn checked_neg( self ) -> Option <Self > {
3211
3227
let ( a, b) = self . overflowing_neg( ) ;
3212
- if b { None } else { Some ( a) }
3228
+ if unlikely! ( b ) { None } else { Some ( a) }
3213
3229
}
3214
3230
}
3215
3231
@@ -3232,7 +3248,7 @@ assert_eq!(0x10", stringify!($SelfT), ".checked_shl(129), None);", $EndFeature,
3232
3248
#[ inline]
3233
3249
pub const fn checked_shl( self , rhs: u32 ) -> Option <Self > {
3234
3250
let ( a, b) = self . overflowing_shl( rhs) ;
3235
- if b { None } else { Some ( a) }
3251
+ if unlikely! ( b ) { None } else { Some ( a) }
3236
3252
}
3237
3253
}
3238
3254
@@ -3255,7 +3271,7 @@ assert_eq!(0x10", stringify!($SelfT), ".checked_shr(129), None);", $EndFeature,
3255
3271
#[ inline]
3256
3272
pub const fn checked_shr( self , rhs: u32 ) -> Option <Self > {
3257
3273
let ( a, b) = self . overflowing_shr( rhs) ;
3258
- if b { None } else { Some ( a) }
3274
+ if unlikely! ( b ) { None } else { Some ( a) }
3259
3275
}
3260
3276
}
3261
3277
0 commit comments