4 ms·
I think relaxing the ordering requirement let's you use simd something like this (semi-pseudo code) (a, b, c, d) = (0, 0, 0, 0); for(int i = 0; i < n; i +=
by im3w1l 2y ago
I think relaxing the ordering requirement let's you use simd something like this (semi-pseudo code)
(a, b, c, d) = (0, 0, 0, 0);
for(int i = 0; i < n; i += 4) {
(a, b, c, d) += (arr[i], arr[i+1], arr[i+2], arr[i+3]);
}
return a + b + c + d;
- mjcohen 2y agoWhat if n is not a multiple of 4? There is a trick I can't recall at the moment for handling this with a case statement on n mod 4 at the end to wrap this up.
- jbboehr 2y agoI assume you're thinking of: https://en.wikipedia.org/wiki/Duff%27s_device https://en.wikipedia.org/wiki/Duff%27s_device
- vlovich123 2y agoAre you sure that the compiler doesn’t autovectorize a simple loop into this form anyway? This is kind of the defacto scenario for that pass.
- im3w1l 2y agoThis transformation is only legal if order of summation doesn't matter. So the compiler can do it for ints, but not floats.
- vlovich123 2y agoApplying std::reduce to floats would be a similarly bad idea for having the compiler do it, except people will write it without thinking. Of course, people always struggle with writing correct floating point so I doubt it makes things better or worse, but it’s conceptually the library equivalent of -fast-math (which coincidentally would also auto-vectorize the loop but can impact more than just that).
- im3w1l 2y agoUsing fast math in one particular expression is way different and less risky than doing it globally.