22.9 SIMD (Single Instruction, Multiple Data)
22.9.1 What Is SIMD?
SIMD enables a CPU to apply the same operation to multiple data elements simultaneously. Modern x86 processors offer SSE/AVX instruction sets, while ARM CPUs provide Neon instructions, and so on. SIMD is especially effective for numerically intensive tasks, such as graphics or scientific computations.
22.9.2 Automatic vs. Manual SIMD in Rust
- Automatic: Sometimes LLVM (the compiler backend) can auto-vectorize loops under the right conditions.
- Manual: Crates like
std::simd
allow explicit control over SIMD operations.
22.9.3 Example
fn sum_of_squares(data: &[f32]) -> f32 { data.iter().map(|x| x * x).sum() } fn main() { let v: Vec<f32> = (0..1_000_000).map(|x| x as f32).collect(); let result = sum_of_squares(&v); println!("Sum of squares = {}", result); }
You may need to benchmark or enable specific compiler flags to see if auto-vectorization kicks in. If it does not, manual SIMD can provide additional speedups.