@@ -1508,37 +1508,42 @@ macro_rules! uint_impl {
15081508 ( a as Self , b)
15091509 }
15101510
1511- /// Calculates `self + rhs + carry` without the ability to overflow.
1511+ /// Calculates `self` + `rhs` + `carry` and returns a tuple containing
1512+ /// the sum and the output carry.
15121513 ///
1513- /// Performs "ternary addition" which takes in an extra bit to add, and may return an
1514- /// additional bit of overflow. This allows for chaining together multiple additions
1515- /// to create "big integers" which represent larger values.
1514+ /// Performs "ternary addition" of two integer operands and a carry-in
1515+ /// bit, and returns an output integer and a carry-out bit. This allows
1516+ /// chaining together multiple additions to create a wider addition, and
1517+ /// can be useful for bignum addition.
15161518 ///
15171519 #[ doc = concat!( "This can be thought of as a " , stringify!( $BITS) , "-bit \" full adder\" , in the electronics sense." ) ]
15181520 ///
1519- /// # Examples
1521+ /// If the input carry is false, this method is equivalent to
1522+ /// [`overflowing_add`](Self::overflowing_add), and the output carry is
1523+ /// equal to the overflow flag. Note that although carry and overflow
1524+ /// flags are similar for unsigned integers, they are different for
1525+ /// signed integers.
15201526 ///
1521- /// Basic usage
1527+ /// # Examples
15221528 ///
15231529 /// ```
15241530 /// #![feature(bigint_helper_methods)]
1525- #[ doc = concat!( "assert_eq!(5" , stringify!( $SelfT) , ".carrying_add(2, false), (7, false));" ) ]
1526- #[ doc = concat!( "assert_eq!(5" , stringify!( $SelfT) , ".carrying_add(2, true), (8, false));" ) ]
1527- #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MAX.carrying_add(1, false), (0, true));" ) ]
1528- #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MAX.carrying_add(0, true), (0, true));" ) ]
1529- #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MAX.carrying_add(1, true), (1, true));" ) ]
1530- #[ doc = concat!( "assert_eq!(" ,
1531- stringify!( $SelfT) , "::MAX.carrying_add(" , stringify!( $SelfT) , "::MAX, true), " ,
1532- "(" , stringify!( $SelfT) , "::MAX, true));"
1533- ) ]
1534- /// ```
15351531 ///
1536- /// If `carry` is false, this method is equivalent to [`overflowing_add`](Self::overflowing_add):
1532+ #[ doc = concat!( "// 3 MAX (a = 3 × 2^" , stringify!( $BITS) , " + 2^" , stringify!( $BITS) , " - 1)" ) ]
1533+ #[ doc = concat!( "// + 5 7 (b = 5 × 2^" , stringify!( $BITS) , " + 7)" ) ]
1534+ /// // ---------
1535+ #[ doc = concat!( "// 9 6 (sum = 9 × 2^" , stringify!( $BITS) , " + 6)" ) ]
15371536 ///
1538- /// ```
1539- /// #![feature(bigint_helper_methods)]
1540- #[ doc = concat!( "assert_eq!(5_" , stringify!( $SelfT) , ".carrying_add(2, false), 5_" , stringify!( $SelfT) , ".overflowing_add(2));" ) ]
1541- #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MAX.carrying_add(1, false), " , stringify!( $SelfT) , "::MAX.overflowing_add(1));" ) ]
1537+ #[ doc = concat!( "let (a1, a0): (" , stringify!( $SelfT) , ", " , stringify!( $SelfT) , ") = (3, " , stringify!( $SelfT) , "::MAX);" ) ]
1538+ #[ doc = concat!( "let (b1, b0): (" , stringify!( $SelfT) , ", " , stringify!( $SelfT) , ") = (5, 7);" ) ]
1539+ /// let carry0 = false;
1540+ ///
1541+ /// let (sum0, carry1) = a0.carrying_add(b0, carry0);
1542+ /// assert_eq!(carry1, true);
1543+ /// let (sum1, carry2) = a1.carrying_add(b1, carry1);
1544+ /// assert_eq!(carry2, false);
1545+ ///
1546+ /// assert_eq!((sum1, sum0), (9, 6));
15421547 /// ```
15431548 #[ unstable( feature = "bigint_helper_methods" , issue = "85532" ) ]
15441549 #[ rustc_const_unstable( feature = "const_bigint_helper_methods" , issue = "85532" ) ]
@@ -1604,22 +1609,35 @@ macro_rules! uint_impl {
16041609 ( a as Self , b)
16051610 }
16061611
1607- /// Calculates `self - rhs - borrow` without the ability to overflow.
1612+ /// Calculates `self` − `rhs` − `borrow` and returns a tuple
1613+ /// containing the difference and the output borrow.
16081614 ///
1609- /// Performs "ternary subtraction" which takes in an extra bit to subtract, and may return
1610- /// an additional bit of overflow. This allows for chaining together multiple subtractions
1611- /// to create "big integers" which represent larger values.
1615+ /// Performs "ternary subtraction" by subtracting both an integer
1616+ /// operand and a borrow-in bit from `self`, and returns an output
1617+ /// integer and a borrow-out bit. This allows chaining together multiple
1618+ /// subtractions to create a wider subtraction, and can be useful for
1619+ /// bignum subtraction.
16121620 ///
16131621 /// # Examples
16141622 ///
1615- /// Basic usage
1616- ///
16171623 /// ```
16181624 /// #![feature(bigint_helper_methods)]
1619- #[ doc = concat!( "assert_eq!(5" , stringify!( $SelfT) , ".borrowing_sub(2, false), (3, false));" ) ]
1620- #[ doc = concat!( "assert_eq!(5" , stringify!( $SelfT) , ".borrowing_sub(2, true), (2, false));" ) ]
1621- #[ doc = concat!( "assert_eq!(0" , stringify!( $SelfT) , ".borrowing_sub(1, false), (" , stringify!( $SelfT) , "::MAX, true));" ) ]
1622- #[ doc = concat!( "assert_eq!(0" , stringify!( $SelfT) , ".borrowing_sub(1, true), (" , stringify!( $SelfT) , "::MAX - 1, true));" ) ]
1625+ ///
1626+ #[ doc = concat!( "// 9 6 (a = 9 × 2^" , stringify!( $BITS) , " + 6)" ) ]
1627+ #[ doc = concat!( "// - 5 7 (b = 5 × 2^" , stringify!( $BITS) , " + 7)" ) ]
1628+ /// // ---------
1629+ #[ doc = concat!( "// 3 MAX (diff = 3 × 2^" , stringify!( $BITS) , " + 2^" , stringify!( $BITS) , " - 1)" ) ]
1630+ ///
1631+ #[ doc = concat!( "let (a1, a0): (" , stringify!( $SelfT) , ", " , stringify!( $SelfT) , ") = (9, 6);" ) ]
1632+ #[ doc = concat!( "let (b1, b0): (" , stringify!( $SelfT) , ", " , stringify!( $SelfT) , ") = (5, 7);" ) ]
1633+ /// let borrow0 = false;
1634+ ///
1635+ /// let (diff0, borrow1) = a0.borrowing_sub(b0, borrow0);
1636+ /// assert_eq!(borrow1, true);
1637+ /// let (diff1, borrow2) = a1.borrowing_sub(b1, borrow1);
1638+ /// assert_eq!(borrow2, false);
1639+ ///
1640+ #[ doc = concat!( "assert_eq!((diff1, diff0), (3, " , stringify!( $SelfT) , "::MAX));" ) ]
16231641 /// ```
16241642 #[ unstable( feature = "bigint_helper_methods" , issue = "85532" ) ]
16251643 #[ rustc_const_unstable( feature = "const_bigint_helper_methods" , issue = "85532" ) ]
0 commit comments