@@ -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 = "unsigned_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"]
@@ -2533,8 +2531,6 @@ macro_rules! uint_impl {
2533
2531
/// # Examples
2534
2532
///
2535
2533
/// ```
2536
- /// #![feature(bigint_helper_methods)]
2537
- ///
2538
2534
#[ doc = concat!( "// 9 6 (a = 9 × 2^" , stringify!( $BITS) , " + 6)" ) ]
2539
2535
#[ doc = concat!( "// - 5 7 (b = 5 × 2^" , stringify!( $BITS) , " + 7)" ) ]
2540
2536
/// // ---------
@@ -2551,7 +2547,7 @@ macro_rules! uint_impl {
2551
2547
///
2552
2548
#[ doc = concat!( "assert_eq!((diff1, diff0), (3, " , stringify!( $SelfT) , "::MAX));" ) ]
2553
2549
/// ```
2554
- #[ unstable ( feature = "bigint_helper_methods " , issue = "85532 " ) ]
2550
+ #[ stable ( feature = "unsigned_bigint_helpers " , since = "CURRENT_RUSTC_VERSION " ) ]
2555
2551
#[ rustc_const_unstable( feature = "bigint_helper_methods" , issue = "85532" ) ]
2556
2552
#[ must_use = "this returns the result of the operation, \
2557
2553
without modifying the original"]
@@ -2625,6 +2621,9 @@ macro_rules! uint_impl {
2625
2621
/// indicating whether an arithmetic overflow would occur. If an
2626
2622
/// overflow would have occurred then the wrapped value is returned.
2627
2623
///
2624
+ /// If you want the *value* of the overflow, rather than just *whether*
2625
+ /// an overflow occurred, see [`Self::carrying_mul`].
2626
+ ///
2628
2627
/// # Examples
2629
2628
///
2630
2629
/// Please note that this example is shared between integer types.
@@ -2644,16 +2643,36 @@ macro_rules! uint_impl {
2644
2643
( a as Self , b)
2645
2644
}
2646
2645
2647
- /// Calculates the complete product `self * rhs` without the possibility to overflow .
2646
+ /// Calculates the complete double-width product `self * rhs`.
2648
2647
///
2649
2648
/// This returns the low-order (wrapping) bits and the high-order (overflow) bits
2650
- /// of the result as two separate values, in that order.
2649
+ /// of the result as two separate values, in that order. As such,
2650
+ /// `a.widening_mul(b).0` produces the same result as `a.wrapping_mul(b)`.
2651
+ ///
2652
+ /// If you also need to add a value and carry to the wide result, then you want
2653
+ /// [`Self::carrying_mul_add`] instead.
2651
2654
///
2652
2655
/// If you also need to add a carry to the wide result, then you want
2653
2656
/// [`Self::carrying_mul`] instead.
2654
2657
///
2658
+ /// If you just want to know *whether* the multiplication overflowed, then you
2659
+ /// want [`Self::overflowing_mul`] instead.
2660
+ ///
2655
2661
/// # Examples
2656
2662
///
2663
+ /// ```
2664
+ #[ doc = concat!( "assert_eq!(5_" , stringify!( $SelfT) , ".widening_mul(7), (35, 0));" ) ]
2665
+ #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MAX.widening_mul(" , stringify!( $SelfT) , "::MAX), (1, " , stringify!( $SelfT) , "::MAX - 1));" ) ]
2666
+ /// ```
2667
+ ///
2668
+ /// Compared to other `*_mul` methods:
2669
+ /// ```
2670
+ #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::widening_mul(1 << " , stringify!( $BITS_MINUS_ONE) , ", 6), (0, 3));" ) ]
2671
+ #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::overflowing_mul(1 << " , stringify!( $BITS_MINUS_ONE) , ", 6), (0, true));" ) ]
2672
+ #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::wrapping_mul(1 << " , stringify!( $BITS_MINUS_ONE) , ", 6), 0);" ) ]
2673
+ #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::checked_mul(1 << " , stringify!( $BITS_MINUS_ONE) , ", 6), None);" ) ]
2674
+ /// ```
2675
+ ///
2657
2676
/// Please note that this example is shared between integer types.
2658
2677
/// Which explains why `u32` is used here.
2659
2678
///
@@ -2681,15 +2700,14 @@ macro_rules! uint_impl {
2681
2700
/// additional amount of overflow. This allows for chaining together multiple
2682
2701
/// multiplications to create "big integers" which represent larger values.
2683
2702
///
2684
- /// If you don't need the `carry` , then you can use [`Self::widening_mul`] instead .
2703
+ /// If you also need to add a value , then use [`Self::carrying_mul_add`] .
2685
2704
///
2686
2705
/// # Examples
2687
2706
///
2688
2707
/// Please note that this example is shared between integer types.
2689
2708
/// Which explains why `u32` is used here.
2690
2709
///
2691
2710
/// ```
2692
- /// #![feature(bigint_helper_methods)]
2693
2711
/// assert_eq!(5u32.carrying_mul(2, 0), (10, 0));
2694
2712
/// assert_eq!(5u32.carrying_mul(2, 10), (20, 0));
2695
2713
/// assert_eq!(1_000_000_000u32.carrying_mul(10, 0), (1410065408, 2));
@@ -2747,7 +2765,7 @@ macro_rules! uint_impl {
2747
2765
/// 789_u16.wrapping_mul(456).wrapping_add(123),
2748
2766
/// );
2749
2767
/// ```
2750
- #[ unstable ( feature = "bigint_helper_methods " , issue = "85532 " ) ]
2768
+ #[ stable ( feature = "unsigned_bigint_helpers " , since = "CURRENT_RUSTC_VERSION " ) ]
2751
2769
#[ rustc_const_unstable( feature = "bigint_helper_methods" , issue = "85532" ) ]
2752
2770
#[ must_use = "this returns the result of the operation, \
2753
2771
without modifying the original"]
@@ -2756,26 +2774,27 @@ macro_rules! uint_impl {
2756
2774
Self :: carrying_mul_add( self , rhs, carry, 0 )
2757
2775
}
2758
2776
2759
- /// Calculates the "full multiplication" `self * rhs + carry1 + carry2`
2760
- /// without the possibility to overflow.
2777
+ /// Calculates the "full multiplication" `self * rhs + carry1 + carry2`.
2761
2778
///
2762
2779
/// This returns the low-order (wrapping) bits and the high-order (overflow) bits
2763
2780
/// of the result as two separate values, in that order.
2764
2781
///
2782
+ /// This cannot overflow, as the double-width result has exactly enough
2783
+ /// space for the largest possible result. This is equivalent to how, in
2784
+ /// decimal, 9 × 9 + 9 + 9 = 81 + 18 = 99 = 9×10⁰ + 9×10¹ = 10² - 1.
2785
+ ///
2765
2786
/// Performs "long multiplication" which takes in an extra amount to add, and may return an
2766
2787
/// additional amount of overflow. This allows for chaining together multiple
2767
2788
/// multiplications to create "big integers" which represent larger values.
2768
2789
///
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.
2790
+ /// If you don't need the `add` part, then you can use [`Self::carrying_mul`] instead.
2771
2791
///
2772
2792
/// # Examples
2773
2793
///
2774
2794
/// Please note that this example is shared between integer types,
2775
2795
/// which explains why `u32` is used here.
2776
2796
///
2777
2797
/// ```
2778
- /// #![feature(bigint_helper_methods)]
2779
2798
/// assert_eq!(5u32.carrying_mul_add(2, 0, 0), (10, 0));
2780
2799
/// assert_eq!(5u32.carrying_mul_add(2, 10, 10), (30, 0));
2781
2800
/// assert_eq!(1_000_000_000u32.carrying_mul_add(10, 0, 0), (1410065408, 2));
@@ -2792,8 +2811,6 @@ macro_rules! uint_impl {
2792
2811
/// using `u8` for simplicity of the demonstration.
2793
2812
///
2794
2813
/// ```
2795
- /// #![feature(bigint_helper_methods)]
2796
- ///
2797
2814
/// fn quadratic_mul<const N: usize>(a: [u8; N], b: [u8; N]) -> [u8; N] {
2798
2815
/// let mut out = [0; N];
2799
2816
/// for j in 0..N {
@@ -2808,13 +2825,13 @@ macro_rules! uint_impl {
2808
2825
/// // -1 * -1 == 1
2809
2826
/// assert_eq!(quadratic_mul([0xFF; 3], [0xFF; 3]), [1, 0, 0]);
2810
2827
///
2811
- /// assert_eq!(u32::wrapping_mul(0x9e3779b9, 0x7f4a7c15), 0xCFFC982D );
2828
+ /// assert_eq!(u32::wrapping_mul(0x9e3779b9, 0x7f4a7c15), 0xcffc982d );
2812
2829
/// assert_eq!(
2813
2830
/// quadratic_mul(u32::to_le_bytes(0x9e3779b9), u32::to_le_bytes(0x7f4a7c15)),
2814
- /// u32::to_le_bytes(0xCFFC982D )
2831
+ /// u32::to_le_bytes(0xcffc982d )
2815
2832
/// );
2816
2833
/// ```
2817
- #[ unstable ( feature = "bigint_helper_methods " , issue = "85532 " ) ]
2834
+ #[ stable ( feature = "unsigned_bigint_helpers " , since = "CURRENT_RUSTC_VERSION " ) ]
2818
2835
#[ rustc_const_unstable( feature = "bigint_helper_methods" , issue = "85532" ) ]
2819
2836
#[ must_use = "this returns the result of the operation, \
2820
2837
without modifying the original"]
0 commit comments