Skip to content

Commit 8d37f38

Browse files
committed
Use disjoint_bitor inside borrowing_sub
1 parent cdd8af2 commit 8d37f38

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

library/core/src/num/uint_macros.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -2533,15 +2533,20 @@ macro_rules! uint_impl {
25332533
#[doc = concat!("assert_eq!((diff1, diff0), (3, ", stringify!($SelfT), "::MAX));")]
25342534
/// ```
25352535
#[unstable(feature = "bigint_helper_methods", issue = "85532")]
2536+
#[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")]
25362537
#[must_use = "this returns the result of the operation, \
25372538
without modifying the original"]
25382539
#[inline]
25392540
pub const fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool) {
25402541
// note: longer-term this should be done via an intrinsic, but this has been shown
25412542
// 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)
2543+
let (a, c1) = self.overflowing_sub(rhs);
2544+
let (b, c2) = a.overflowing_sub(borrow as $SelfT);
2545+
// SAFETY: Only one of `c1` and `c2` can be set.
2546+
// For c1 to be set we need to have underflowed, but if we did then
2547+
// `a` is nonzero, which means that `c2` cannot possibly
2548+
// underflow because it's subtracting at most `1` (since it came from `bool`)
2549+
(b, unsafe { intrinsics::disjoint_bitor(c1, c2) })
25452550
}
25462551

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

0 commit comments

Comments
 (0)