I'm trying, without succeeding, to make the Rust compiler (rustc 1.66.0
) auto-vectorize the following function (without using any unsafe
intrinsics):
pub fn get_transforms(s: &mut [i32]) -> u32 {
assert_eq!(s.len(), 4);
let mut transforms = 1;
while s[0] > 0 || s[1] > 0 || s[2] > 0 || s[3] > 0 {
// Calculate deltas
let d0 = s[0] - s[1];
let d1 = s[1] - s[2];
let d2 = s[2] - s[3];
let d3 = s[3] - s[0];
// Assign absolute values
s[0] = d0.abs();
s[1] = d1.abs();
s[2] = d2.abs();
s[3] = d3.abs();
transforms += 1;
}
transforms
}
My idea is to make it perform the subtractions and the abs()
once (using 16 byte registers) instead of four times.
I've read that iterators might help, but I haven't found an easy way to use them here. Compiler flags don't seem to able to help either.
Here's the link to the Compiler Explorer output I'm using as reference: https://godbolt.org/z/7sqq6bszT