Skip to content

Commit

Permalink
Auto merge of #126750 - scottmcm:less-unlikely, r=<try>
Browse files Browse the repository at this point in the history
Stop using `unlikely` in `core::num`

(Draft for now, will definitely need rebasing)
r? ghost
  • Loading branch information
bors committed Jun 20, 2024
2 parents 1ca578e + 776b56e commit d44c1b9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 49 deletions.
54 changes: 27 additions & 27 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ macro_rules! int_impl {
#[inline]
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
let (a, b) = self.overflowing_add(rhs);
if unlikely!(b) { None } else { Some(a) }
if b { None } else { Some(a) }
}

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

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

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

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

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

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

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

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

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

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

/// Checked Euclidean division. Computes `self.div_euclid(rhs)`,
Expand All @@ -924,7 +924,7 @@ macro_rules! int_impl {
#[inline]
pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
// Using `&` helps LLVM see that it is the same check made in division.
if unlikely!(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
if rhs == 0 || ((self == Self::MIN) & (rhs == -1)) {
None
} else {
Some(self.div_euclid(rhs))
Expand Down Expand Up @@ -976,7 +976,7 @@ macro_rules! int_impl {
#[track_caller]
pub const fn strict_div_euclid(self, rhs: Self) -> Self {
let (a, b) = self.overflowing_div_euclid(rhs);
if unlikely!(b) { overflow_panic::div() } else { a }
if b { overflow_panic::div() } else { a }
}

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

/// Checked Euclidean remainder. Computes `self.rem_euclid(rhs)`, returning `None`
Expand All @@ -1071,7 +1071,7 @@ macro_rules! int_impl {
#[inline]
pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
// Using `&` helps LLVM see that it is the same check made in division.
if unlikely!(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
if rhs == 0 || ((self == Self::MIN) & (rhs == -1)) {
None
} else {
Some(self.rem_euclid(rhs))
Expand Down Expand Up @@ -1122,7 +1122,7 @@ macro_rules! int_impl {
#[track_caller]
pub const fn strict_rem_euclid(self, rhs: Self) -> Self {
let (a, b) = self.overflowing_rem_euclid(rhs);
if unlikely!(b) { overflow_panic::rem() } else { a }
if b { overflow_panic::rem() } else { a }
}

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

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

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

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

/// Unchecked shift right. Computes `self >> rhs`, assuming that
Expand Down Expand Up @@ -2458,7 +2458,7 @@ macro_rules! int_impl {
without modifying the original"]
pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
// Using `&` helps LLVM see that it is the same check made in division.
if unlikely!((self == Self::MIN) & (rhs == -1)) {
if (self == Self::MIN) & (rhs == -1) {
(self, true)
} else {
(self / rhs, false)
Expand Down Expand Up @@ -2489,7 +2489,7 @@ macro_rules! int_impl {
without modifying the original"]
pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
// Using `&` helps LLVM see that it is the same check made in division.
if unlikely!((self == Self::MIN) & (rhs == -1)) {
if (self == Self::MIN) & (rhs == -1) {
(self, true)
} else {
(self.div_euclid(rhs), false)
Expand Down Expand Up @@ -2519,7 +2519,7 @@ macro_rules! int_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
if unlikely!(rhs == -1) {
if rhs == -1 {
(0, self == Self::MIN)
} else {
(self % rhs, false)
Expand Down Expand Up @@ -2551,7 +2551,7 @@ macro_rules! int_impl {
#[inline]
#[track_caller]
pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
if unlikely!(rhs == -1) {
if rhs == -1 {
(0, self == Self::MIN)
} else {
(self.rem_euclid(rhs), false)
Expand Down Expand Up @@ -2580,7 +2580,7 @@ macro_rules! int_impl {
without modifying the original"]
#[allow(unused_attributes)]
pub const fn overflowing_neg(self) -> (Self, bool) {
if unlikely!(self == Self::MIN) {
if self == Self::MIN {
(Self::MIN, true)
} else {
(-self, false)
Expand Down
7 changes: 0 additions & 7 deletions library/core/src/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ macro_rules! try_opt {
};
}

#[allow_internal_unstable(const_likely)]
macro_rules! unlikely {
($e: expr) => {
intrinsics::unlikely($e)
};
}

// All these modules are technically private and only exposed for coretests:
#[cfg(not(no_fp_fmt_parse))]
pub mod bignum;
Expand Down
30 changes: 15 additions & 15 deletions library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ macro_rules! uint_impl {
#[inline]
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
let (a, b) = self.overflowing_add(rhs);
if unlikely!(b) { None } else { Some(a) }
if b { None } else { Some(a) }
}

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

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

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

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

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

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

/// Unchecked integer multiplication. Computes `self * rhs`, assuming overflow
Expand Down Expand Up @@ -839,7 +839,7 @@ macro_rules! uint_impl {
without modifying the original"]
#[inline]
pub const fn checked_div(self, rhs: Self) -> Option<Self> {
if unlikely!(rhs == 0) {
if rhs == 0 {
None
} else {
// SAFETY: div by zero has been checked above and unsigned types have no other
Expand Down Expand Up @@ -900,7 +900,7 @@ macro_rules! uint_impl {
without modifying the original"]
#[inline]
pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
if unlikely!(rhs == 0) {
if rhs == 0 {
None
} else {
Some(self.div_euclid(rhs))
Expand Down Expand Up @@ -961,7 +961,7 @@ macro_rules! uint_impl {
without modifying the original"]
#[inline]
pub const fn checked_rem(self, rhs: Self) -> Option<Self> {
if unlikely!(rhs == 0) {
if rhs == 0 {
None
} else {
// SAFETY: div by zero has been checked above and unsigned types have no other
Expand Down Expand Up @@ -1023,7 +1023,7 @@ macro_rules! uint_impl {
without modifying the original"]
#[inline]
pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
if unlikely!(rhs == 0) {
if rhs == 0 {
None
} else {
Some(self.rem_euclid(rhs))
Expand Down Expand Up @@ -1267,7 +1267,7 @@ macro_rules! uint_impl {
#[inline]
pub const fn checked_neg(self) -> Option<Self> {
let (a, b) = self.overflowing_neg();
if unlikely!(b) { None } else { Some(a) }
if b { None } else { Some(a) }
}

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

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

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

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

0 comments on commit d44c1b9

Please sign in to comment.