22
I need to implement a prefix sum algorithm and would need it to be as fast as possible.
Ex:
[3, 1, 7, 0, 4, 1, 6, 3]
should give:
[3, 4, 11, 11, 15, 16, 22, 25]
Is there a way to do this using SSE SIMD CPU instruction?
My first idea is to sum each pair in parallel recursively until all sum have been computed like below!
//in parallel do
for (int i = 0; i < z.length; i++) {
z[i] = x[i << 1] + x[(i << 1) + 1];
}
To make the algorithm a little bit more clear, z is not the final output, but instead used to compute the output.
int[] w = computePrefixSum(z);
for (int i = 1; i < ouput.length; i++) {
ouput[i] = (i % 2 == 0) ? (x[i] + ouput[i - 1]) : w[(i - 1) >> 1];
}