Skip to content

Commit ff49176

Browse files
scottmcmgitbot
authored and
gitbot
committed
Move {widening, carrying}_mul to an intrinsic with fallback MIR
Including implementing it for `u128`, so it can be defined in `uint_impl!`. This way it works for all backends, including CTFE.
1 parent 5577537 commit ff49176

File tree

7 files changed

+316
-135
lines changed

7 files changed

+316
-135
lines changed

core/src/intrinsics/fallback.rs

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#![unstable(
2+
feature = "core_intrinsics_fallbacks",
3+
reason = "The fallbacks will never be stable, as they exist only to be called \
4+
by the fallback MIR, but they're exported so they can be tested on \
5+
platforms where the fallback MIR isn't actually used",
6+
issue = "none"
7+
)]
8+
#![allow(missing_docs)]
9+
10+
#[const_trait]
11+
pub trait CarryingMulAdd: Copy + 'static {
12+
type Unsigned: Copy + 'static;
13+
fn carrying_mul_add(
14+
self,
15+
multiplicand: Self,
16+
addend: Self,
17+
carry: Self,
18+
) -> (Self::Unsigned, Self);
19+
}
20+
21+
macro_rules! impl_carrying_mul_add_by_widening {
22+
($($t:ident $u:ident $w:ident,)+) => {$(
23+
#[rustc_const_unstable(feature = "core_intrinsics_fallbacks", issue = "none")]
24+
impl const CarryingMulAdd for $t {
25+
type Unsigned = $u;
26+
#[inline]
27+
fn carrying_mul_add(self, a: Self, b: Self, c: Self) -> ($u, $t) {
28+
let wide = (self as $w) * (a as $w) + (b as $w) + (c as $w);
29+
(wide as _, (wide >> Self::BITS) as _)
30+
}
31+
}
32+
)+};
33+
}
34+
impl_carrying_mul_add_by_widening! {
35+
u8 u8 u16,
36+
u16 u16 u32,
37+
u32 u32 u64,
38+
u64 u64 u128,
39+
usize usize UDoubleSize,
40+
i8 u8 i16,
41+
i16 u16 i32,
42+
i32 u32 i64,
43+
i64 u64 i128,
44+
isize usize UDoubleSize,
45+
}
46+
47+
#[cfg(target_pointer_width = "16")]
48+
type UDoubleSize = u32;
49+
#[cfg(target_pointer_width = "32")]
50+
type UDoubleSize = u64;
51+
#[cfg(target_pointer_width = "64")]
52+
type UDoubleSize = u128;
53+
54+
#[inline]
55+
const fn wide_mul_u128(a: u128, b: u128) -> (u128, u128) {
56+
#[inline]
57+
const fn to_low_high(x: u128) -> [u128; 2] {
58+
const MASK: u128 = u64::MAX as _;
59+
[x & MASK, x >> 64]
60+
}
61+
#[inline]
62+
const fn from_low_high(x: [u128; 2]) -> u128 {
63+
x[0] | (x[1] << 64)
64+
}
65+
#[inline]
66+
const fn scalar_mul(low_high: [u128; 2], k: u128) -> [u128; 3] {
67+
let [x, c] = to_low_high(k * low_high[0]);
68+
let [y, z] = to_low_high(k * low_high[1] + c);
69+
[x, y, z]
70+
}
71+
let a = to_low_high(a);
72+
let b = to_low_high(b);
73+
let low = scalar_mul(a, b[0]);
74+
let high = scalar_mul(a, b[1]);
75+
let r0 = low[0];
76+
let [r1, c] = to_low_high(low[1] + high[0]);
77+
let [r2, c] = to_low_high(low[2] + high[1] + c);
78+
let r3 = high[2] + c;
79+
(from_low_high([r0, r1]), from_low_high([r2, r3]))
80+
}
81+
82+
#[rustc_const_unstable(feature = "core_intrinsics_fallbacks", issue = "none")]
83+
impl const CarryingMulAdd for u128 {
84+
type Unsigned = u128;
85+
#[inline]
86+
fn carrying_mul_add(self, b: u128, c: u128, d: u128) -> (u128, u128) {
87+
let (low, mut high) = wide_mul_u128(self, b);
88+
let (low, carry) = u128::overflowing_add(low, c);
89+
high += carry as u128;
90+
let (low, carry) = u128::overflowing_add(low, d);
91+
high += carry as u128;
92+
(low, high)
93+
}
94+
}
95+
96+
#[rustc_const_unstable(feature = "core_intrinsics_fallbacks", issue = "none")]
97+
impl const CarryingMulAdd for i128 {
98+
type Unsigned = u128;
99+
#[inline]
100+
fn carrying_mul_add(self, b: i128, c: i128, d: i128) -> (u128, i128) {
101+
let (low, high) = wide_mul_u128(self as u128, b as u128);
102+
let mut high = high as i128;
103+
high = high.wrapping_add((self >> 127) * b);
104+
high = high.wrapping_add(self * (b >> 127));
105+
let (low, carry) = u128::overflowing_add(low, c as u128);
106+
high = high.wrapping_add((carry as i128) + (c >> 127));
107+
let (low, carry) = u128::overflowing_add(low, d as u128);
108+
high = high.wrapping_add((carry as i128) + (d >> 127));
109+
(low, high)
110+
}
111+
}

core/src/intrinsics/mod.rs

+29
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ use crate::marker::{DiscriminantKind, Tuple};
6868
use crate::mem::SizedTypeProperties;
6969
use crate::{ptr, ub_checks};
7070

71+
pub mod fallback;
7172
pub mod mir;
7273
pub mod simd;
7374

@@ -3305,6 +3306,34 @@ pub const fn mul_with_overflow<T: Copy>(_x: T, _y: T) -> (T, bool) {
33053306
unimplemented!()
33063307
}
33073308

3309+
/// Performs full-width multiplication and addition with a carry:
3310+
/// `multiplier * multiplicand + addend + carry`.
3311+
///
3312+
/// This is possible without any overflow. For `uN`:
3313+
/// MAX * MAX + MAX + MAX
3314+
/// => (2ⁿ-1) × (2ⁿ-1) + (2ⁿ-1) + (2ⁿ-1)
3315+
/// => (2²ⁿ - 2ⁿ⁺¹ + 1) + (2ⁿ⁺¹ - 2)
3316+
/// => 2²ⁿ - 1
3317+
///
3318+
/// For `iN`, the upper bound is MIN * MIN + MAX + MAX => 2²ⁿ⁻² + 2ⁿ - 2,
3319+
/// and the lower bound is MAX * MIN + MIN + MIN => -2²ⁿ⁻² - 2ⁿ + 2ⁿ⁺¹.
3320+
///
3321+
/// This currently supports unsigned integers *only*, no signed ones.
3322+
/// The stabilized versions of this intrinsic are available on integers.
3323+
#[unstable(feature = "core_intrinsics", issue = "none")]
3324+
#[rustc_const_unstable(feature = "const_carrying_mul_add", issue = "85532")]
3325+
#[rustc_nounwind]
3326+
#[cfg_attr(not(bootstrap), rustc_intrinsic)]
3327+
#[cfg_attr(not(bootstrap), miri::intrinsic_fallback_is_spec)]
3328+
pub const fn carrying_mul_add<T: ~const fallback::CarryingMulAdd<Unsigned = U>, U>(
3329+
multiplier: T,
3330+
multiplicand: T,
3331+
addend: T,
3332+
carry: T,
3333+
) -> (U, T) {
3334+
multiplier.carrying_mul_add(multiplicand, addend, carry)
3335+
}
3336+
33083337
/// Performs an exact division, resulting in undefined behavior where
33093338
/// `x % y != 0` or `y == 0` or `x == T::MIN && y == -1`
33103339
///

core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
#![cfg_attr(bootstrap, feature(do_not_recommend))]
111111
#![feature(array_ptr_get)]
112112
#![feature(asm_experimental_arch)]
113+
#![feature(const_carrying_mul_add)]
113114
#![feature(const_eval_select)]
114115
#![feature(const_typed_swap)]
115116
#![feature(core_intrinsics)]

core/src/num/mod.rs

-135
Original file line numberDiff line numberDiff line change
@@ -228,134 +228,6 @@ macro_rules! midpoint_impl {
228228
};
229229
}
230230

231-
macro_rules! widening_impl {
232-
($SelfT:ty, $WideT:ty, $BITS:literal, unsigned) => {
233-
/// Calculates the complete product `self * rhs` without the possibility to overflow.
234-
///
235-
/// This returns the low-order (wrapping) bits and the high-order (overflow) bits
236-
/// of the result as two separate values, in that order.
237-
///
238-
/// If you also need to add a carry to the wide result, then you want
239-
/// [`Self::carrying_mul`] instead.
240-
///
241-
/// # Examples
242-
///
243-
/// Basic usage:
244-
///
245-
/// Please note that this example is shared between integer types.
246-
/// Which explains why `u32` is used here.
247-
///
248-
/// ```
249-
/// #![feature(bigint_helper_methods)]
250-
/// assert_eq!(5u32.widening_mul(2), (10, 0));
251-
/// assert_eq!(1_000_000_000u32.widening_mul(10), (1410065408, 2));
252-
/// ```
253-
#[unstable(feature = "bigint_helper_methods", issue = "85532")]
254-
#[must_use = "this returns the result of the operation, \
255-
without modifying the original"]
256-
#[inline]
257-
pub const fn widening_mul(self, rhs: Self) -> (Self, Self) {
258-
// note: longer-term this should be done via an intrinsic,
259-
// but for now we can deal without an impl for u128/i128
260-
// SAFETY: overflow will be contained within the wider types
261-
let wide = unsafe { (self as $WideT).unchecked_mul(rhs as $WideT) };
262-
(wide as $SelfT, (wide >> $BITS) as $SelfT)
263-
}
264-
265-
/// Calculates the "full multiplication" `self * rhs + carry`
266-
/// without the possibility to overflow.
267-
///
268-
/// This returns the low-order (wrapping) bits and the high-order (overflow) bits
269-
/// of the result as two separate values, in that order.
270-
///
271-
/// Performs "long multiplication" which takes in an extra amount to add, and may return an
272-
/// additional amount of overflow. This allows for chaining together multiple
273-
/// multiplications to create "big integers" which represent larger values.
274-
///
275-
/// If you don't need the `carry`, then you can use [`Self::widening_mul`] instead.
276-
///
277-
/// # Examples
278-
///
279-
/// Basic usage:
280-
///
281-
/// Please note that this example is shared between integer types.
282-
/// Which explains why `u32` is used here.
283-
///
284-
/// ```
285-
/// #![feature(bigint_helper_methods)]
286-
/// assert_eq!(5u32.carrying_mul(2, 0), (10, 0));
287-
/// assert_eq!(5u32.carrying_mul(2, 10), (20, 0));
288-
/// assert_eq!(1_000_000_000u32.carrying_mul(10, 0), (1410065408, 2));
289-
/// assert_eq!(1_000_000_000u32.carrying_mul(10, 10), (1410065418, 2));
290-
#[doc = concat!("assert_eq!(",
291-
stringify!($SelfT), "::MAX.carrying_mul(", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX), ",
292-
"(0, ", stringify!($SelfT), "::MAX));"
293-
)]
294-
/// ```
295-
///
296-
/// This is the core operation needed for scalar multiplication when
297-
/// implementing it for wider-than-native types.
298-
///
299-
/// ```
300-
/// #![feature(bigint_helper_methods)]
301-
/// fn scalar_mul_eq(little_endian_digits: &mut Vec<u16>, multiplicand: u16) {
302-
/// let mut carry = 0;
303-
/// for d in little_endian_digits.iter_mut() {
304-
/// (*d, carry) = d.carrying_mul(multiplicand, carry);
305-
/// }
306-
/// if carry != 0 {
307-
/// little_endian_digits.push(carry);
308-
/// }
309-
/// }
310-
///
311-
/// let mut v = vec![10, 20];
312-
/// scalar_mul_eq(&mut v, 3);
313-
/// assert_eq!(v, [30, 60]);
314-
///
315-
/// assert_eq!(0x87654321_u64 * 0xFEED, 0x86D3D159E38D);
316-
/// let mut v = vec![0x4321, 0x8765];
317-
/// scalar_mul_eq(&mut v, 0xFEED);
318-
/// assert_eq!(v, [0xE38D, 0xD159, 0x86D3]);
319-
/// ```
320-
///
321-
/// If `carry` is zero, this is similar to [`overflowing_mul`](Self::overflowing_mul),
322-
/// except that it gives the value of the overflow instead of just whether one happened:
323-
///
324-
/// ```
325-
/// #![feature(bigint_helper_methods)]
326-
/// let r = u8::carrying_mul(7, 13, 0);
327-
/// assert_eq!((r.0, r.1 != 0), u8::overflowing_mul(7, 13));
328-
/// let r = u8::carrying_mul(13, 42, 0);
329-
/// assert_eq!((r.0, r.1 != 0), u8::overflowing_mul(13, 42));
330-
/// ```
331-
///
332-
/// The value of the first field in the returned tuple matches what you'd get
333-
/// by combining the [`wrapping_mul`](Self::wrapping_mul) and
334-
/// [`wrapping_add`](Self::wrapping_add) methods:
335-
///
336-
/// ```
337-
/// #![feature(bigint_helper_methods)]
338-
/// assert_eq!(
339-
/// 789_u16.carrying_mul(456, 123).0,
340-
/// 789_u16.wrapping_mul(456).wrapping_add(123),
341-
/// );
342-
/// ```
343-
#[unstable(feature = "bigint_helper_methods", issue = "85532")]
344-
#[must_use = "this returns the result of the operation, \
345-
without modifying the original"]
346-
#[inline]
347-
pub const fn carrying_mul(self, rhs: Self, carry: Self) -> (Self, Self) {
348-
// note: longer-term this should be done via an intrinsic,
349-
// but for now we can deal without an impl for u128/i128
350-
// SAFETY: overflow will be contained within the wider types
351-
let wide = unsafe {
352-
(self as $WideT).unchecked_mul(rhs as $WideT).unchecked_add(carry as $WideT)
353-
};
354-
(wide as $SelfT, (wide >> $BITS) as $SelfT)
355-
}
356-
};
357-
}
358-
359231
impl i8 {
360232
int_impl! {
361233
Self = i8,
@@ -576,7 +448,6 @@ impl u8 {
576448
from_xe_bytes_doc = u8_xe_bytes_doc!(),
577449
bound_condition = "",
578450
}
579-
widening_impl! { u8, u16, 8, unsigned }
580451
midpoint_impl! { u8, u16, unsigned }
581452

582453
/// Checks if the value is within the ASCII range.
@@ -1192,7 +1063,6 @@ impl u16 {
11921063
from_xe_bytes_doc = "",
11931064
bound_condition = "",
11941065
}
1195-
widening_impl! { u16, u32, 16, unsigned }
11961066
midpoint_impl! { u16, u32, unsigned }
11971067

11981068
/// Checks if the value is a Unicode surrogate code point, which are disallowed values for [`char`].
@@ -1240,7 +1110,6 @@ impl u32 {
12401110
from_xe_bytes_doc = "",
12411111
bound_condition = "",
12421112
}
1243-
widening_impl! { u32, u64, 32, unsigned }
12441113
midpoint_impl! { u32, u64, unsigned }
12451114
}
12461115

@@ -1264,7 +1133,6 @@ impl u64 {
12641133
from_xe_bytes_doc = "",
12651134
bound_condition = "",
12661135
}
1267-
widening_impl! { u64, u128, 64, unsigned }
12681136
midpoint_impl! { u64, u128, unsigned }
12691137
}
12701138

@@ -1314,7 +1182,6 @@ impl usize {
13141182
from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
13151183
bound_condition = " on 16-bit targets",
13161184
}
1317-
widening_impl! { usize, u32, 16, unsigned }
13181185
midpoint_impl! { usize, u32, unsigned }
13191186
}
13201187

@@ -1339,7 +1206,6 @@ impl usize {
13391206
from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
13401207
bound_condition = " on 32-bit targets",
13411208
}
1342-
widening_impl! { usize, u64, 32, unsigned }
13431209
midpoint_impl! { usize, u64, unsigned }
13441210
}
13451211

@@ -1364,7 +1230,6 @@ impl usize {
13641230
from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
13651231
bound_condition = " on 64-bit targets",
13661232
}
1367-
widening_impl! { usize, u128, 64, unsigned }
13681233
midpoint_impl! { usize, u128, unsigned }
13691234
}
13701235

0 commit comments

Comments
 (0)