Files
compiler-explorer/examples/rust/Sum_over_array_(Raw_pointers).rs
redzic 9e27ff27fb Make Rust examples more idiomatic (#2634)
* Make Rust examples more idiomatic

* Make 'non-idiomatic' example match the aligned version

* Rename examples
2021-05-05 11:52:24 +02:00

14 lines
397 B
Rust

// Compile with -C opt-level=3 -C target-cpu=native to see autovectorization
// assumes input is aligned on 64-byte boundary and that
// input's length is a multiple of 64.
pub fn sum_array(input: &[i32]) -> i32 {
if input.len() & 63 != 0 {
unsafe { std::hint::unreachable_unchecked() }
}
(0..input.len())
.map(|i| unsafe { *input.as_ptr().add(i) })
.sum()
}