Skip to content

Commit 20f76e0

Browse files
committed
non allocating fold simd
1 parent c99a217 commit 20f76e0

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

crates/core_simd/examples/dot_product.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,40 @@ pub fn dot_prod_simd_3(a: &[f32], b: &[f32]) -> f32 {
108108

109109
sums.reduce_sum()
110110
}
111+
112+
// Finally, we present an iterator version for handling remainders in a scalar fashion at the end of the loop.
113+
// Unfortunately, this is allocating 1 `XMM` register on the order of `~len(a)` - we'll see how we can get around it in the
114+
// next example.
115+
use std::ops::Add;
116+
pub fn dot_prod_simd_4(a: &[f32], b: &[f32]) -> f32 {
117+
let mut sum = a
118+
.array_chunks::<4>()
119+
.map(|&a| f32x4::from_array(a))
120+
.zip(b.array_chunks::<4>().map(|&b| f32x4::from_array(b)))
121+
.map(|(a, b)| a * b)
122+
.fold(f32x4::splat(0.), std::ops::Add)
123+
.reduce_sum();
124+
let remain = a.len() - (a.len() % 4);
125+
sum += a[remain..]
126+
.iter()
127+
.zip(&b[remain..])
128+
.map(|(a, b)| a * b)
129+
.sum();
130+
sum
131+
}
132+
133+
// This version allocates a single `XMM` register for accumulation, and the folds don't allocate on top of that.
134+
// Notice the the use of `mul_add`, which can do a multiply and an add operation ber iteration.
135+
pub fn dot_prod_simd_5(a: &[f32], b: &[f32]) -> f32 {
136+
let mut sum = a
137+
.array_chunks::<4>()
138+
.map(|&a| f32x4::from_array(a))
139+
.zip(b.array_chunks::<4>().map(|&b| f32x4::from_array(b)))
140+
.fold(f32x4::splat(0.), |acc, (a, b)| acc.mul_add(a, b))
141+
.reduce_sum();
142+
sum
143+
}
144+
111145
fn main() {
112146
// Empty main to make cargo happy
113147
}

0 commit comments

Comments
 (0)