-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
implement carryless_mul
#152132
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
base: main
Are you sure you want to change the base?
implement carryless_mul
#152132
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -218,3 +218,57 @@ macro_rules! impl_funnel_shifts { | |||||
| impl_funnel_shifts! { | ||||||
| u8, u16, u32, u64, u128, usize | ||||||
| } | ||||||
|
|
||||||
| #[rustc_const_unstable(feature = "core_intrinsics_fallbacks", issue = "none")] | ||||||
| pub const trait CarrylessMul: Copy + 'static { | ||||||
| /// See [`super::carryless_mul`]; we just need the trait indirection to handle | ||||||
| /// different types since calling intrinsics with generics doesn't work. | ||||||
| fn carryless_mul(self, rhs: Self) -> Self; | ||||||
| } | ||||||
|
|
||||||
| macro_rules! impl_carryless_mul{ | ||||||
| ($($type:ident),*) => {$( | ||||||
| /// This approach uses a bitmask of the form `0b100010001...0001` to avoid carry spilling. | ||||||
| /// When carries do occur, they wind up in a "hole" of zeros and are subsequently masked | ||||||
| /// out of the result. | ||||||
|
Comment on lines
+231
to
+233
Contributor
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. This approach with 4-bit digits works up to integers with For The impl for some tests against a naive impl: playground |
||||||
| #[rustc_const_unstable(feature = "core_intrinsics_fallbacks", issue = "none")] | ||||||
| impl const CarrylessMul for $type { | ||||||
| #[inline] | ||||||
| fn carryless_mul(self, rhs: Self) -> Self { | ||||||
| use crate::num::Wrapping; | ||||||
|
|
||||||
| // i.e. 0b100010001...0001 in binary. | ||||||
| const MASK: u128 = 0x1111_1111_1111_1111_1111_1111_1111_1111; | ||||||
|
Member
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.
Suggested change
seems easier to read
Contributor
Author
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. Is it? When I look at that expression I'd have to trust the comment that it does what it says. The |
||||||
|
|
||||||
| let m0 = MASK as $type; | ||||||
| let x = self; | ||||||
| let y = rhs; | ||||||
|
|
||||||
| let m1 = m0 << 1; | ||||||
| let m2 = m1 << 1; | ||||||
| let m3 = m2 << 1; | ||||||
|
|
||||||
| let x0 = Wrapping(x & m0); | ||||||
| let x1 = Wrapping(x & m1); | ||||||
| let x2 = Wrapping(x & m2); | ||||||
| let x3 = Wrapping(x & m3); | ||||||
|
|
||||||
| let y0 = Wrapping(y & m0); | ||||||
| let y1 = Wrapping(y & m1); | ||||||
| let y2 = Wrapping(y & m2); | ||||||
| let y3 = Wrapping(y & m3); | ||||||
|
|
||||||
| let z0 = (x0 * y0) ^ (x1 * y3) ^ (x2 * y2) ^ (x3 * y1); | ||||||
| let z1 = (x0 * y1) ^ (x1 * y0) ^ (x2 * y3) ^ (x3 * y2); | ||||||
| let z2 = (x0 * y2) ^ (x1 * y1) ^ (x2 * y0) ^ (x3 * y3); | ||||||
| let z3 = (x0 * y3) ^ (x1 * y2) ^ (x2 * y1) ^ (x3 * y0); | ||||||
|
|
||||||
| (z0.0 & m0) | (z1.0 & m1) | (z2.0 & m2) | (z3.0 & m3) | ||||||
| } | ||||||
| } | ||||||
| )*}; | ||||||
| } | ||||||
|
|
||||||
| impl_carryless_mul! { | ||||||
| u8, u16, u32, u64, u128, usize | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2179,6 +2179,19 @@ pub const unsafe fn unchecked_funnel_shr<T: [const] fallback::FunnelShift>( | |
| unsafe { a.unchecked_funnel_shr(b, shift) } | ||
| } | ||
|
|
||
| /// Carryless multiply. | ||
|
Member
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. Is this meant to be a self-contained description? To me these words mean nothing.^^ |
||
| /// | ||
| /// Safe versions of this intrinsic are available on the integer primitives | ||
| /// via the `carryless_mul` method. For example, [`u32::carryless_mul`]. | ||
| #[rustc_intrinsic] | ||
| #[rustc_nounwind] | ||
| #[rustc_const_unstable(feature = "uint_carryless_mul", issue = "152080")] | ||
| #[unstable(feature = "uint_carryless_mul", issue = "152080")] | ||
| #[miri::intrinsic_fallback_is_spec] | ||
| pub const fn carryless_mul<T: [const] fallback::CarrylessMul>(a: T, b: T) -> T { | ||
| a.carryless_mul(b) | ||
| } | ||
|
|
||
| /// This is an implementation detail of [`crate::ptr::read`] and should | ||
| /// not be used anywhere else. See its comments for why this exists. | ||
| /// | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,9 @@ macro_rules! uint_impl { | |
| fsh_op = $fsh_op:literal, | ||
| fshl_result = $fshl_result:literal, | ||
| fshr_result = $fshr_result:literal, | ||
| clmul_lhs = $clmul_rhs:literal, | ||
| clmul_rhs = $clmul_lhs:literal, | ||
| clmul_result = $clmul_result:literal, | ||
| swap_op = $swap_op:literal, | ||
| swapped = $swapped:literal, | ||
| reversed = $reversed:literal, | ||
|
|
@@ -482,6 +485,32 @@ macro_rules! uint_impl { | |
| unsafe { intrinsics::unchecked_funnel_shr(self, rhs, n) } | ||
| } | ||
|
|
||
| /// Performs a carry-less multiplication. | ||
| /// | ||
| /// This is similar to long multiplication except that the carry is discarded. | ||
| /// This function wraps, so only the low bits are returned. | ||
| /// | ||
| /// This operation can be used to model multiplication in `GF(2)[X]`, the polynomial | ||
| /// ring over `GF(2)`. | ||
| /// | ||
| /// ``` | ||
| /// #![feature(uint_carryless_mul)] | ||
| /// | ||
| #[doc = concat!("let a = ", $clmul_lhs, stringify!($SelfT), ";")] | ||
| #[doc = concat!("let b = ", $clmul_rhs, stringify!($SelfT), ";")] | ||
| /// | ||
| #[doc = concat!("assert_eq!(a.carryless_mul(b), ", $clmul_result, ");")] | ||
|
Member
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. The only example here uses huge numbers which means the example is entirely useless if one doesn't already know what this operation does. I for one still have no clue after reading all this.^^ Which carry is discarded? All the ones that show up when adding up the results of the first steps of long multiplication? Or also all the carries that occur within the long multiplication steps? This needs way more detail. Doesn't that mean the result depends on the base? No base is mentioed in the first 2 paragraphs, and even in the third paragraph it's only mentioned in an obscure way.
Member
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. maybe describe it as equivalent to: impl uN {
pub fn carryless_mul(self, other: Self) -> Self {
let mut retval = 0;
for i in 0..Self::BITS {
if (other >> i) & 1 != 0 {
retval ^= self << i;
}
}
retval
}
}
Contributor
Author
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. That is a good specification. I haven't really found/been able to come up with a good intuitive explanation, and even if you understand the mechanics, how/when to apply this function is still hard to see.
So in part that is to test that the implementation doesn't just mask off the upper bits. Because of how these functions are generated with a macro, getting examples to work with type inference and without overflowing literals is a bit finicky. I'll try again to come up with something more insightful.
Member
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. Regular long multiplication would be using I have no idea why that would be called "carryless", but that does seem like a reasonable way to explain this, yeah. And maybe we should also do the const-eval/Miri implementation that way to avoid relying on the fallback impl that seems to want to be clever. We generally don't want to be clever for intrinsics in Miri.
Contributor
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.
Exactly. It is carryless in the sense that the sum of two bits (x,y) produces a carry bit The connection to polynomial multiplication is that there you naturally don't carry between the coefficients: |
||
| /// ``` | ||
| #[rustc_const_unstable(feature = "uint_carryless_mul", issue = "152080")] | ||
| #[doc(alias = "clmul")] | ||
| #[unstable(feature = "uint_carryless_mul", issue = "152080")] | ||
| #[must_use = "this returns the result of the operation, \ | ||
| without modifying the original"] | ||
| #[inline(always)] | ||
| pub const fn carryless_mul(self, rhs: Self) -> Self { | ||
| intrinsics::carryless_mul(self, rhs) | ||
| } | ||
|
|
||
| /// Reverses the byte order of the integer. | ||
| /// | ||
| /// # Examples | ||
|
|
||
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.
Since the fallback body is non-trivial, maybe this should be added to the list from #150605 ?