Skip to content

Commit 5c69055

Browse files
committed
Correct signed bit int documentation
1 parent 1266099 commit 5c69055

File tree

1 file changed

+64
-10
lines changed

1 file changed

+64
-10
lines changed

library/core/src/num/int_macros.rs

+64-10
Original file line numberDiff line numberDiff line change
@@ -1514,15 +1514,41 @@ macro_rules! int_impl {
15141514

15151515
/// Calculates `self + rhs + carry` without the ability to overflow.
15161516
///
1517-
/// Performs "ternary addition" which takes in an extra bit to add, and may return an
1518-
/// additional bit of overflow. This allows for chaining together multiple additions
1519-
/// to create "big integers" which represent larger values.
1520-
///
1521-
#[doc = concat!("This can be thought of as a ", stringify!($BITS), "-bit \"full adder\", in the electronics sense.")]
1517+
/// Performs "signed ternary addition" which takes in an extra bit to add, and may return an
1518+
/// additional bit of overflow. This signed function is used only on the highest-ordered data,
1519+
/// for which the signed overflow result indicates whether the big integer overflowed or not.
15221520
///
15231521
/// # Examples
15241522
///
1525-
/// Basic usage
1523+
/// Standard signed bit integer implementation
1524+
///
1525+
/// ```rs
1526+
/// #![feature(bigint_helper_methods)]
1527+
/// struct I16 {
1528+
/// pub low: u8, // Low-order bytes has to be unsigned.
1529+
/// /// Most Significant Data has to be of the same signedness as the desired type.
1530+
/// /// So u8 to implement U16, i8 to implement I16.
1531+
/// pub high: i8,
1532+
/// }
1533+
///
1534+
/// impl I16 {
1535+
/// /// Adds `rhs` to `self` and returns true if signed overflow occurs, false otherwise.
1536+
/// pub fn overflowing_add(&mut self, rhs: Self) -> bool {
1537+
/// let (low_res, low_carry) = self.low.carrying_add(rhs.low, false);
1538+
///
1539+
/// // The signed `carrying_add` method is used to detect signed overflow.
1540+
/// let (high_res, high_carry) = self.high.carrying_add(rhs.high, low_carry);
1541+
///
1542+
/// self.low = low_res;
1543+
/// self.high = high_res;
1544+
/// high_carry
1545+
/// }
1546+
/// }
1547+
///
1548+
/// fn main() {}
1549+
/// ```
1550+
///
1551+
/// General behavior
15261552
///
15271553
/// ```
15281554
/// #![feature(bigint_helper_methods)]
@@ -1612,13 +1638,41 @@ macro_rules! int_impl {
16121638

16131639
/// Calculates `self - rhs - borrow` without the ability to overflow.
16141640
///
1615-
/// Performs "ternary subtraction" which takes in an extra bit to subtract, and may return
1616-
/// an additional bit of overflow. This allows for chaining together multiple subtractions
1617-
/// to create "big integers" which represent larger values.
1641+
/// Performs "signed ternary subtraction" which takes in an extra bit to subtract, and may return an
1642+
/// additional bit of overflow. This signed function is used only on the highest-ordered data,
1643+
/// for which the signed overflow result indicates whether the big integer overflowed or not.
16181644
///
16191645
/// # Examples
16201646
///
1621-
/// Basic usage
1647+
/// Standard signed bit integer implementation
1648+
///
1649+
/// ```rs
1650+
/// #![feature(bigint_helper_methods)]
1651+
/// struct I16 {
1652+
/// pub low: u8, // Low-order bytes has to be unsigned.
1653+
/// /// Most Significant Data has to be of the same signedness as the desired type.
1654+
/// /// So u8 to implement U16, i8 to implement I16.
1655+
/// pub high: i8,
1656+
/// }
1657+
///
1658+
/// impl I16 {
1659+
/// /// Subtracts `rhs` from `self` and returns true if signed overflow occurs, false otherwise.
1660+
/// pub fn overflowing_sub(&mut self, rhs: Self) -> bool {
1661+
/// let (low_res, low_carry) = self.low.borrowing_sub(rhs.low, false);
1662+
///
1663+
/// // The signed `borrowing_sub` method is used to detect signed overflow.
1664+
/// let (high_res, high_carry) = self.high.borrowing_sub(rhs.high, low_carry);
1665+
///
1666+
/// self.low = low_res;
1667+
/// self.high = high_res;
1668+
/// high_carry
1669+
/// }
1670+
/// }
1671+
///
1672+
/// fn main() {}
1673+
/// ```
1674+
///
1675+
/// General behavior
16221676
///
16231677
/// ```
16241678
/// #![feature(bigint_helper_methods)]

0 commit comments

Comments
 (0)