diff options
| author | Trevor Spiteri <tspiteri@ieee.org> | 2021-10-05 15:15:24 +0200 |
|---|---|---|
| committer | Trevor Spiteri <tspiteri@ieee.org> | 2021-10-05 15:15:24 +0200 |
| commit | 4ec0377d6a9118b41df4fb587dc0b7d1fc53656f (patch) | |
| tree | e309ca8a04628bf7874642cdc1921fdd84c92293 | |
| parent | 074f63648bd2368d5ca19aed02b5763a144e5d05 (diff) | |
| download | rust-4ec0377d6a9118b41df4fb587dc0b7d1fc53656f.tar.gz rust-4ec0377d6a9118b41df4fb587dc0b7d1fc53656f.zip | |
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.
| -rw-r--r-- | library/core/src/num/int_macros.rs | 10 |
1 files changed, 4 insertions, 6 deletions
diff --git a/library/core/src/num/int_macros.rs b/library/core/src/num/int_macros.rs index f1dcdf2c1aa..9d46948ce02 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) } |
