Skip to content

Commit

Permalink
Optimize saturating_add_signed
Browse files Browse the repository at this point in the history
  • Loading branch information
a1phyr committed Oct 4, 2021
1 parent 4846fd9 commit 47edde1
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit 47edde1

Please sign in to comment.