Skip to content

Commit 4ec0377

Browse files
committed
for signed overflowing remainder, delay comparing lhs with MIN
Since the wrapped remainder is going to be 0 for all cases when the rhs is -1, there is no need to divide in this case. Comparing the lhs with MIN is only done for the overflow bool. In particular, this results in better code generation for wrapping remainder, which discards the overflow bool completely.
1 parent 074f636 commit 4ec0377

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

library/core/src/num/int_macros.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1533,9 +1533,8 @@ macro_rules! int_impl {
15331533
#[must_use = "this returns the result of the operation, \
15341534
without modifying the original"]
15351535
pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
1536-
// Using `&` helps LLVM see that it is the same check made in division.
1537-
if unlikely!((self == Self::MIN) & (rhs == -1)) {
1538-
(0, true)
1536+
if unlikely!(rhs == -1) {
1537+
(0, self == Self::MIN)
15391538
} else {
15401539
(self % rhs, false)
15411540
}
@@ -1565,9 +1564,8 @@ macro_rules! int_impl {
15651564
without modifying the original"]
15661565
#[inline]
15671566
pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
1568-
// Using `&` helps LLVM see that it is the same check made in division.
1569-
if unlikely!((self == Self::MIN) & (rhs == -1)) {
1570-
(0, true)
1567+
if unlikely!(rhs == -1) {
1568+
(0, self == Self::MIN)
15711569
} else {
15721570
(self.rem_euclid(rhs), false)
15731571
}

0 commit comments

Comments
 (0)