Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 776b56e

Browse files
committedJun 20, 2024·
Stop using unlikely in core::num
1 parent 1ca578e commit 776b56e

File tree

3 files changed

+42
-49
lines changed

3 files changed

+42
-49
lines changed
 

‎library/core/src/num/int_macros.rs

+27-27
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ macro_rules! int_impl {
449449
#[inline]
450450
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
451451
let (a, b) = self.overflowing_add(rhs);
452-
if unlikely!(b) { None } else { Some(a) }
452+
if b { None } else { Some(a) }
453453
}
454454

455455
/// Strict integer addition. Computes `self + rhs`, panicking
@@ -484,7 +484,7 @@ macro_rules! int_impl {
484484
#[track_caller]
485485
pub const fn strict_add(self, rhs: Self) -> Self {
486486
let (a, b) = self.overflowing_add(rhs);
487-
if unlikely!(b) { overflow_panic::add() } else { a }
487+
if b { overflow_panic::add() } else { a }
488488
}
489489

490490
/// Unchecked integer addition. Computes `self + rhs`, assuming overflow
@@ -545,7 +545,7 @@ macro_rules! int_impl {
545545
#[inline]
546546
pub const fn checked_add_unsigned(self, rhs: $UnsignedT) -> Option<Self> {
547547
let (a, b) = self.overflowing_add_unsigned(rhs);
548-
if unlikely!(b) { None } else { Some(a) }
548+
if b { None } else { Some(a) }
549549
}
550550

551551
/// Strict addition with an unsigned integer. Computes `self + rhs`,
@@ -580,7 +580,7 @@ macro_rules! int_impl {
580580
#[track_caller]
581581
pub const fn strict_add_unsigned(self, rhs: $UnsignedT) -> Self {
582582
let (a, b) = self.overflowing_add_unsigned(rhs);
583-
if unlikely!(b) { overflow_panic::add() } else { a }
583+
if b { overflow_panic::add() } else { a }
584584
}
585585

586586
/// Checked integer subtraction. Computes `self - rhs`, returning `None` if
@@ -601,7 +601,7 @@ macro_rules! int_impl {
601601
#[inline]
602602
pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
603603
let (a, b) = self.overflowing_sub(rhs);
604-
if unlikely!(b) { None } else { Some(a) }
604+
if b { None } else { Some(a) }
605605
}
606606

607607
/// Strict integer subtraction. Computes `self - rhs`, panicking if
@@ -636,7 +636,7 @@ macro_rules! int_impl {
636636
#[track_caller]
637637
pub const fn strict_sub(self, rhs: Self) -> Self {
638638
let (a, b) = self.overflowing_sub(rhs);
639-
if unlikely!(b) { overflow_panic::sub() } else { a }
639+
if b { overflow_panic::sub() } else { a }
640640
}
641641

642642
/// Unchecked integer subtraction. Computes `self - rhs`, assuming overflow
@@ -697,7 +697,7 @@ macro_rules! int_impl {
697697
#[inline]
698698
pub const fn checked_sub_unsigned(self, rhs: $UnsignedT) -> Option<Self> {
699699
let (a, b) = self.overflowing_sub_unsigned(rhs);
700-
if unlikely!(b) { None } else { Some(a) }
700+
if b { None } else { Some(a) }
701701
}
702702

703703
/// Strict subtraction with an unsigned integer. Computes `self - rhs`,
@@ -732,7 +732,7 @@ macro_rules! int_impl {
732732
#[track_caller]
733733
pub const fn strict_sub_unsigned(self, rhs: $UnsignedT) -> Self {
734734
let (a, b) = self.overflowing_sub_unsigned(rhs);
735-
if unlikely!(b) { overflow_panic::sub() } else { a }
735+
if b { overflow_panic::sub() } else { a }
736736
}
737737

738738
/// Checked integer multiplication. Computes `self * rhs`, returning `None` if
@@ -753,7 +753,7 @@ macro_rules! int_impl {
753753
#[inline]
754754
pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
755755
let (a, b) = self.overflowing_mul(rhs);
756-
if unlikely!(b) { None } else { Some(a) }
756+
if b { None } else { Some(a) }
757757
}
758758

759759
/// Strict integer multiplication. Computes `self * rhs`, panicking if
@@ -788,7 +788,7 @@ macro_rules! int_impl {
788788
#[track_caller]
789789
pub const fn strict_mul(self, rhs: Self) -> Self {
790790
let (a, b) = self.overflowing_mul(rhs);
791-
if unlikely!(b) { overflow_panic::mul() } else { a }
791+
if b { overflow_panic::mul() } else { a }
792792
}
793793

794794
/// Unchecked integer multiplication. Computes `self * rhs`, assuming overflow
@@ -849,7 +849,7 @@ macro_rules! int_impl {
849849
without modifying the original"]
850850
#[inline]
851851
pub const fn checked_div(self, rhs: Self) -> Option<Self> {
852-
if unlikely!(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
852+
if rhs == 0 || ((self == Self::MIN) && (rhs == -1)) {
853853
None
854854
} else {
855855
// SAFETY: div by zero and by INT_MIN have been checked above
@@ -902,7 +902,7 @@ macro_rules! int_impl {
902902
#[track_caller]
903903
pub const fn strict_div(self, rhs: Self) -> Self {
904904
let (a, b) = self.overflowing_div(rhs);
905-
if unlikely!(b) { overflow_panic::div() } else { a }
905+
if b { overflow_panic::div() } else { a }
906906
}
907907

908908
/// Checked Euclidean division. Computes `self.div_euclid(rhs)`,
@@ -924,7 +924,7 @@ macro_rules! int_impl {
924924
#[inline]
925925
pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
926926
// Using `&` helps LLVM see that it is the same check made in division.
927-
if unlikely!(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
927+
if rhs == 0 || ((self == Self::MIN) & (rhs == -1)) {
928928
None
929929
} else {
930930
Some(self.div_euclid(rhs))
@@ -976,7 +976,7 @@ macro_rules! int_impl {
976976
#[track_caller]
977977
pub const fn strict_div_euclid(self, rhs: Self) -> Self {
978978
let (a, b) = self.overflowing_div_euclid(rhs);
979-
if unlikely!(b) { overflow_panic::div() } else { a }
979+
if b { overflow_panic::div() } else { a }
980980
}
981981

982982
/// Checked integer remainder. Computes `self % rhs`, returning `None` if
@@ -997,7 +997,7 @@ macro_rules! int_impl {
997997
without modifying the original"]
998998
#[inline]
999999
pub const fn checked_rem(self, rhs: Self) -> Option<Self> {
1000-
if unlikely!(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
1000+
if rhs == 0 || ((self == Self::MIN) && (rhs == -1)) {
10011001
None
10021002
} else {
10031003
// SAFETY: div by zero and by INT_MIN have been checked above
@@ -1049,7 +1049,7 @@ macro_rules! int_impl {
10491049
#[track_caller]
10501050
pub const fn strict_rem(self, rhs: Self) -> Self {
10511051
let (a, b) = self.overflowing_rem(rhs);
1052-
if unlikely!(b) { overflow_panic::rem() } else { a }
1052+
if b { overflow_panic::rem() } else { a }
10531053
}
10541054

10551055
/// Checked Euclidean remainder. Computes `self.rem_euclid(rhs)`, returning `None`
@@ -1071,7 +1071,7 @@ macro_rules! int_impl {
10711071
#[inline]
10721072
pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
10731073
// Using `&` helps LLVM see that it is the same check made in division.
1074-
if unlikely!(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
1074+
if rhs == 0 || ((self == Self::MIN) & (rhs == -1)) {
10751075
None
10761076
} else {
10771077
Some(self.rem_euclid(rhs))
@@ -1122,7 +1122,7 @@ macro_rules! int_impl {
11221122
#[track_caller]
11231123
pub const fn strict_rem_euclid(self, rhs: Self) -> Self {
11241124
let (a, b) = self.overflowing_rem_euclid(rhs);
1125-
if unlikely!(b) { overflow_panic::rem() } else { a }
1125+
if b { overflow_panic::rem() } else { a }
11261126
}
11271127

11281128
/// Checked negation. Computes `-self`, returning `None` if `self == MIN`.
@@ -1142,7 +1142,7 @@ macro_rules! int_impl {
11421142
#[inline]
11431143
pub const fn checked_neg(self) -> Option<Self> {
11441144
let (a, b) = self.overflowing_neg();
1145-
if unlikely!(b) { None } else { Some(a) }
1145+
if b { None } else { Some(a) }
11461146
}
11471147

11481148
/// Unchecked negation. Computes `-self`, assuming overflow cannot occur.
@@ -1210,7 +1210,7 @@ macro_rules! int_impl {
12101210
#[track_caller]
12111211
pub const fn strict_neg(self) -> Self {
12121212
let (a, b) = self.overflowing_neg();
1213-
if unlikely!(b) { overflow_panic::neg() } else { a }
1213+
if b { overflow_panic::neg() } else { a }
12141214
}
12151215

12161216
/// Checked shift left. Computes `self << rhs`, returning `None` if `rhs` is larger
@@ -1273,7 +1273,7 @@ macro_rules! int_impl {
12731273
#[track_caller]
12741274
pub const fn strict_shl(self, rhs: u32) -> Self {
12751275
let (a, b) = self.overflowing_shl(rhs);
1276-
if unlikely!(b) { overflow_panic::shl() } else { a }
1276+
if b { overflow_panic::shl() } else { a }
12771277
}
12781278

12791279
/// Unchecked shift left. Computes `self << rhs`, assuming that
@@ -1371,7 +1371,7 @@ macro_rules! int_impl {
13711371
#[track_caller]
13721372
pub const fn strict_shr(self, rhs: u32) -> Self {
13731373
let (a, b) = self.overflowing_shr(rhs);
1374-
if unlikely!(b) { overflow_panic::shr() } else { a }
1374+
if b { overflow_panic::shr() } else { a }
13751375
}
13761376

13771377
/// Unchecked shift right. Computes `self >> rhs`, assuming that
@@ -2458,7 +2458,7 @@ macro_rules! int_impl {
24582458
without modifying the original"]
24592459
pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
24602460
// Using `&` helps LLVM see that it is the same check made in division.
2461-
if unlikely!((self == Self::MIN) & (rhs == -1)) {
2461+
if (self == Self::MIN) & (rhs == -1) {
24622462
(self, true)
24632463
} else {
24642464
(self / rhs, false)
@@ -2489,7 +2489,7 @@ macro_rules! int_impl {
24892489
without modifying the original"]
24902490
pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
24912491
// Using `&` helps LLVM see that it is the same check made in division.
2492-
if unlikely!((self == Self::MIN) & (rhs == -1)) {
2492+
if (self == Self::MIN) & (rhs == -1) {
24932493
(self, true)
24942494
} else {
24952495
(self.div_euclid(rhs), false)
@@ -2519,7 +2519,7 @@ macro_rules! int_impl {
25192519
#[must_use = "this returns the result of the operation, \
25202520
without modifying the original"]
25212521
pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
2522-
if unlikely!(rhs == -1) {
2522+
if rhs == -1 {
25232523
(0, self == Self::MIN)
25242524
} else {
25252525
(self % rhs, false)
@@ -2551,7 +2551,7 @@ macro_rules! int_impl {
25512551
#[inline]
25522552
#[track_caller]
25532553
pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
2554-
if unlikely!(rhs == -1) {
2554+
if rhs == -1 {
25552555
(0, self == Self::MIN)
25562556
} else {
25572557
(self.rem_euclid(rhs), false)
@@ -2580,7 +2580,7 @@ macro_rules! int_impl {
25802580
without modifying the original"]
25812581
#[allow(unused_attributes)]
25822582
pub const fn overflowing_neg(self) -> (Self, bool) {
2583-
if unlikely!(self == Self::MIN) {
2583+
if self == Self::MIN {
25842584
(Self::MIN, true)
25852585
} else {
25862586
(-self, false)

‎library/core/src/num/mod.rs

-7
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@ macro_rules! try_opt {
1919
};
2020
}
2121

22-
#[allow_internal_unstable(const_likely)]
23-
macro_rules! unlikely {
24-
($e: expr) => {
25-
intrinsics::unlikely($e)
26-
};
27-
}
28-
2922
// All these modules are technically private and only exposed for coretests:
3023
#[cfg(not(no_fp_fmt_parse))]
3124
pub mod bignum;

‎library/core/src/num/uint_macros.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ macro_rules! uint_impl {
456456
#[inline]
457457
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
458458
let (a, b) = self.overflowing_add(rhs);
459-
if unlikely!(b) { None } else { Some(a) }
459+
if b { None } else { Some(a) }
460460
}
461461

462462
/// Strict integer addition. Computes `self + rhs`, panicking
@@ -491,7 +491,7 @@ macro_rules! uint_impl {
491491
#[track_caller]
492492
pub const fn strict_add(self, rhs: Self) -> Self {
493493
let (a, b) = self.overflowing_add(rhs);
494-
if unlikely!(b) { overflow_panic ::add()} else {a}
494+
if b { overflow_panic ::add()} else {a}
495495
}
496496

497497
/// Unchecked integer addition. Computes `self + rhs`, assuming overflow
@@ -553,7 +553,7 @@ macro_rules! uint_impl {
553553
#[inline]
554554
pub const fn checked_add_signed(self, rhs: $SignedT) -> Option<Self> {
555555
let (a, b) = self.overflowing_add_signed(rhs);
556-
if unlikely!(b) { None } else { Some(a) }
556+
if b { None } else { Some(a) }
557557
}
558558

559559
/// Strict addition with a signed integer. Computes `self + rhs`,
@@ -593,7 +593,7 @@ macro_rules! uint_impl {
593593
#[track_caller]
594594
pub const fn strict_add_signed(self, rhs: $SignedT) -> Self {
595595
let (a, b) = self.overflowing_add_signed(rhs);
596-
if unlikely!(b) { overflow_panic ::add()} else {a}
596+
if b { overflow_panic ::add()} else {a}
597597
}
598598

599599
/// Checked integer subtraction. Computes `self - rhs`, returning
@@ -658,7 +658,7 @@ macro_rules! uint_impl {
658658
#[track_caller]
659659
pub const fn strict_sub(self, rhs: Self) -> Self {
660660
let (a, b) = self.overflowing_sub(rhs);
661-
if unlikely!(b) { overflow_panic ::sub()} else {a}
661+
if b { overflow_panic ::sub()} else {a}
662662
}
663663

664664
/// Unchecked integer subtraction. Computes `self - rhs`, assuming overflow
@@ -744,7 +744,7 @@ macro_rules! uint_impl {
744744
#[inline]
745745
pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
746746
let (a, b) = self.overflowing_mul(rhs);
747-
if unlikely!(b) { None } else { Some(a) }
747+
if b { None } else { Some(a) }
748748
}
749749

750750
/// Strict integer multiplication. Computes `self * rhs`, panicking if
@@ -779,7 +779,7 @@ macro_rules! uint_impl {
779779
#[track_caller]
780780
pub const fn strict_mul(self, rhs: Self) -> Self {
781781
let (a, b) = self.overflowing_mul(rhs);
782-
if unlikely!(b) { overflow_panic ::mul()} else {a}
782+
if b { overflow_panic ::mul()} else {a}
783783
}
784784

785785
/// Unchecked integer multiplication. Computes `self * rhs`, assuming overflow
@@ -839,7 +839,7 @@ macro_rules! uint_impl {
839839
without modifying the original"]
840840
#[inline]
841841
pub const fn checked_div(self, rhs: Self) -> Option<Self> {
842-
if unlikely!(rhs == 0) {
842+
if rhs == 0 {
843843
None
844844
} else {
845845
// SAFETY: div by zero has been checked above and unsigned types have no other
@@ -900,7 +900,7 @@ macro_rules! uint_impl {
900900
without modifying the original"]
901901
#[inline]
902902
pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
903-
if unlikely!(rhs == 0) {
903+
if rhs == 0 {
904904
None
905905
} else {
906906
Some(self.div_euclid(rhs))
@@ -961,7 +961,7 @@ macro_rules! uint_impl {
961961
without modifying the original"]
962962
#[inline]
963963
pub const fn checked_rem(self, rhs: Self) -> Option<Self> {
964-
if unlikely!(rhs == 0) {
964+
if rhs == 0 {
965965
None
966966
} else {
967967
// SAFETY: div by zero has been checked above and unsigned types have no other
@@ -1023,7 +1023,7 @@ macro_rules! uint_impl {
10231023
without modifying the original"]
10241024
#[inline]
10251025
pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
1026-
if unlikely!(rhs == 0) {
1026+
if rhs == 0 {
10271027
None
10281028
} else {
10291029
Some(self.rem_euclid(rhs))
@@ -1267,7 +1267,7 @@ macro_rules! uint_impl {
12671267
#[inline]
12681268
pub const fn checked_neg(self) -> Option<Self> {
12691269
let (a, b) = self.overflowing_neg();
1270-
if unlikely!(b) { None } else { Some(a) }
1270+
if b { None } else { Some(a) }
12711271
}
12721272

12731273
/// Strict negation. Computes `-self`, panicking unless `self ==
@@ -1304,7 +1304,7 @@ macro_rules! uint_impl {
13041304
#[track_caller]
13051305
pub const fn strict_neg(self) -> Self {
13061306
let (a, b) = self.overflowing_neg();
1307-
if unlikely!(b) { overflow_panic::neg() } else { a }
1307+
if b { overflow_panic::neg() } else { a }
13081308
}
13091309

13101310
/// Checked shift left. Computes `self << rhs`, returning `None`
@@ -1367,7 +1367,7 @@ macro_rules! uint_impl {
13671367
#[track_caller]
13681368
pub const fn strict_shl(self, rhs: u32) -> Self {
13691369
let (a, b) = self.overflowing_shl(rhs);
1370-
if unlikely!(b) { overflow_panic::shl() } else { a }
1370+
if b { overflow_panic::shl() } else { a }
13711371
}
13721372

13731373
/// Unchecked shift left. Computes `self << rhs`, assuming that
@@ -1465,7 +1465,7 @@ macro_rules! uint_impl {
14651465
#[track_caller]
14661466
pub const fn strict_shr(self, rhs: u32) -> Self {
14671467
let (a, b) = self.overflowing_shr(rhs);
1468-
if unlikely!(b) { overflow_panic::shr() } else { a }
1468+
if b { overflow_panic::shr() } else { a }
14691469
}
14701470

14711471
/// Unchecked shift right. Computes `self >> rhs`, assuming that

0 commit comments

Comments
 (0)
Please sign in to comment.