diff options
| author | miguel raz <miguelraz@gmail.com> | 2021-06-04 14:24:47 -0500 |
|---|---|---|
| committer | The Atelier <workingjubilee@gmail.com> | 2022-12-03 16:12:00 -0800 |
| commit | df3a63906c44b23de7065d60c20bf99e2571ccc8 (patch) | |
| tree | bfbc94d1611ccc13064ae19aece139bcd48c00f5 | |
| parent | 1547dd66f9c3c2ba9e4998f8d4885e4f7bd62c52 (diff) | |
| download | rust-df3a63906c44b23de7065d60c20bf99e2571ccc8.tar.gz rust-df3a63906c44b23de7065d60c20bf99e2571ccc8.zip | |
add dot_product example
| -rw-r--r-- | crates/core_simd/examples/dot_product.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/crates/core_simd/examples/dot_product.rs b/crates/core_simd/examples/dot_product.rs new file mode 100644 index 00000000000..812b0b23eeb --- /dev/null +++ b/crates/core_simd/examples/dot_product.rs @@ -0,0 +1,31 @@ +// Code taken from the `packed_simd` crate +// Run this code with `cargo test --example dot_product` +#![feature(array_chunks)] +use core_simd::*; + +pub fn dot_prod(a: &[f32], b: &[f32]) -> f32 { + assert_eq!(a.len(), b.len()); + + // TODO handle remainder when a.len() % 4 != 0 + a.array_chunks::<4>() + .map(|&a| f32x4::from_array(a)) + .zip(b.array_chunks::<4>().map(|&b| f32x4::from_array(b))) + .map(|(a, b)| (a * b).horizontal_sum()) + .sum() +} + +fn main() { + // Empty main to make cargo happy +} + +#[cfg(test)] +mod tests { + #[test] + fn test() { + use super::*; + let a: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]; + let b: Vec<f32> = vec![-8.0, -7.0, -6.0, -5.0, 4.0, 3.0, 2.0, 1.0]; + + assert_eq!(0.0, dot_prod(&a, &b)); + } +} |
