-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a compiler intrinsic to back bigint_helper_methods
#133663
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#![unstable( | ||
feature = "core_intrinsics_fallbacks", | ||
reason = "The fallbacks will never be stable, as they exist only to be called \ | ||
by the fallback MIR, but they're exported so they can be tested on \ | ||
platforms where the fallback MIR isn't actually used", | ||
issue = "none" | ||
)] | ||
#![allow(missing_docs)] | ||
|
||
#[const_trait] | ||
pub trait CarryingMulAdd: Copy + 'static { | ||
type Unsigned: Copy + 'static; | ||
fn carrying_mul_add( | ||
self, | ||
multiplicand: Self, | ||
addend: Self, | ||
carry: Self, | ||
) -> (Self::Unsigned, Self); | ||
} | ||
|
||
macro_rules! impl_carrying_mul_add_by_widening { | ||
($($t:ident $u:ident $w:ident,)+) => {$( | ||
#[rustc_const_unstable(feature = "core_intrinsics_fallbacks", issue = "none")] | ||
impl const CarryingMulAdd for $t { | ||
type Unsigned = $u; | ||
#[inline] | ||
fn carrying_mul_add(self, a: Self, b: Self, c: Self) -> ($u, $t) { | ||
let wide = (self as $w) * (a as $w) + (b as $w) + (c as $w); | ||
(wide as _, (wide >> Self::BITS) as _) | ||
} | ||
} | ||
)+}; | ||
} | ||
impl_carrying_mul_add_by_widening! { | ||
u8 u8 u16, | ||
u16 u16 u32, | ||
u32 u32 u64, | ||
u64 u64 u128, | ||
usize usize UDoubleSize, | ||
i8 u8 i16, | ||
i16 u16 i32, | ||
i32 u32 i64, | ||
i64 u64 i128, | ||
isize usize UDoubleSize, | ||
} | ||
|
||
#[cfg(target_pointer_width = "16")] | ||
type UDoubleSize = u32; | ||
#[cfg(target_pointer_width = "32")] | ||
type UDoubleSize = u64; | ||
#[cfg(target_pointer_width = "64")] | ||
type UDoubleSize = u128; | ||
|
||
#[inline] | ||
const fn wide_mul_u128(a: u128, b: u128) -> (u128, u128) { | ||
#[inline] | ||
const fn to_low_high(x: u128) -> [u128; 2] { | ||
const MASK: u128 = u64::MAX as _; | ||
[x & MASK, x >> 64] | ||
} | ||
#[inline] | ||
const fn from_low_high(x: [u128; 2]) -> u128 { | ||
x[0] | (x[1] << 64) | ||
} | ||
#[inline] | ||
const fn scalar_mul(low_high: [u128; 2], k: u128) -> [u128; 3] { | ||
let [x, c] = to_low_high(k * low_high[0]); | ||
let [y, z] = to_low_high(k * low_high[1] + c); | ||
[x, y, z] | ||
} | ||
let a = to_low_high(a); | ||
let b = to_low_high(b); | ||
let low = scalar_mul(a, b[0]); | ||
let high = scalar_mul(a, b[1]); | ||
let r0 = low[0]; | ||
let [r1, c] = to_low_high(low[1] + high[0]); | ||
let [r2, c] = to_low_high(low[2] + high[1] + c); | ||
let r3 = high[2] + c; | ||
(from_low_high([r0, r1]), from_low_high([r2, r3])) | ||
} | ||
|
||
#[rustc_const_unstable(feature = "core_intrinsics_fallbacks", issue = "none")] | ||
impl const CarryingMulAdd for u128 { | ||
type Unsigned = u128; | ||
#[inline] | ||
fn carrying_mul_add(self, b: u128, c: u128, d: u128) -> (u128, u128) { | ||
let (low, mut high) = wide_mul_u128(self, b); | ||
let (low, carry) = u128::overflowing_add(low, c); | ||
high += carry as u128; | ||
let (low, carry) = u128::overflowing_add(low, d); | ||
high += carry as u128; | ||
(low, high) | ||
} | ||
} | ||
|
||
#[rustc_const_unstable(feature = "core_intrinsics_fallbacks", issue = "none")] | ||
impl const CarryingMulAdd for i128 { | ||
type Unsigned = u128; | ||
#[inline] | ||
fn carrying_mul_add(self, b: i128, c: i128, d: i128) -> (u128, i128) { | ||
let (low, high) = wide_mul_u128(self as u128, b as u128); | ||
let mut high = high as i128; | ||
high = high.wrapping_add(i128::wrapping_mul(self >> 127, b)); | ||
high = high.wrapping_add(i128::wrapping_mul(self, b >> 127)); | ||
let (low, carry) = u128::overflowing_add(low, c as u128); | ||
high = high.wrapping_add((carry as i128) + (c >> 127)); | ||
let (low, carry) = u128::overflowing_add(low, d as u128); | ||
high = high.wrapping_add((carry as i128) + (d >> 127)); | ||
(low, high) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,7 @@ use crate::marker::{DiscriminantKind, Tuple}; | |
use crate::mem::SizedTypeProperties; | ||
use crate::{ptr, ub_checks}; | ||
|
||
pub mod fallback; | ||
pub mod mir; | ||
pub mod simd; | ||
|
||
|
@@ -3305,6 +3306,34 @@ pub const fn mul_with_overflow<T: Copy>(_x: T, _y: T) -> (T, bool) { | |
unimplemented!() | ||
} | ||
|
||
/// Performs full-width multiplication and addition with a carry: | ||
/// `multiplier * multiplicand + addend + carry`. | ||
/// | ||
/// This is possible without any overflow. For `uN`: | ||
/// MAX * MAX + MAX + MAX | ||
/// => (2ⁿ-1) × (2ⁿ-1) + (2ⁿ-1) + (2ⁿ-1) | ||
/// => (2²ⁿ - 2ⁿ⁺¹ + 1) + (2ⁿ⁺¹ - 2) | ||
/// => 2²ⁿ - 1 | ||
/// | ||
/// For `iN`, the upper bound is MIN * MIN + MAX + MAX => 2²ⁿ⁻² + 2ⁿ - 2, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pop Quiz: after a double-width Answer: (The |
||
/// and the lower bound is MAX * MIN + MIN + MIN => -2²ⁿ⁻² - 2ⁿ + 2ⁿ⁺¹. | ||
/// | ||
/// This currently supports unsigned integers *only*, no signed ones. | ||
/// The stabilized versions of this intrinsic are available on integers. | ||
#[unstable(feature = "core_intrinsics", issue = "none")] | ||
#[rustc_const_unstable(feature = "const_carrying_mul_add", issue = "85532")] | ||
#[rustc_nounwind] | ||
#[cfg_attr(not(bootstrap), rustc_intrinsic)] | ||
#[cfg_attr(not(bootstrap), miri::intrinsic_fallback_is_spec)] | ||
pub const fn carrying_mul_add<T: ~const fallback::CarryingMulAdd<Unsigned = U>, U>( | ||
multiplier: T, | ||
multiplicand: T, | ||
addend: T, | ||
carry: T, | ||
) -> (U, T) { | ||
multiplier.carrying_mul_add(multiplicand, addend, carry) | ||
} | ||
|
||
/// Performs an exact division, resulting in undefined behavior where | ||
/// `x % y != 0` or `y == 0` or `x == T::MIN && y == -1` | ||
/// | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just comparing to my version, this doesn't really matter that much, but
const_undef
feels slightly more accurate here just semantically. Not actually sure if this affects codegen though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
poison
is always preferred where possible, because it doesn't have the same complications asundef
.(undef << 1) & 1
is guaranteed to be0
, but(poison << 1) & 1
is still poison. You'll also see that both what we emit for tuples and what the optimizer does for tuples both usepoison
for this: https://rust.godbolt.org/z/WTnW4GPE6