From 4ec0377d6a9118b41df4fb587dc0b7d1fc53656f Mon Sep 17 00:00:00 2001 From: Trevor Spiteri Date: Tue, 5 Oct 2021 15:15:24 +0200 Subject: [PATCH] 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. --- library/core/src/num/int_macros.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/library/core/src/num/int_macros.rs b/library/core/src/num/int_macros.rs index f1dcdf2c1aa04..9d46948ce02af 100644 --- a/library/core/src/num/int_macros.rs +++ b/library/core/src/num/int_macros.rs @@ -1533,9 +1533,8 @@ 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) { - // Using `&` helps LLVM see that it is the same check made in division. - if unlikely!((self == Self::MIN) & (rhs == -1)) { - (0, true) + if unlikely!(rhs == -1) { + (0, self == Self::MIN) } else { (self % rhs, false) } @@ -1565,9 +1564,8 @@ macro_rules! int_impl { without modifying the original"] #[inline] pub const fn overflowing_rem_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)) { - (0, true) + if unlikely!(rhs == -1) { + (0, self == Self::MIN) } else { (self.rem_euclid(rhs), false) }