aboutsummaryrefslogtreecommitdiff
path: root/examples/rust/Sum_over_array_(Raw_pointers).rs
blob: 8d7c6049abfb093fe0c6cee9ea0684a9f5d8394a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
// 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()
}