Skip to content

Commit 8ac4690

Browse files
committed
Partial-stabilize the basics from bigint_helper_methods
1 parent a7a1618 commit 8ac4690

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

library/core/src/num/uint_macros.rs

Lines changed: 36 additions & 20 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 = "min_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"]
@@ -2625,6 +2623,9 @@ macro_rules! uint_impl {
26252623
/// indicating whether an arithmetic overflow would occur. If an
26262624
/// overflow would have occurred then the wrapped value is returned.
26272625
///
2626+
/// If you want the *value* of the overflow, rather than just *whether*
2627+
/// an overflow occurred, see [`Self::carrying_mul`].
2628+
///
26282629
/// # Examples
26292630
///
26302631
/// Please note that this example is shared between integer types.
@@ -2644,25 +2645,41 @@ macro_rules! uint_impl {
26442645
(a as Self, b)
26452646
}
26462647

2647-
/// Calculates the complete product `self * rhs` without the possibility to overflow.
2648+
/// Calculates the complete double-width product `self * rhs`.
26482649
///
26492650
/// This returns the low-order (wrapping) bits and the high-order (overflow) bits
2650-
/// of the result as two separate values, in that order.
2651+
/// of the result as two separate values, in that order. As such,
2652+
/// `a.widening_mul(b).0` produces the same result as `a.wrapping_mul(b)`.
26512653
///
2652-
/// If you also need to add a carry to the wide result, then you want
2653-
/// [`Self::carrying_mul`] instead.
2654+
/// If you also need to add a value and/or carry to the wide result, then you want
2655+
/// [`Self::carrying_mul_add`] instead.
2656+
///
2657+
/// If you just want to know *whether* the multiplication overflowed, then you
2658+
/// want [`Self::overflowing_mul`] instead.
26542659
///
26552660
/// # Examples
26562661
///
2662+
/// ```
2663+
#[doc = concat!("assert_eq!(5_", stringify!($SelfT), ".widening_mul(7), (35, 0));")]
2664+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.widening_mul(", stringify!($SelfT), "::MAX), (1, ", stringify!($SelfT), "::MAX - 1));")]
2665+
/// ```
2666+
///
2667+
/// Compared to other `*_mul` methods:
2668+
/// ```
2669+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::widening_mul(1 << ", stringify!($BITS_MINUS_ONE), ", 6), (0, 3));")]
2670+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::overflowing_mul(1 << ", stringify!($BITS_MINUS_ONE), ", 6), (0, true));")]
2671+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::wrapping_mul(1 << ", stringify!($BITS_MINUS_ONE), ", 6), 0);")]
2672+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::checked_mul(1 << ", stringify!($BITS_MINUS_ONE), ", 6), None);")]
2673+
/// ```
2674+
///
26572675
/// Please note that this example is shared between integer types.
26582676
/// Which explains why `u32` is used here.
26592677
///
26602678
/// ```
2661-
/// #![feature(bigint_helper_methods)]
26622679
/// assert_eq!(5u32.widening_mul(2), (10, 0));
26632680
/// assert_eq!(1_000_000_000u32.widening_mul(10), (1410065408, 2));
26642681
/// ```
2665-
#[unstable(feature = "bigint_helper_methods", issue = "85532")]
2682+
#[stable(feature = "min_bigint_helpers", since = "CURRENT_RUSTC_VERSION")]
26662683
#[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")]
26672684
#[must_use = "this returns the result of the operation, \
26682685
without modifying the original"]
@@ -2756,26 +2773,27 @@ macro_rules! uint_impl {
27562773
Self::carrying_mul_add(self, rhs, carry, 0)
27572774
}
27582775

2759-
/// Calculates the "full multiplication" `self * rhs + carry1 + carry2`
2760-
/// without the possibility to overflow.
2776+
/// Calculates the "full multiplication" `self * rhs + carry1 + carry2`.
27612777
///
27622778
/// This returns the low-order (wrapping) bits and the high-order (overflow) bits
27632779
/// of the result as two separate values, in that order.
27642780
///
2781+
/// This cannot overflow, as the double-width result has exactly enough
2782+
/// space for the largest possible result. This is equivalent to how, in
2783+
/// decimal, 9 × 9 + 9 + 9 = 81 + 18 = 99 = 9×10⁰ + 9×10¹ = 10² - 1.
2784+
///
27652785
/// Performs "long multiplication" which takes in an extra amount to add, and may return an
27662786
/// additional amount of overflow. This allows for chaining together multiple
27672787
/// multiplications to create "big integers" which represent larger values.
27682788
///
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.
2789+
/// If you don't need either `carry`, then you can use [`Self::widening_mul`] instead.
27712790
///
27722791
/// # Examples
27732792
///
27742793
/// Please note that this example is shared between integer types,
27752794
/// which explains why `u32` is used here.
27762795
///
27772796
/// ```
2778-
/// #![feature(bigint_helper_methods)]
27792797
/// assert_eq!(5u32.carrying_mul_add(2, 0, 0), (10, 0));
27802798
/// assert_eq!(5u32.carrying_mul_add(2, 10, 10), (30, 0));
27812799
/// assert_eq!(1_000_000_000u32.carrying_mul_add(10, 0, 0), (1410065408, 2));
@@ -2792,8 +2810,6 @@ macro_rules! uint_impl {
27922810
/// using `u8` for simplicity of the demonstration.
27932811
///
27942812
/// ```
2795-
/// #![feature(bigint_helper_methods)]
2796-
///
27972813
/// fn quadratic_mul<const N: usize>(a: [u8; N], b: [u8; N]) -> [u8; N] {
27982814
/// let mut out = [0; N];
27992815
/// for j in 0..N {
@@ -2808,13 +2824,13 @@ macro_rules! uint_impl {
28082824
/// // -1 * -1 == 1
28092825
/// assert_eq!(quadratic_mul([0xFF; 3], [0xFF; 3]), [1, 0, 0]);
28102826
///
2811-
/// assert_eq!(u32::wrapping_mul(0x9e3779b9, 0x7f4a7c15), 0xCFFC982D);
2827+
/// assert_eq!(u32::wrapping_mul(0x9e3779b9, 0x7f4a7c15), 0xcffc982d);
28122828
/// assert_eq!(
28132829
/// quadratic_mul(u32::to_le_bytes(0x9e3779b9), u32::to_le_bytes(0x7f4a7c15)),
2814-
/// u32::to_le_bytes(0xCFFC982D)
2830+
/// u32::to_le_bytes(0xcffc982d)
28152831
/// );
28162832
/// ```
2817-
#[unstable(feature = "bigint_helper_methods", issue = "85532")]
2833+
#[stable(feature = "min_bigint_helpers", since = "CURRENT_RUSTC_VERSION")]
28182834
#[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")]
28192835
#[must_use = "this returns the result of the operation, \
28202836
without modifying the original"]

0 commit comments

Comments
 (0)