In C/C++, you can use intrinsics for SIMD (such as AVX and AVX2) instructions. Is there a way to use SIMD in Rust?
1 Answers
The answer is yes, with caveats:
- It is available on stable for x86 and x86_64 through the
core::arch
module, reexported asstd::arch
. - Other CPUs require the use of a nightly compiler for now.
- Not all instructions may be available through
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.

- 1
- 1

- 287,565
- 48
- 449
- 722
-
1You don't **need** inline assembly or nightly. You can also use shims compiled from C or direct assembly that are then linked to Rust (see [my cupid crate](https://github.com/shepmaster/cupid/blob/1ac5bfcc783ab59163dbbf0462754e0373f56069/build.rs) for an example). – Shepmaster Mar 22 '17 at 12:03
-
2@Shepmaster: I added a note; but I personally feel that is no longer "SIMD in Rust". Also, any instruction requiring over-aligned arguments will require a lot of care (unless alignment specification is finally available?); and I don't see how FFI can help with that. – Matthieu M. Mar 22 '17 at 12:16
-
1Yeah, one of my other crates has to deal with alignment in a much more manual way. However, in that case it's nicer to do it one level up as I can deal with misaligned data once, then do aligned data in the tight loop. – Shepmaster Mar 22 '17 at 12:33