blob: 7fc2c2f23c98a58e5df9a6c739b82def75795f80 [file] [log] [blame] [raw]
Patrick Quistce648882021-03-11 17:30:03 +01001// Compile with -C opt-level=3 -C target-cpu=native to see autovectorization
2
3#[repr(align(64))]
4pub struct Aligned<T: ?Sized>(T);
5
6// assumes input is aligned on 64-byte boundary and that
7// input's length is a multiple of 64.
8pub fn sum_array(input: &Aligned<[i32]>) -> i32 {
redzic9e27ff22021-05-05 04:52:24 -05009 if input.0.len() & 63 != 0 {
Patrick Quistce648882021-03-11 17:30:03 +010010 unsafe { std::hint::unreachable_unchecked() }
11 }
12
redzic9e27ff22021-05-05 04:52:24 -050013 (0..input.0.len())
14 .map(|i| unsafe { *input.0.as_ptr().add(i) })
15 .sum()
Patrick Quistce648882021-03-11 17:30:03 +010016}