Say, I have a C++ function:
int arr_1[1000];
double arr_2[1000];
// arr_1 and arr_2 to be assigned values.
// This step is neglected. Not important.
double val(int i){
int a = arr_1[i];
int b = arr_1[i+1];
if (a == b) {
return 0.0;
}
else {
// arr_2 is properly designed so not to worry about any kind of leakage.
return arr_2[a] * arr_2[b];
}
}
Is there an efficient way to vectorize this function under the SIMD (Single instruction, multiple data) framework? I work on apple M1 chip so the right library is #include <arm_neon.h>
since apple chips do not support AVX.
Auto-vectorisation is not done by the complier under -o3
flag due to that if/else
logic and also the array slicing in arr_3
.
I could not think of a way that could completely avoid the for loop. Since I have a if/else
logic so I always need a for loop to do comparison use vcgtq_f32
from arm_neon.h
. That then boils everything down to not doing any kind of vectorisation.
Tony Shi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1