Skip to content

Commit 410e0b2

Browse files
authored
Rollup merge of rust-lang#101889 - tspiteri:redoc-uint-adc-sbb, r=m-ou-se
doc: rewrite doc for uint::{carrying_add,borrowing_sub} Reword the documentation for bigint helper methods `uint::{carrying_add,borrowing_sub}` (rust-lang#85532). The examples were also rewritten to demonstrate how the methods can be used in bignum arithmetic. No loops are used in the examples, but the variable names were chosen to include indices so that it is clear how this can be used in a loop if required. Also, previously `carrying_add` had an example to say that if the input carry is false, the method is equivalent to `overflowing_add`. While the note was kept, the example was removed and an extra note was added to make sure this equivalence is not assumed for signed integers as well.
2 parents e94827e + 33421da commit 410e0b2

File tree

1 file changed

+49
-31
lines changed

1 file changed

+49
-31
lines changed

library/core/src/num/uint_macros.rs

+49-31
Original file line numberDiff line numberDiff line change
@@ -1469,37 +1469,42 @@ macro_rules! uint_impl {
14691469
(a as Self, b)
14701470
}
14711471

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.
14731474
///
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.
14771479
///
14781480
#[doc = concat!("This can be thought of as a ", stringify!($BITS), "-bit \"full adder\", in the electronics sense.")]
14791481
///
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.
14811487
///
1482-
/// Basic usage
1488+
/// # Examples
14831489
///
14841490
/// ```
14851491
/// #![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-
/// ```
14961492
///
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)")]
14981497
///
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));
15031508
/// ```
15041509
#[unstable(feature = "bigint_helper_methods", issue = "85532")]
15051510
#[rustc_const_unstable(feature = "const_bigint_helper_methods", issue = "85532")]
@@ -1563,22 +1568,35 @@ macro_rules! uint_impl {
15631568
(a as Self, b)
15641569
}
15651570

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.
15671573
///
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.
15711579
///
15721580
/// # Examples
15731581
///
1574-
/// Basic usage
1575-
///
15761582
/// ```
15771583
/// #![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));")]
15821600
/// ```
15831601
#[unstable(feature = "bigint_helper_methods", issue = "85532")]
15841602
#[rustc_const_unstable(feature = "const_bigint_helper_methods", issue = "85532")]

0 commit comments

Comments
 (0)