22.8 SIMD (Single Instruction, Multiple Data)
SIMD operations let a single instruction process multiple data points at once. They’re useful for tasks like image processing or numeric loops.
22.8.1 Automatic vs. Manual SIMD
- Automatic: LLVM may auto-vectorize loops with high optimization settings (
-C opt-level=3
), depending on heuristics. - Manual: You can use portable-simd of Rust’s standard library or other crates.
22.8.2 Example of Manual SIMD
Portable_simd requires still the nightly compiler.
#![feature(portable_simd)] use std::simd::f32x4; fn main() { let a = f32x4::splat(10.0); let b = f32x4::from_array([1.0, 2.0, 3.0, 4.0]); println!("{:?}", a + b); }
Explanation: We construct our SIMD vectors with methods like splat or from_array. Next, we can use operators like + on them, and the appropriate SIMD instructions will be carried out.
For details see Portable-simd and the Guide.