In C/C++, you can use intrinsics for SIMD (such as AVX and AVX2) instructions. Is there a way to use SIMD in Rust?
The answer is yes, with caveats:
core::arch
module, reexported as std::arch
.core::arch
, in which case inline assembly is necessary, which also requires a nightly compiler.The std::arch
module only provides CPU instructions as intrinsics, and requires the use of unsafe
blocks as well as specific feature
on the functions containing those instructions to properly align arguments. The documentation of std::arch
is a good starting point for compile-time and run-time detection of CPU features.
As noted in the documentation, higher level APIs will likely be available at some point in the future under std::simd
(and possibly core::simd
); a sneak preview being available in the stdsimd
crate:
Ergonomics
It's important to note that using the
arch
module is not the easiest thing in the world, so if you're curious to try it out you may want to brace yourself for some wordiness!The primary purpose of this module is to enable stable crates on crates.io to build up much more ergonomic abstractions which end up using SIMD under the hood. Over time these abstractions may also move into the standard library itself, but for now this module is tasked with providing the bare minimum necessary to use vendor intrinsics on stable Rust.
Note: you may also possibly use the FFI to link in a library that does so for you; for example Shepmaster's cupid crate uses such a strategy to access cpu features at runtime.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With