@@ -1514,15 +1514,41 @@ macro_rules! int_impl {
1514
1514
1515
1515
/// Calculates `self + rhs + carry` without the ability to overflow.
1516
1516
///
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.
1522
1520
///
1523
1521
/// # Examples
1524
1522
///
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
1526
1552
///
1527
1553
/// ```
1528
1554
/// #![feature(bigint_helper_methods)]
@@ -1612,13 +1638,41 @@ macro_rules! int_impl {
1612
1638
1613
1639
/// Calculates `self - rhs - borrow` without the ability to overflow.
1614
1640
///
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 .
1618
1644
///
1619
1645
/// # Examples
1620
1646
///
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
1622
1676
///
1623
1677
/// ```
1624
1678
/// #![feature(bigint_helper_methods)]
0 commit comments