Skip to content

Commit de4f6cf

Browse files
committed
Partial-stabilize the basics from bigint_helper_methods
1 parent adcb3d3 commit de4f6cf

File tree

1 file changed

+41
-22
lines changed

1 file changed

+41
-22
lines changed

library/core/src/num/uint_macros.rs

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,7 +2421,7 @@ macro_rules! uint_impl {
24212421
}
24222422

24232423
/// 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).
24252425
///
24262426
/// Performs "ternary addition" of two integer operands and a carry-in
24272427
/// bit, and returns an output integer and a carry-out bit. This allows
@@ -2439,8 +2439,6 @@ macro_rules! uint_impl {
24392439
/// # Examples
24402440
///
24412441
/// ```
2442-
/// #![feature(bigint_helper_methods)]
2443-
///
24442442
#[doc = concat!("// 3 MAX (a = 3 × 2^", stringify!($BITS), " + 2^", stringify!($BITS), " - 1)")]
24452443
#[doc = concat!("// + 5 7 (b = 5 × 2^", stringify!($BITS), " + 7)")]
24462444
/// // ---------
@@ -2457,7 +2455,7 @@ macro_rules! uint_impl {
24572455
///
24582456
/// assert_eq!((sum1, sum0), (9, 6));
24592457
/// ```
2460-
#[unstable(feature = "bigint_helper_methods", issue = "85532")]
2458+
#[stable(feature = "unsigned_bigint_helpers", since = "CURRENT_RUSTC_VERSION")]
24612459
#[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")]
24622460
#[must_use = "this returns the result of the operation, \
24632461
without modifying the original"]
@@ -2533,8 +2531,6 @@ macro_rules! uint_impl {
25332531
/// # Examples
25342532
///
25352533
/// ```
2536-
/// #![feature(bigint_helper_methods)]
2537-
///
25382534
#[doc = concat!("// 9 6 (a = 9 × 2^", stringify!($BITS), " + 6)")]
25392535
#[doc = concat!("// - 5 7 (b = 5 × 2^", stringify!($BITS), " + 7)")]
25402536
/// // ---------
@@ -2551,7 +2547,7 @@ macro_rules! uint_impl {
25512547
///
25522548
#[doc = concat!("assert_eq!((diff1, diff0), (3, ", stringify!($SelfT), "::MAX));")]
25532549
/// ```
2554-
#[unstable(feature = "bigint_helper_methods", issue = "85532")]
2550+
#[stable(feature = "unsigned_bigint_helpers", since = "CURRENT_RUSTC_VERSION")]
25552551
#[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")]
25562552
#[must_use = "this returns the result of the operation, \
25572553
without modifying the original"]
@@ -2625,6 +2621,9 @@ macro_rules! uint_impl {
26252621
/// indicating whether an arithmetic overflow would occur. If an
26262622
/// overflow would have occurred then the wrapped value is returned.
26272623
///
2624+
/// If you want the *value* of the overflow, rather than just *whether*
2625+
/// an overflow occurred, see [`Self::carrying_mul`].
2626+
///
26282627
/// # Examples
26292628
///
26302629
/// Please note that this example is shared between integer types.
@@ -2644,16 +2643,38 @@ macro_rules! uint_impl {
26442643
(a as Self, b)
26452644
}
26462645

2647-
/// Calculates the complete product `self * rhs` without the possibility to overflow.
2646+
/// Calculates the complete double-width product `self * rhs`.
26482647
///
26492648
/// 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.
26512654
///
26522655
/// If you also need to add a carry to the wide result, then you want
26532656
/// [`Self::carrying_mul`] instead.
26542657
///
2658+
/// If you just want to know *whether* the multiplication overflowed, then you
2659+
/// want [`Self::overflowing_mul`] instead.
2660+
///
26552661
/// # Examples
26562662
///
2663+
/// ```
2664+
/// #![feature(bigint_helper_methods)]
2665+
#[doc = concat!("assert_eq!(5_", stringify!($SelfT), ".widening_mul(7), (35, 0));")]
2666+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.widening_mul(", stringify!($SelfT), "::MAX), (1, ", stringify!($SelfT), "::MAX - 1));")]
2667+
/// ```
2668+
///
2669+
/// Compared to other `*_mul` methods:
2670+
/// ```
2671+
/// #![feature(bigint_helper_methods)]
2672+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::widening_mul(1 << ", stringify!($BITS_MINUS_ONE), ", 6), (0, 3));")]
2673+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::overflowing_mul(1 << ", stringify!($BITS_MINUS_ONE), ", 6), (0, true));")]
2674+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::wrapping_mul(1 << ", stringify!($BITS_MINUS_ONE), ", 6), 0);")]
2675+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::checked_mul(1 << ", stringify!($BITS_MINUS_ONE), ", 6), None);")]
2676+
/// ```
2677+
///
26572678
/// Please note that this example is shared between integer types.
26582679
/// Which explains why `u32` is used here.
26592680
///
@@ -2681,15 +2702,14 @@ macro_rules! uint_impl {
26812702
/// additional amount of overflow. This allows for chaining together multiple
26822703
/// multiplications to create "big integers" which represent larger values.
26832704
///
2684-
/// If you don't need the `carry`, then you can use [`Self::widening_mul`] instead.
2705+
/// If you also need to add a value, then use [`Self::carrying_mul_add`].
26852706
///
26862707
/// # Examples
26872708
///
26882709
/// Please note that this example is shared between integer types.
26892710
/// Which explains why `u32` is used here.
26902711
///
26912712
/// ```
2692-
/// #![feature(bigint_helper_methods)]
26932713
/// assert_eq!(5u32.carrying_mul(2, 0), (10, 0));
26942714
/// assert_eq!(5u32.carrying_mul(2, 10), (20, 0));
26952715
/// assert_eq!(1_000_000_000u32.carrying_mul(10, 0), (1410065408, 2));
@@ -2747,7 +2767,7 @@ macro_rules! uint_impl {
27472767
/// 789_u16.wrapping_mul(456).wrapping_add(123),
27482768
/// );
27492769
/// ```
2750-
#[unstable(feature = "bigint_helper_methods", issue = "85532")]
2770+
#[stable(feature = "unsigned_bigint_helpers", since = "CURRENT_RUSTC_VERSION")]
27512771
#[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")]
27522772
#[must_use = "this returns the result of the operation, \
27532773
without modifying the original"]
@@ -2756,26 +2776,27 @@ macro_rules! uint_impl {
27562776
Self::carrying_mul_add(self, rhs, carry, 0)
27572777
}
27582778

2759-
/// Calculates the "full multiplication" `self * rhs + carry1 + carry2`
2760-
/// without the possibility to overflow.
2779+
/// Calculates the "full multiplication" `self * rhs + carry1 + carry2`.
27612780
///
27622781
/// This returns the low-order (wrapping) bits and the high-order (overflow) bits
27632782
/// of the result as two separate values, in that order.
27642783
///
2784+
/// This cannot overflow, as the double-width result has exactly enough
2785+
/// space for the largest possible result. This is equivalent to how, in
2786+
/// decimal, 9 × 9 + 9 + 9 = 81 + 18 = 99 = 9×10⁰ + 9×10¹ = 10² - 1.
2787+
///
27652788
/// Performs "long multiplication" which takes in an extra amount to add, and may return an
27662789
/// additional amount of overflow. This allows for chaining together multiple
27672790
/// multiplications to create "big integers" which represent larger values.
27682791
///
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.
2792+
/// If you don't need the `add` part, then you can use [`Self::carrying_mul`] instead.
27712793
///
27722794
/// # Examples
27732795
///
27742796
/// Please note that this example is shared between integer types,
27752797
/// which explains why `u32` is used here.
27762798
///
27772799
/// ```
2778-
/// #![feature(bigint_helper_methods)]
27792800
/// assert_eq!(5u32.carrying_mul_add(2, 0, 0), (10, 0));
27802801
/// assert_eq!(5u32.carrying_mul_add(2, 10, 10), (30, 0));
27812802
/// assert_eq!(1_000_000_000u32.carrying_mul_add(10, 0, 0), (1410065408, 2));
@@ -2792,8 +2813,6 @@ macro_rules! uint_impl {
27922813
/// using `u8` for simplicity of the demonstration.
27932814
///
27942815
/// ```
2795-
/// #![feature(bigint_helper_methods)]
2796-
///
27972816
/// fn quadratic_mul<const N: usize>(a: [u8; N], b: [u8; N]) -> [u8; N] {
27982817
/// let mut out = [0; N];
27992818
/// for j in 0..N {
@@ -2808,13 +2827,13 @@ macro_rules! uint_impl {
28082827
/// // -1 * -1 == 1
28092828
/// assert_eq!(quadratic_mul([0xFF; 3], [0xFF; 3]), [1, 0, 0]);
28102829
///
2811-
/// assert_eq!(u32::wrapping_mul(0x9e3779b9, 0x7f4a7c15), 0xCFFC982D);
2830+
/// assert_eq!(u32::wrapping_mul(0x9e3779b9, 0x7f4a7c15), 0xcffc982d);
28122831
/// assert_eq!(
28132832
/// quadratic_mul(u32::to_le_bytes(0x9e3779b9), u32::to_le_bytes(0x7f4a7c15)),
2814-
/// u32::to_le_bytes(0xCFFC982D)
2833+
/// u32::to_le_bytes(0xcffc982d)
28152834
/// );
28162835
/// ```
2817-
#[unstable(feature = "bigint_helper_methods", issue = "85532")]
2836+
#[stable(feature = "unsigned_bigint_helpers", since = "CURRENT_RUSTC_VERSION")]
28182837
#[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")]
28192838
#[must_use = "this returns the result of the operation, \
28202839
without modifying the original"]

0 commit comments

Comments
 (0)