@@ -1514,37 +1514,50 @@ macro_rules! int_impl {
15141514 ( a as Self , b)
15151515 }
15161516
1517- /// Calculates `self + rhs + carry` without the ability to overflow.
1517+ /// Calculates `self` + ` rhs` + ` carry` and checks for overflow.
15181518 ///
1519- /// Performs "signed ternary addition" which takes in an extra bit to add, and may return an
1520- /// additional bit of overflow. This signed function is used only on the highest-ordered data,
1521- /// for which the signed overflow result indicates whether the big integer overflowed or not.
1519+ /// Performs "ternary addition" of two integer operands and a carry-in
1520+ /// bit, and returns a tuple of the sum along with a boolean indicating
1521+ /// whether an arithmetic overflow would occur. On overflow, the wrapped
1522+ /// value is returned.
15221523 ///
1523- /// # Examples
1524+ /// This allows chaining together multiple additions to create a wider
1525+ /// addition, and can be useful for bignum addition. This method should
1526+ /// only be used for the most significant word; for the less significant
1527+ /// words the unsigned method
1528+ #[ doc = concat!( "[`" , stringify!( $UnsignedT) , "::carrying_add`]" ) ]
1529+ /// should be used.
15241530 ///
1525- /// Basic usage:
1531+ /// The output boolean returned by this method is *not* a carry flag,
1532+ /// and should *not* be added to a more significant word.
15261533 ///
1527- /// ```
1528- /// #![feature(bigint_helper_methods)]
1529- #[ doc = concat!( "assert_eq!(5" , stringify!( $SelfT) , ".carrying_add(2, false), (7, false));" ) ]
1530- #[ doc = concat!( "assert_eq!(5" , stringify!( $SelfT) , ".carrying_add(2, true), (8, false));" ) ]
1531- #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MAX.carrying_add(1, false), (" , stringify!( $SelfT) , "::MIN, true));" ) ]
1532- #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MAX.carrying_add(0, true), (" , stringify!( $SelfT) , "::MIN, true));" ) ]
1533- #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MAX.carrying_add(1, true), (" , stringify!( $SelfT) , "::MIN + 1, true));" ) ]
1534- #[ doc = concat!( "assert_eq!(" ,
1535- stringify!( $SelfT) , "::MAX.carrying_add(" , stringify!( $SelfT) , "::MAX, true), " ,
1536- "(-1, true));"
1537- ) ]
1538- #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MIN.carrying_add(-1, true), (" , stringify!( $SelfT) , "::MIN, false));" ) ]
1539- #[ doc = concat!( "assert_eq!(0" , stringify!( $SelfT) , ".carrying_add(" , stringify!( $SelfT) , "::MAX, true), (" , stringify!( $SelfT) , "::MIN, true));" ) ]
1540- /// ```
1534+ /// If the input carry is false, this method is equivalent to
1535+ /// [`overflowing_add`](Self::overflowing_add).
15411536 ///
1542- /// If `carry` is false, this method is equivalent to [`overflowing_add`](Self::overflowing_add):
1537+ /// # Examples
15431538 ///
15441539 /// ```
15451540 /// #![feature(bigint_helper_methods)]
1546- #[ doc = concat!( "assert_eq!(5_" , stringify!( $SelfT) , ".carrying_add(2, false), 5_" , stringify!( $SelfT) , ".overflowing_add(2));" ) ]
1547- #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MAX.carrying_add(1, false), " , stringify!( $SelfT) , "::MAX.overflowing_add(1));" ) ]
1541+ /// // Only the most significant word is signed.
1542+ /// //
1543+ #[ doc = concat!( "// 10 MAX (a = 10 × 2^" , stringify!( $BITS) , " + 2^" , stringify!( $BITS) , " - 1)" ) ]
1544+ #[ doc = concat!( "// + -5 9 (b = -5 × 2^" , stringify!( $BITS) , " + 9)" ) ]
1545+ /// // ---------
1546+ #[ doc = concat!( "// 6 8 (sum = 6 × 2^" , stringify!( $BITS) , " + 8)" ) ]
1547+ ///
1548+ #[ doc = concat!( "let (a1, a0): (" , stringify!( $SelfT) , ", " , stringify!( $UnsignedT) , ") = (10, " , stringify!( $UnsignedT) , "::MAX);" ) ]
1549+ #[ doc = concat!( "let (b1, b0): (" , stringify!( $SelfT) , ", " , stringify!( $UnsignedT) , ") = (-5, 9);" ) ]
1550+ /// let carry0 = false;
1551+ ///
1552+ #[ doc = concat!( "// " , stringify!( $UnsignedT) , "::carrying_add for the less significant words" ) ]
1553+ /// let (sum0, carry1) = a0.carrying_add(b0, carry0);
1554+ /// assert_eq!(carry1, true);
1555+ ///
1556+ #[ doc = concat!( "// " , stringify!( $SelfT) , "::carrying_add for the most significant word" ) ]
1557+ /// let (sum1, overflow) = a1.carrying_add(b1, carry1);
1558+ /// assert_eq!(overflow, false);
1559+ ///
1560+ /// assert_eq!((sum1, sum0), (6, 8));
15481561 /// ```
15491562 #[ unstable( feature = "bigint_helper_methods" , issue = "85532" ) ]
15501563 #[ rustc_const_unstable( feature = "const_bigint_helper_methods" , issue = "85532" ) ]
@@ -1608,25 +1621,51 @@ macro_rules! int_impl {
16081621 ( a as Self , b)
16091622 }
16101623
1611- /// Calculates `self - rhs - borrow` without the ability to overflow.
1624+ /// Calculates `self` − `rhs` − `borrow` and checks for
1625+ /// overflow.
16121626 ///
1613- /// Performs "signed ternary subtraction" which takes in an extra bit to subtract, and may return an
1614- /// additional bit of overflow. This signed function is used only on the highest-ordered data,
1615- /// for which the signed overflow result indicates whether the big integer overflowed or not.
1627+ /// Performs "ternary subtraction" by subtracting both an integer
1628+ /// operandand a borrow-in bit from `self`, and returns a tuple of the
1629+ /// difference along with a boolean indicating whether an arithmetic
1630+ /// overflow would occur. On overflow, the wrapped value is returned.
16161631 ///
1617- /// # Examples
1632+ /// This allows chaining together multiple subtractions to create a
1633+ /// wider subtraction, and can be useful for bignum subtraction. This
1634+ /// method should only be used for the most significant word; for the
1635+ /// less significant words the unsigned method
1636+ #[ doc = concat!( "[`" , stringify!( $UnsignedT) , "::borrowing_sub`]" ) ]
1637+ /// should be used.
16181638 ///
1619- /// Basic usage:
1639+ /// The output boolean returned by this method is *not* a borrow flag,
1640+ /// and should *not* be subtracted from a more significant word.
1641+ ///
1642+ /// If the input borrow is false, this method is equivalent to
1643+ /// [`overflowing_sub`](Self::overflowing_sub).
1644+ ///
1645+ /// # Examples
16201646 ///
16211647 /// ```
16221648 /// #![feature(bigint_helper_methods)]
1623- #[ doc = concat!( "assert_eq!(5" , stringify!( $SelfT) , ".borrowing_sub(2, false), (3, false));" ) ]
1624- #[ doc = concat!( "assert_eq!(5" , stringify!( $SelfT) , ".borrowing_sub(2, true), (2, false));" ) ]
1625- #[ doc = concat!( "assert_eq!(0" , stringify!( $SelfT) , ".borrowing_sub(1, false), (-1, false));" ) ]
1626- #[ doc = concat!( "assert_eq!(0" , stringify!( $SelfT) , ".borrowing_sub(1, true), (-2, false));" ) ]
1627- #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MIN.borrowing_sub(1, true), (" , stringify!( $SelfT) , "::MAX - 1, true));" ) ]
1628- #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MAX.borrowing_sub(-1, false), (" , stringify!( $SelfT) , "::MIN, true));" ) ]
1629- #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MAX.borrowing_sub(-1, true), (" , stringify!( $SelfT) , "::MAX, false));" ) ]
1649+ /// // Only the most significant word is signed.
1650+ /// //
1651+ #[ doc = concat!( "// 6 8 (a = 6 × 2^" , stringify!( $BITS) , " + 8)" ) ]
1652+ #[ doc = concat!( "// - -5 9 (b = -5 × 2^" , stringify!( $BITS) , " + 9)" ) ]
1653+ /// // ---------
1654+ #[ doc = concat!( "// 10 MAX (diff = 10 × 2^" , stringify!( $BITS) , " + 2^" , stringify!( $BITS) , " - 1)" ) ]
1655+ ///
1656+ #[ doc = concat!( "let (a1, a0): (" , stringify!( $SelfT) , ", " , stringify!( $UnsignedT) , ") = (6, 8);" ) ]
1657+ #[ doc = concat!( "let (b1, b0): (" , stringify!( $SelfT) , ", " , stringify!( $UnsignedT) , ") = (-5, 9);" ) ]
1658+ /// let borrow0 = false;
1659+ ///
1660+ #[ doc = concat!( "// " , stringify!( $UnsignedT) , "::borrowing_sub for the less significant words" ) ]
1661+ /// let (diff0, borrow1) = a0.borrowing_sub(b0, borrow0);
1662+ /// assert_eq!(borrow1, true);
1663+ ///
1664+ #[ doc = concat!( "// " , stringify!( $SelfT) , "::borrowing_sub for the most significant word" ) ]
1665+ /// let (diff1, overflow) = a1.borrowing_sub(b1, borrow1);
1666+ /// assert_eq!(overflow, false);
1667+ ///
1668+ #[ doc = concat!( "assert_eq!((diff1, diff0), (10, " , stringify!( $UnsignedT) , "::MAX));" ) ]
16301669 /// ```
16311670 #[ unstable( feature = "bigint_helper_methods" , issue = "85532" ) ]
16321671 #[ rustc_const_unstable( feature = "const_bigint_helper_methods" , issue = "85532" ) ]
0 commit comments