I am trying to find sum reduction of 32 elements (each 1 byte data) on an Intel i3 processor. I did this:
s=0;
for (i=0; i<32; i++)
{
s = s + a[i];
}
However, its taking more time, since my application is a real-time application requiring much lesser time. Please note that the final sum could be more than 255.
Is there a way I can implement this using low level SIMD SSE2 instructions? Unfortunately I have never used SSE. I tried searching for sse2 function for this purpose, but it is also not available. Is it (sse) guaranteed to reduce the computation time for such a small-sized problems?
Any suggestions??
Note: I have implemented the similar algorithms using OpenCL and CUDA and that worked great but only when the problem size was big. For small sized problems the cost of overhead was more. Not sure how it works on SSE
This is a bit long-winded but it should still be at least 2x faster than the scalar code:
Note that
a[]
needs to be 16 byte aligned.You can probably improve on the above code using
_mm_hadd_epi16
.You can abuse
PSADBW
to calculate small horizontal sums quickly.Something like this: (not tested)
Attempted intrinsics version:
I never use intrinsics so this code probably makes no sense whatsoever. The disassembly looked OK though.