Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use SIMD instructions in Rust?

Tags:

avx

rust

simd

avx2

In C/C++, you can use intrinsics for SIMD (such as AVX and AVX2) instructions. Is there a way to use SIMD in Rust?

like image 819
pythonic Avatar asked Mar 21 '17 21:03

pythonic


1 Answers

The answer is yes, with caveats:

  • It is available on stable for x86 and x86_64 through the core::arch module, reexported as std::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.

like image 81
Matthieu M. Avatar answered Sep 20 '22 03:09

Matthieu M.