Skip to content

Commit 0f31b6c

Browse files
authored
Use disjoint_bitor inside borrowing_sub
1 parent cdd8af2 commit 0f31b6c

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

library/core/src/num/uint_macros.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2539,9 +2539,13 @@ macro_rules! uint_impl {
25392539
pub const fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool) {
25402540
// note: longer-term this should be done via an intrinsic, but this has been shown
25412541
// to generate optimal code for now, and LLVM doesn't have an equivalent intrinsic
2542-
let (a, b) = self.overflowing_sub(rhs);
2543-
let (c, d) = a.overflowing_sub(borrow as $SelfT);
2544-
(c, b | d)
2542+
let (a, c1) = self.overflowing_sub(rhs);
2543+
let (b, c2) = a.overflowing_sub(borrow as $SelfT);
2544+
// SAFETY: Only one of `c1` and `c2` can be set.
2545+
// For c1 to be set we need to have underflowed, but if we did then
2546+
// `a` is nonzero, which means that `c2` cannot possibly
2547+
// underflow because it's subtracting at most `1` (since it came from `bool`)
2548+
(b, unsafe { intrinsics::disjoint_bitor(c1, c2) })
25452549
}
25462550

25472551
/// Calculates `self` - `rhs` with a signed `rhs`

0 commit comments

Comments
 (0)