about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTobias Decking <Tobias.Decking@gmail.com>2025-03-08 14:40:56 +0100
committerTobias Decking <Tobias.Decking@gmail.com>2025-03-08 15:45:03 +0100
commit8d37f38873fb374371bcd582f9ab29f223ce5d5a (patch)
tree405422ab7d0123b6ddd5e90c1d32446cf2a908c1
parentcdd8af229960d05c8dfe5ca3e5f5e2066e676213 (diff)
downloadrust-8d37f38873fb374371bcd582f9ab29f223ce5d5a.tar.gz
rust-8d37f38873fb374371bcd582f9ab29f223ce5d5a.zip
Use `disjoint_bitor` inside `borrowing_sub`
-rw-r--r--library/core/src/num/uint_macros.rs11
1 files changed, 8 insertions, 3 deletions
diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs
index d8709d51ccc..58689275839 100644
--- a/library/core/src/num/uint_macros.rs
+++ b/library/core/src/num/uint_macros.rs
@@ -2533,15 +2533,20 @@ macro_rules! uint_impl {
         #[doc = concat!("assert_eq!((diff1, diff0), (3, ", stringify!($SelfT), "::MAX));")]
         /// ```
         #[unstable(feature = "bigint_helper_methods", issue = "85532")]
+        #[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")]
         #[must_use = "this returns the result of the operation, \
                       without modifying the original"]
         #[inline]
         pub const fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool) {
             // note: longer-term this should be done via an intrinsic, but this has been shown
             //   to generate optimal code for now, and LLVM doesn't have an equivalent intrinsic
-            let (a, b) = self.overflowing_sub(rhs);
-            let (c, d) = a.overflowing_sub(borrow as $SelfT);
-            (c, b | d)
+            let (a, c1) = self.overflowing_sub(rhs);
+            let (b, c2) = a.overflowing_sub(borrow as $SelfT);
+            // SAFETY: Only one of `c1` and `c2` can be set.
+            // For c1 to be set we need to have underflowed, but if we did then
+            // `a` is nonzero, which means that `c2` cannot possibly
+            // underflow because it's subtracting at most `1` (since it came from `bool`)
+            (b, unsafe { intrinsics::disjoint_bitor(c1, c2) })
         }
 
         /// Calculates `self` - `rhs` with a signed `rhs`