Skip to content

Commit

Permalink
Also get add nuw from uN::checked_add
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmcm committed Jun 23, 2024
1 parent a48f3d6 commit e4bc79d
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,19 @@ macro_rules! uint_impl {
without modifying the original"]
#[inline]
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
let (a, b) = self.overflowing_add(rhs);
if unlikely!(b) { None } else { Some(a) }
// This used to use `overflowing_add`, but that means it ends up being
// a `wrapping_add`, losing some optimization opportunities. Notably,
// phrasing it this way helps `.checked_add(1)` optimize to a check
// against `MAX` and a `add nuw`.
// Per <https://github.com/rust-lang/rust/pull/124114#issuecomment-2066173305>,
// LLVM is happy to re-form the intrinsic later if useful.

if unlikely!(intrinsics::add_with_overflow(self, rhs).1) {
None
} else {
// SAFETY: Just checked it doesn't overflow
Some(unsafe { intrinsics::unchecked_add(self, rhs) })
}
}

/// Strict integer addition. Computes `self + rhs`, panicking
Expand Down

0 comments on commit e4bc79d

Please sign in to comment.