-
Notifications
You must be signed in to change notification settings - Fork 25
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
Using x86 intrinsics for carry-less multiplication #544
Conversation
let bit = u128::from(rhs[i]); | ||
product ^= bit * (a << i); | ||
} | ||
let a = _mm_set_epi64x( |
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.
Could be wrong but I think this wouldn't work on ARM because it uses different instruction set for carry-less multiplies. Lots of folks use M1 Mac (ARM) so we probably need to support both x86 and ARM and define 3 sets of multiplication instructions: no clmul support (basically what we had before), x86 with clmul support and ARM with clmul support.
In other words, something like this should be enough
#[cfg(all(
target_arch = "x86_64",
target_feature = "pclmulqdq"
))]
fn mul(a: 64, b: 64) -> u128 // or [u64; 2]
{
... Intel intrinsics
}
#[cfg(all(
target_arch="aarch64",
target_feature="neon"
)]
fn mul(a: 64, b: 64) -> u128 // or [u64; 2]
{
... ARM intrinsics
}
#[cfg(not(any(all(
target_arch="aarch64",
target_feature="neon"), all(
target_arch = "x86_64",
target_feature = "pclmulqdq"
)))]
fn mul(a: 64, b: 64) -> u128 // or [u64; 2]
{
no hardware support, standard multiply
}
some reference materials and I would be happy to help if needed:
rust-lang/stdarch#318
https://github.com/geky/gf256/blob/master/src/xmul.rs
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.
This will need feature detection.
We can maybe assume the existence of these instructions and avoid having to deal with cpuid instructions (which are even worse than this), but we at least need to make this conditional on CPU architecture. Otherwise our Mac users will be unable to run this.
I might be able to pick this up, but I can't guarantee that I'll have time for it.
I'd say this is very lo-pri as it: I suggest we just keep this draft around for a while and only pick it up once we run out of higher-impact stuff to do =). |
I HATE THIS.
I give up. After a bunch of Googling, I just can't figure out how to safely convert from a __m128i to something normal like a u128.
It's kind of funny that the actual carryless multiplication function isn't unsafe, it's just casting it back to a value that isn't safe.
I mean, the struct is so simple, it's just:
So if only the struct members were not private, I would just call:
If someone wants to commandeer this PR and find a way to land it, please go for it!