Patrick Quist | ce64888 | 2021-03-11 17:30:03 +0100 | [diff] [blame] | 1 | // Compile with -C opt-level=3 -C target-cpu=native to see autovectorization |
| 2 | |
| 3 | #[repr(align(64))] |
| 4 | pub 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. |
| 8 | pub fn sum_array(input: &Aligned<[i32]>) -> i32 { |
redzic | 9e27ff2 | 2021-05-05 04:52:24 -0500 | [diff] [blame] | 9 | if input.0.len() & 63 != 0 { |
Patrick Quist | ce64888 | 2021-03-11 17:30:03 +0100 | [diff] [blame] | 10 | unsafe { std::hint::unreachable_unchecked() } |
| 11 | } |
| 12 | |
redzic | 9e27ff2 | 2021-05-05 04:52:24 -0500 | [diff] [blame] | 13 | (0..input.0.len()) |
| 14 | .map(|i| unsafe { *input.0.as_ptr().add(i) }) |
| 15 | .sum() |
Patrick Quist | ce64888 | 2021-03-11 17:30:03 +0100 | [diff] [blame] | 16 | } |