capypad
0 day streak
rust / expert
Snippet

Data-Level Parallelism with Portable SIMD

Portable SIMD (Single Instruction, Multiple Data) allows you to perform operations on multiple values simultaneously using CPU vector instructions. By packing four f32 values into a single f32x4 register, we can perform additions in parallel, significantly boosting performance for numerical computations and signal processing.

snippet.rs
rust
1
2
3
4
5
6
7
8
9
10
11
#![feature(portable_simd)]
use std::simd::f32x4;
 
fn fast_add(a: [f32; 4], b: [f32; 4]) -> [f32; 4] {
let va = f32x4::from_array(a);
let vb = f32x4::from_array(b);
let sum = va + vb; // Executes a single SIMD instruction (e.g., ADDPS)
sum.to_array()
}
Breakdown
1
use std::simd::f32x4;
Imports a 128-bit vector type containing four 32-bit floats.
2
va + vb
The operator is overloaded to trigger parallel hardware instructions.
3
sum.to_array()
Converts the hardware vector back into a standard Rust array.