I don't really see any explanation of why you'd want to do this? Not only is there a instruction for doing exactly this in most vector instruction sets, but you typically don't need to do it inside your hot loop anyway.
I don't really see any explanation of why you'd want to do this? Not only is there a instruction for doing exactly this in most vector instruction sets, but you typically don't need to do it inside your hot loop anyway.
"Reduce" or "fold" is a standard term for applying a binary operation (like +) to all elements of a vector/list/more general data structure: https://en.wikipedia.org/wiki/Fold_(higher-order_function)
> Not only is there a instruction for doing exactly this in most vector instruction sets
Is there a single one in AVX for adding all the components of a vector register? Is it fast? How does it associate the computation? If there is such an instruction, GCC and Clang seem reluctant to emit it: https://gcc.godbolt.org/z/vkaeYi though I might be missing some magic flags.
edit:
apparently GCC doesn't specifically optimize reductions [2] and does a generic vectorization instead.
vhaddpd %xmm0, %xmm0, %xmm0
$ gcc --version | head -1
gcc (Debian 8.3.0-6) 8.3.0
$ cat x.c
#include <x86intrin.h>
double f(__m128d v){return v[1]+v[0];}
$ gcc -Ofast -march=haswell -S x.c
$ grep -C1 hadd x.s
.cfi_startproc
vhaddpd %xmm0, %xmm0, %xmm0
ret
GCC 4.8.5 with adjusted options does the same, and 7.5, so perhaps there was some regression in early 8.(-ffast-math is redundant with -Ofast.)