I'm trying to have VC++ 2012 auto-vectorize a loop that looks a bit like this (there are actually interesting computations going on, but they're elided for the purpose of making the question as to the point as possible).
parameters:
int period;
unsigned char* out_array;
unsigned char* in_array1;
unsigned char* in_array2;
unsigned char* in_array3;
for (int x = 0; x < width; ++x)
{
int index = period * (x / 2);
out_array[0] = in_array1[x];
out_array[1] = in_array2[index];
out_array[2] = in_array3[index];
out_array += 4;
}
I thought the only thing standing in the way of vectorizing was out_array += 4, so I made an inner "unrolled" loop, hoping at least that one could be vectorized:
for (int x = 0; x < width; ++x)
{
for (int xx = 0; xx < 4; ++xx)
{
int index = period * ((xx + x) / 2);
unsigned char* pout_array = out_array + (4 * xx);
pout_array[0] = in_array1[xx + x];
pout_array[1] = in_array2[index];
pout_array[2] = in_array3[index];
}
out_array += 16;
}
But as I run the compiler with /Qvect-report:2, it is telling me the inner loop cannot be vectorized because of error code 1200. Error code 1200 states:
Loop contains loop-carried data dependences that prevent vectorization. Different iterations of the loop interfere with each other such that vectorizing the loop would produce wrong answers, and the auto-vectorizer cannot prove to itself that there are no such data dependences.
I don't understand this. Obviously each iteration of this loop is independent. How can I get Visual Studio to vectorize it?
out_array[0] = in_array1[x];one with the other 2. Then which loop does the compiler complain about? - indeterminately sequencedout_arrayarray is likely non-negligible. That said, I can't say whether it will be better or worse anyway. - Mysticialin_array2andin_array3are being read non-sequentially. You are making hops of sizeperiod * (x / 2)Vectorization really only works with sequential accesses where you don't skip any elements. - Mysticial