Skip to content

Commit 86d8644

Browse files
committed
Optimise fast path of checked_ops with unlikely
1 parent f781bab commit 86d8644

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

src/libcore/num/mod.rs

+43-27
Original file line numberDiff line numberDiff line change
@@ -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+
2439
macro_rules! impl_nonzero_fmt {
2540
( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
2641
$(
@@ -745,7 +760,7 @@ $EndFeature, "
745760
#[inline]
746761
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
747762
let (a, b) = self.overflowing_add(rhs);
748-
if b {None} else {Some(a)}
763+
if unlikely!(b) {None} else {Some(a)}
749764
}
750765
}
751766

@@ -787,7 +802,7 @@ $EndFeature, "
787802
#[inline]
788803
pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
789804
let (a, b) = self.overflowing_sub(rhs);
790-
if b {None} else {Some(a)}
805+
if unlikely!(b) {None} else {Some(a)}
791806
}
792807
}
793808

@@ -829,7 +844,7 @@ $EndFeature, "
829844
#[inline]
830845
pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
831846
let (a, b) = self.overflowing_mul(rhs);
832-
if b {None} else {Some(a)}
847+
if unlikely!(b) {None} else {Some(a)}
833848
}
834849
}
835850

@@ -871,7 +886,7 @@ $EndFeature, "
871886
without modifying the original"]
872887
#[inline]
873888
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)) {
875890
None
876891
} else {
877892
// 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);
900915
without modifying the original"]
901916
#[inline]
902917
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)) {
904919
None
905920
} else {
906921
Some(self.div_euclid(rhs))
@@ -929,7 +944,7 @@ $EndFeature, "
929944
without modifying the original"]
930945
#[inline]
931946
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)) {
933948
None
934949
} else {
935950
// 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);
957972
without modifying the original"]
958973
#[inline]
959974
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)) {
961976
None
962977
} else {
963978
Some(self.rem_euclid(rhs))
@@ -983,7 +998,7 @@ $EndFeature, "
983998
#[inline]
984999
pub const fn checked_neg(self) -> Option<Self> {
9851000
let (a, b) = self.overflowing_neg();
986-
if b {None} else {Some(a)}
1001+
if unlikely!(b) {None} else {Some(a)}
9871002
}
9881003
}
9891004

@@ -1007,7 +1022,7 @@ $EndFeature, "
10071022
#[inline]
10081023
pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
10091024
let (a, b) = self.overflowing_shl(rhs);
1010-
if b {None} else {Some(a)}
1025+
if unlikely!(b) {None} else {Some(a)}
10111026
}
10121027
}
10131028

@@ -1031,7 +1046,7 @@ $EndFeature, "
10311046
#[inline]
10321047
pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
10331048
let (a, b) = self.overflowing_shr(rhs);
1034-
if b {None} else {Some(a)}
1049+
if unlikely!(b) {None} else {Some(a)}
10351050
}
10361051
}
10371052

@@ -1738,7 +1753,7 @@ $EndFeature, "
17381753
#[must_use = "this returns the result of the operation, \
17391754
without modifying the original"]
17401755
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) {
17421757
(self, true)
17431758
} else {
17441759
(self / rhs, false)
@@ -1771,7 +1786,7 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div_euclid(-1), (", stringi
17711786
#[must_use = "this returns the result of the operation, \
17721787
without modifying the original"]
17731788
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) {
17751790
(self, true)
17761791
} else {
17771792
(self.div_euclid(rhs), false)
@@ -1805,7 +1820,7 @@ $EndFeature, "
18051820
#[must_use = "this returns the result of the operation, \
18061821
without modifying the original"]
18071822
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) {
18091824
(0, true)
18101825
} else {
18111826
(self % rhs, false)
@@ -1838,7 +1853,7 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem_euclid(-1), (0, true));
18381853
without modifying the original"]
18391854
#[inline]
18401855
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) {
18421857
(0, true)
18431858
} else {
18441859
(self.rem_euclid(rhs), false)
@@ -1869,7 +1884,7 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($Self
18691884
#[allow(unused_attributes)]
18701885
#[cfg_attr(bootstrap, allow_internal_unstable(const_if_match))]
18711886
pub const fn overflowing_neg(self) -> (Self, bool) {
1872-
if self == Self::MIN {
1887+
if unlikely!(self == Self::MIN) {
18731888
(Self::MIN, true)
18741889
} else {
18751890
(-self, false)
@@ -2981,7 +2996,7 @@ assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(3), None);", $EndFeat
29812996
#[inline]
29822997
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
29832998
let (a, b) = self.overflowing_add(rhs);
2984-
if b {None} else {Some(a)}
2999+
if unlikely!(b) {None} else {Some(a)}
29853000
}
29863001
}
29873002

@@ -3021,7 +3036,7 @@ assert_eq!(0", stringify!($SelfT), ".checked_sub(1), None);", $EndFeature, "
30213036
#[inline]
30223037
pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
30233038
let (a, b) = self.overflowing_sub(rhs);
3024-
if b {None} else {Some(a)}
3039+
if unlikely!(b) {None} else {Some(a)}
30253040
}
30263041
}
30273042

@@ -3061,7 +3076,7 @@ assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(2), None);", $EndFeature, "
30613076
#[inline]
30623077
pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
30633078
let (a, b) = self.overflowing_mul(rhs);
3064-
if b {None} else {Some(a)}
3079+
if unlikely!(b) {None} else {Some(a)}
30653080
}
30663081
}
30673082

@@ -3100,11 +3115,12 @@ assert_eq!(1", stringify!($SelfT), ".checked_div(0), None);", $EndFeature, "
31003115
without modifying the original"]
31013116
#[inline]
31023117
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 {
31053121
// SAFETY: div by zero has been checked above and unsigned types have no other
31063122
// failure modes for division
3107-
rhs => Some(unsafe { intrinsics::unchecked_div(self, rhs) }),
3123+
Some(unsafe { intrinsics::unchecked_div(self, rhs) })
31083124
}
31093125
}
31103126
}
@@ -3127,7 +3143,7 @@ assert_eq!(1", stringify!($SelfT), ".checked_div_euclid(0), None);
31273143
without modifying the original"]
31283144
#[inline]
31293145
pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
3130-
if rhs == 0 {
3146+
if unlikely!(rhs == 0) {
31313147
None
31323148
} else {
31333149
Some(self.div_euclid(rhs))
@@ -3154,7 +3170,7 @@ assert_eq!(5", stringify!($SelfT), ".checked_rem(0), None);", $EndFeature, "
31543170
without modifying the original"]
31553171
#[inline]
31563172
pub const fn checked_rem(self, rhs: Self) -> Option<Self> {
3157-
if rhs == 0 {
3173+
if unlikely!(rhs == 0) {
31583174
None
31593175
} else {
31603176
// 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);
31823198
without modifying the original"]
31833199
#[inline]
31843200
pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
3185-
if rhs == 0 {
3201+
if unlikely!(rhs == 0) {
31863202
None
31873203
} else {
31883204
Some(self.rem_euclid(rhs))
@@ -3209,7 +3225,7 @@ assert_eq!(1", stringify!($SelfT), ".checked_neg(), None);", $EndFeature, "
32093225
#[inline]
32103226
pub const fn checked_neg(self) -> Option<Self> {
32113227
let (a, b) = self.overflowing_neg();
3212-
if b {None} else {Some(a)}
3228+
if unlikely!(b) {None} else {Some(a)}
32133229
}
32143230
}
32153231

@@ -3232,7 +3248,7 @@ assert_eq!(0x10", stringify!($SelfT), ".checked_shl(129), None);", $EndFeature,
32323248
#[inline]
32333249
pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
32343250
let (a, b) = self.overflowing_shl(rhs);
3235-
if b {None} else {Some(a)}
3251+
if unlikely!(b) {None} else {Some(a)}
32363252
}
32373253
}
32383254

@@ -3255,7 +3271,7 @@ assert_eq!(0x10", stringify!($SelfT), ".checked_shr(129), None);", $EndFeature,
32553271
#[inline]
32563272
pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
32573273
let (a, b) = self.overflowing_shr(rhs);
3258-
if b {None} else {Some(a)}
3274+
if unlikely!(b) {None} else {Some(a)}
32593275
}
32603276
}
32613277

0 commit comments

Comments
 (0)