Also get add nuw
from uN::checked_add
· model-checking/verify-rust-std@e4bc79d (original) (raw)
`@@ -455,8 +455,19 @@ macro_rules! uint_impl {
`
455
455
` without modifying the original"]
`
456
456
` #[inline]
`
457
457
`pub const fn checked_add(self, rhs: Self) -> Option {
`
458
``
`-
let (a, b) = self.overflowing_add(rhs);
`
459
``
`-
if unlikely!(b) { None } else { Some(a) }
`
``
458
`` +
// This used to use overflowing_add
, but that means it ends up being
``
``
459
`` +
// a wrapping_add
, losing some optimization opportunities. Notably,
``
``
460
`` +
// phrasing it this way helps .checked_add(1)
optimize to a check
``
``
461
`` +
// against MAX
and a add nuw
.
``
``
462
`+
// Per https://github.com/rust-lang/rust/pull/124114#issuecomment-2066173305,
`
``
463
`+
// LLVM is happy to re-form the intrinsic later if useful.
`
``
464
+
``
465
`+
if unlikely!(intrinsics::add_with_overflow(self, rhs).1) {
`
``
466
`+
None
`
``
467
`+
} else {
`
``
468
`+
// SAFETY: Just checked it doesn't overflow
`
``
469
`+
Some(unsafe { intrinsics::unchecked_add(self, rhs) })
`
``
470
`+
}
`
460
471
`}
`
461
472
``
462
473
`` /// Strict integer addition. Computes self + rhs
, panicking
``