@@ -1469,37 +1469,42 @@ macro_rules! uint_impl {
1469
1469
( a as Self , b)
1470
1470
}
1471
1471
1472
- /// Calculates `self + rhs + carry` without the ability to overflow.
1472
+ /// Calculates `self` + `rhs` + `carry` and returns a tuple containing
1473
+ /// the sum and the output carry.
1473
1474
///
1474
- /// Performs "ternary addition" which takes in an extra bit to add, and may return an
1475
- /// additional bit of overflow. This allows for chaining together multiple additions
1476
- /// to create "big integers" which represent larger values.
1475
+ /// Performs "ternary addition" of two integer operands and a carry-in
1476
+ /// bit, and returns an output integer and a carry-out bit. This allows
1477
+ /// chaining together multiple additions to create a wider addition, and
1478
+ /// can be useful for bignum addition.
1477
1479
///
1478
1480
#[ doc = concat!( "This can be thought of as a " , stringify!( $BITS) , "-bit \" full adder\" , in the electronics sense." ) ]
1479
1481
///
1480
- /// # Examples
1482
+ /// If the input carry is false, this method is equivalent to
1483
+ /// [`overflowing_add`](Self::overflowing_add), and the output carry is
1484
+ /// equal to the overflow flag. Note that although carry and overflow
1485
+ /// flags are similar for unsigned integers, they are different for
1486
+ /// signed integers.
1481
1487
///
1482
- /// Basic usage
1488
+ /// # Examples
1483
1489
///
1484
1490
/// ```
1485
1491
/// #![feature(bigint_helper_methods)]
1486
- #[ doc = concat!( "assert_eq!(5" , stringify!( $SelfT) , ".carrying_add(2, false), (7, false));" ) ]
1487
- #[ doc = concat!( "assert_eq!(5" , stringify!( $SelfT) , ".carrying_add(2, true), (8, false));" ) ]
1488
- #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MAX.carrying_add(1, false), (0, true));" ) ]
1489
- #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MAX.carrying_add(0, true), (0, true));" ) ]
1490
- #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MAX.carrying_add(1, true), (1, true));" ) ]
1491
- #[ doc = concat!( "assert_eq!(" ,
1492
- stringify!( $SelfT) , "::MAX.carrying_add(" , stringify!( $SelfT) , "::MAX, true), " ,
1493
- "(" , stringify!( $SelfT) , "::MAX, true));"
1494
- ) ]
1495
- /// ```
1496
1492
///
1497
- /// If `carry` is false, this method is equivalent to [`overflowing_add`](Self::overflowing_add):
1493
+ #[ doc = concat!( "// 3 MAX (a = 3 × 2^" , stringify!( $BITS) , " + 2^" , stringify!( $BITS) , " - 1)" ) ]
1494
+ #[ doc = concat!( "// + 5 7 (b = 5 × 2^" , stringify!( $BITS) , " + 7)" ) ]
1495
+ /// // ---------
1496
+ #[ doc = concat!( "// 9 6 (sum = 9 × 2^" , stringify!( $BITS) , " + 6)" ) ]
1498
1497
///
1499
- /// ```
1500
- /// #![feature(bigint_helper_methods)]
1501
- #[ doc = concat!( "assert_eq!(5_" , stringify!( $SelfT) , ".carrying_add(2, false), 5_" , stringify!( $SelfT) , ".overflowing_add(2));" ) ]
1502
- #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MAX.carrying_add(1, false), " , stringify!( $SelfT) , "::MAX.overflowing_add(1));" ) ]
1498
+ #[ doc = concat!( "let (a1, a0): (" , stringify!( $SelfT) , ", " , stringify!( $SelfT) , ") = (3, " , stringify!( $SelfT) , "::MAX);" ) ]
1499
+ #[ doc = concat!( "let (b1, b0): (" , stringify!( $SelfT) , ", " , stringify!( $SelfT) , ") = (5, 7);" ) ]
1500
+ /// let carry0 = false;
1501
+ ///
1502
+ /// let (sum0, carry1) = a0.carrying_add(b0, carry0);
1503
+ /// assert_eq!(carry1, true);
1504
+ /// let (sum1, carry2) = a1.carrying_add(b1, carry1);
1505
+ /// assert_eq!(carry2, false);
1506
+ ///
1507
+ /// assert_eq!((sum1, sum0), (9, 6));
1503
1508
/// ```
1504
1509
#[ unstable( feature = "bigint_helper_methods" , issue = "85532" ) ]
1505
1510
#[ rustc_const_unstable( feature = "const_bigint_helper_methods" , issue = "85532" ) ]
@@ -1563,22 +1568,35 @@ macro_rules! uint_impl {
1563
1568
( a as Self , b)
1564
1569
}
1565
1570
1566
- /// Calculates `self - rhs - borrow` without the ability to overflow.
1571
+ /// Calculates `self` − `rhs` − `borrow` and returns a tuple
1572
+ /// containing the difference and the output borrow.
1567
1573
///
1568
- /// Performs "ternary subtraction" which takes in an extra bit to subtract, and may return
1569
- /// an additional bit of overflow. This allows for chaining together multiple subtractions
1570
- /// to create "big integers" which represent larger values.
1574
+ /// Performs "ternary subtraction" by subtracting both an integer
1575
+ /// operand and a borrow-in bit from `self`, and returns an output
1576
+ /// integer and a borrow-out bit. This allows chaining together multiple
1577
+ /// subtractions to create a wider subtraction, and can be useful for
1578
+ /// bignum subtraction.
1571
1579
///
1572
1580
/// # Examples
1573
1581
///
1574
- /// Basic usage
1575
- ///
1576
1582
/// ```
1577
1583
/// #![feature(bigint_helper_methods)]
1578
- #[ doc = concat!( "assert_eq!(5" , stringify!( $SelfT) , ".borrowing_sub(2, false), (3, false));" ) ]
1579
- #[ doc = concat!( "assert_eq!(5" , stringify!( $SelfT) , ".borrowing_sub(2, true), (2, false));" ) ]
1580
- #[ doc = concat!( "assert_eq!(0" , stringify!( $SelfT) , ".borrowing_sub(1, false), (" , stringify!( $SelfT) , "::MAX, true));" ) ]
1581
- #[ doc = concat!( "assert_eq!(0" , stringify!( $SelfT) , ".borrowing_sub(1, true), (" , stringify!( $SelfT) , "::MAX - 1, true));" ) ]
1584
+ ///
1585
+ #[ doc = concat!( "// 9 6 (a = 9 × 2^" , stringify!( $BITS) , " + 6)" ) ]
1586
+ #[ doc = concat!( "// - 5 7 (b = 5 × 2^" , stringify!( $BITS) , " + 7)" ) ]
1587
+ /// // ---------
1588
+ #[ doc = concat!( "// 3 MAX (diff = 3 × 2^" , stringify!( $BITS) , " + 2^" , stringify!( $BITS) , " - 1)" ) ]
1589
+ ///
1590
+ #[ doc = concat!( "let (a1, a0): (" , stringify!( $SelfT) , ", " , stringify!( $SelfT) , ") = (9, 6);" ) ]
1591
+ #[ doc = concat!( "let (b1, b0): (" , stringify!( $SelfT) , ", " , stringify!( $SelfT) , ") = (5, 7);" ) ]
1592
+ /// let borrow0 = false;
1593
+ ///
1594
+ /// let (diff0, borrow1) = a0.borrowing_sub(b0, borrow0);
1595
+ /// assert_eq!(borrow1, true);
1596
+ /// let (diff1, borrow2) = a1.borrowing_sub(b1, borrow1);
1597
+ /// assert_eq!(borrow2, false);
1598
+ ///
1599
+ #[ doc = concat!( "assert_eq!((diff1, diff0), (3, " , stringify!( $SelfT) , "::MAX));" ) ]
1582
1600
/// ```
1583
1601
#[ unstable( feature = "bigint_helper_methods" , issue = "85532" ) ]
1584
1602
#[ rustc_const_unstable( feature = "const_bigint_helper_methods" , issue = "85532" ) ]
0 commit comments