diff options
| author | Benoît du Garreau <bdgdlm@outlook.com> | 2021-10-04 18:52:17 +0200 |
|---|---|---|
| committer | Benoît du Garreau <bdgdlm@outlook.com> | 2021-10-04 18:52:17 +0200 |
| commit | 47edde1086412b36e9efd6098b191ec15a2a760a (patch) | |
| tree | a69886ea0830db8b731851aad39835530c1e6020 | |
| parent | 4846fd92c0cc82545c5fd33c9ab3007f03f0f9f7 (diff) | |
| download | rust-47edde1086412b36e9efd6098b191ec15a2a760a.tar.gz rust-47edde1086412b36e9efd6098b191ec15a2a760a.zip | |
Optimize `saturating_add_signed`
| -rw-r--r-- | library/core/src/num/uint_macros.rs | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs index 5a65f77a879..96375b82582 100644 --- a/library/core/src/num/uint_macros.rs +++ b/library/core/src/num/uint_macros.rs @@ -1037,10 +1037,13 @@ macro_rules! uint_impl { without modifying the original"] #[inline] pub const fn saturating_add_signed(self, rhs: $SignedT) -> Self { - if rhs >= 0 { - self.saturating_add(rhs as Self) + let (res, overflow) = self.overflowing_add(rhs as Self); + if overflow == (rhs < 0) { + res + } else if overflow { + Self::MAX } else { - self.saturating_sub(rhs.unsigned_abs()) + 0 } } |
