aboutsummaryrefslogtreecommitdiff
path: root/examples/rust/Sum_over_array_(Raw_pointers_aligned).rs
blob: 7fc2c2f23c98a58e5df9a6c739b82def75795f80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Compile with -C opt-level=3 -C target-cpu=native to see autovectorization

#[repr(align(64))]
pub struct Aligned<T: ?Sized>(T);

// assumes input is aligned on 64-byte boundary and that
// input's length is a multiple of 64.
pub fn sum_array(input: &Aligned<[i32]>) -> i32 {
    if input.0.len() & 63 != 0 {
        unsafe { std::hint::unreachable_unchecked() }
    }

    (0..input.0.len())
        .map(|i| unsafe { *input.0.as_ptr().add(i) })
        .sum()
}