about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBenoît du Garreau <bdgdlm@outlook.com>2021-10-04 18:52:17 +0200
committerBenoît du Garreau <bdgdlm@outlook.com>2021-10-04 18:52:17 +0200
commit47edde1086412b36e9efd6098b191ec15a2a760a (patch)
treea69886ea0830db8b731851aad39835530c1e6020
parent4846fd92c0cc82545c5fd33c9ab3007f03f0f9f7 (diff)
downloadrust-47edde1086412b36e9efd6098b191ec15a2a760a.tar.gz
rust-47edde1086412b36e9efd6098b191ec15a2a760a.zip
Optimize `saturating_add_signed`
-rw-r--r--library/core/src/num/uint_macros.rs9
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
             }
         }