@@ -2421,7 +2421,7 @@ macro_rules! uint_impl {
2421
2421
}
2422
2422
2423
2423
/// Calculates `self` + `rhs` + `carry` and returns a tuple containing
2424
- /// the sum and the output carry.
2424
+ /// the sum and the output carry (in that order) .
2425
2425
///
2426
2426
/// Performs "ternary addition" of two integer operands and a carry-in
2427
2427
/// bit, and returns an output integer and a carry-out bit. This allows
@@ -2439,8 +2439,6 @@ macro_rules! uint_impl {
2439
2439
/// # Examples
2440
2440
///
2441
2441
/// ```
2442
- /// #![feature(bigint_helper_methods)]
2443
- ///
2444
2442
#[ doc = concat!( "// 3 MAX (a = 3 × 2^" , stringify!( $BITS) , " + 2^" , stringify!( $BITS) , " - 1)" ) ]
2445
2443
#[ doc = concat!( "// + 5 7 (b = 5 × 2^" , stringify!( $BITS) , " + 7)" ) ]
2446
2444
/// // ---------
@@ -2457,7 +2455,7 @@ macro_rules! uint_impl {
2457
2455
///
2458
2456
/// assert_eq!((sum1, sum0), (9, 6));
2459
2457
/// ```
2460
- #[ unstable ( feature = "bigint_helper_methods " , issue = "85532 " ) ]
2458
+ #[ stable ( feature = "min_bigint_helpers " , since = "CURRENT_RUSTC_VERSION " ) ]
2461
2459
#[ rustc_const_unstable( feature = "bigint_helper_methods" , issue = "85532" ) ]
2462
2460
#[ must_use = "this returns the result of the operation, \
2463
2461
without modifying the original"]
@@ -2644,25 +2642,41 @@ macro_rules! uint_impl {
2644
2642
( a as Self , b)
2645
2643
}
2646
2644
2647
- /// Calculates the complete product `self * rhs` without the possibility to overflow .
2645
+ /// Calculates the complete double-width product `self * rhs`.
2648
2646
///
2649
2647
/// This returns the low-order (wrapping) bits and the high-order (overflow) bits
2650
- /// of the result as two separate values, in that order.
2648
+ /// of the result as two separate values, in that order. As such,
2649
+ /// `a.widening_mul(b).0` produces the same result as `a.wrapping_mul(b)`.
2650
+ ///
2651
+ /// If you also need to add a value and/or carry to the wide result, then you want
2652
+ /// [`Self::carrying_mul_add`] instead.
2651
2653
///
2652
- /// If you also need to add a carry to the wide result , then you want
2653
- /// [`Self::carrying_mul `] instead.
2654
+ /// If you just want to know *whether* the multiplication overflowed , then you
2655
+ /// want [`Self::overflowing_mul `] instead.
2654
2656
///
2655
2657
/// # Examples
2656
2658
///
2659
+ /// ```
2660
+ #[ doc = concat!( "assert_eq!(5_" , stringify!( $SelfT) , ".widening_mul(7), (35, 0));" ) ]
2661
+ #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MAX.widening_mul(" , stringify!( $SelfT) , "::MAX), (1, " , stringify!( $SelfT) , "::MAX - 1));" ) ]
2662
+ /// ```
2663
+ ///
2664
+ /// Compared to other `*_mul` methods:
2665
+ /// ```
2666
+ #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::widening_mul(1 << " , stringify!( $BITS_MINUS_ONE) , ", 6), (0, 3));" ) ]
2667
+ #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::overflowing_mul(1 << " , stringify!( $BITS_MINUS_ONE) , ", 6), (0, true));" ) ]
2668
+ #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::wrapping_mul(1 << " , stringify!( $BITS_MINUS_ONE) , ", 6), 0);" ) ]
2669
+ #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::checked_mul(1 << " , stringify!( $BITS_MINUS_ONE) , ", 6), None);" ) ]
2670
+ /// ```
2671
+ ///
2657
2672
/// Please note that this example is shared between integer types.
2658
2673
/// Which explains why `u32` is used here.
2659
2674
///
2660
2675
/// ```
2661
- /// #![feature(bigint_helper_methods)]
2662
2676
/// assert_eq!(5u32.widening_mul(2), (10, 0));
2663
2677
/// assert_eq!(1_000_000_000u32.widening_mul(10), (1410065408, 2));
2664
2678
/// ```
2665
- #[ unstable ( feature = "bigint_helper_methods " , issue = "85532 " ) ]
2679
+ #[ stable ( feature = "min_bigint_helpers " , since = "CURRENT_RUSTC_VERSION " ) ]
2666
2680
#[ rustc_const_unstable( feature = "bigint_helper_methods" , issue = "85532" ) ]
2667
2681
#[ must_use = "this returns the result of the operation, \
2668
2682
without modifying the original"]
@@ -2756,26 +2770,27 @@ macro_rules! uint_impl {
2756
2770
Self :: carrying_mul_add( self , rhs, carry, 0 )
2757
2771
}
2758
2772
2759
- /// Calculates the "full multiplication" `self * rhs + carry1 + carry2`
2760
- /// without the possibility to overflow.
2773
+ /// Calculates the "full multiplication" `self * rhs + carry1 + carry2`.
2761
2774
///
2762
2775
/// This returns the low-order (wrapping) bits and the high-order (overflow) bits
2763
2776
/// of the result as two separate values, in that order.
2764
2777
///
2778
+ /// This cannot overflow, as the double-width result has exactly enough
2779
+ /// space for the largest possible result. This is equivalent to how, in
2780
+ /// decimal, 9 × 9 + 9 + 9 = 81 + 18 = 99 = 9×10⁰ + 9×10¹ = 10² - 1.
2781
+ ///
2765
2782
/// Performs "long multiplication" which takes in an extra amount to add, and may return an
2766
2783
/// additional amount of overflow. This allows for chaining together multiple
2767
2784
/// multiplications to create "big integers" which represent larger values.
2768
2785
///
2769
- /// If you don't need either `carry`, then you can use [`Self::widening_mul`] instead,
2770
- /// and if you only need one `carry`, then you can use [`Self::carrying_mul`] instead.
2786
+ /// If you don't need either `carry`, then you can use [`Self::widening_mul`] instead.
2771
2787
///
2772
2788
/// # Examples
2773
2789
///
2774
2790
/// Please note that this example is shared between integer types,
2775
2791
/// which explains why `u32` is used here.
2776
2792
///
2777
2793
/// ```
2778
- /// #![feature(bigint_helper_methods)]
2779
2794
/// assert_eq!(5u32.carrying_mul_add(2, 0, 0), (10, 0));
2780
2795
/// assert_eq!(5u32.carrying_mul_add(2, 10, 10), (30, 0));
2781
2796
/// assert_eq!(1_000_000_000u32.carrying_mul_add(10, 0, 0), (1410065408, 2));
@@ -2792,8 +2807,6 @@ macro_rules! uint_impl {
2792
2807
/// using `u8` for simplicity of the demonstration.
2793
2808
///
2794
2809
/// ```
2795
- /// #![feature(bigint_helper_methods)]
2796
- ///
2797
2810
/// fn quadratic_mul<const N: usize>(a: [u8; N], b: [u8; N]) -> [u8; N] {
2798
2811
/// let mut out = [0; N];
2799
2812
/// for j in 0..N {
@@ -2808,13 +2821,13 @@ macro_rules! uint_impl {
2808
2821
/// // -1 * -1 == 1
2809
2822
/// assert_eq!(quadratic_mul([0xFF; 3], [0xFF; 3]), [1, 0, 0]);
2810
2823
///
2811
- /// assert_eq!(u32::wrapping_mul(0x9e3779b9, 0x7f4a7c15), 0xCFFC982D );
2824
+ /// assert_eq!(u32::wrapping_mul(0x9e3779b9, 0x7f4a7c15), 0xcffc982d );
2812
2825
/// assert_eq!(
2813
2826
/// quadratic_mul(u32::to_le_bytes(0x9e3779b9), u32::to_le_bytes(0x7f4a7c15)),
2814
- /// u32::to_le_bytes(0xCFFC982D )
2827
+ /// u32::to_le_bytes(0xcffc982d )
2815
2828
/// );
2816
2829
/// ```
2817
- #[ unstable ( feature = "bigint_helper_methods " , issue = "85532 " ) ]
2830
+ #[ stable ( feature = "min_bigint_helpers " , since = "CURRENT_RUSTC_VERSION " ) ]
2818
2831
#[ rustc_const_unstable( feature = "bigint_helper_methods" , issue = "85532" ) ]
2819
2832
#[ must_use = "this returns the result of the operation, \
2820
2833
without modifying the original"]
0 commit comments