I’m in the process of optimizing my code using SSE3. There’s one point in the code that is forcing me to shift all of the elements in an vector by one element
v[0] = 0 //v is some char* and N = v.size()
for(int i = 1;i<N;i++){
v[i] = v[i-1];
}
As far as I can tell, SSE doesn’t support vector shifting, so I’ll have to code this one from scratch.
But then I had the idea, what if I just decrement the pointer.
v = (v-1);
v[0] = 0;
In this way, the operation will be constant won’t require any operations at all.
I’ve already tested this and it works for my test program.
However, I’m not sure that this operation is safe.
Is this a really dumb idea?
SSEdoes support shifting, either bitwise shifting of the elements inside a vector and shifting of whole registers along byte boundaries too.Assuming your vector is of type 16 times
uint8_t, the operation you are looking for iswith the intrinsic
To your first question: As long as
vis a pointer to char, decrementing or incrementing it is completely safe. Dereferencing may not, that depends on your program.To your second question: Yes, it looks like a dumb idea. If you try to optimize with
SSEand you perform some tasks with pointers to bytes you are most likely doing something wrong, and you are calling for trouble if you try to load 16 of yourvinto aSSEregister – either segfaults because of misalignment or a performance penalty because of forcing the compiler to usemovdqu.