I would suggest using Vc: portable, zero-overhead C++ types for explicitly data-parallel programming library for simd, I hear it is targeted for inclusion into the C++ standard. It is easier to write and easier to read.
Example:
#include <iostream>
#include <Vc/Vc>
int main() {
using A = Vc::SimdArray<int, 8>;
A arr1 = A::Random();
A arr2 = A::Random();
std::cout << arr1 << '\n';
std::cout << arr2 << '\n';
std::cout << arr1 / arr2 << '\n';
}
Outputs:
<1513634383 -963914658 1763536262 -1285037745 | -695608406 -35372374 1025922083 444041308>
<824703811 1962744590 1568022524 -293901648 | 549806324 248334095 1663905340 641164273>
[1, 0, 1, 4, -1, 0, 0, 0]
The following function
using A = Vc::SimdArray<int, 8>;
__attribute__((noinline)) A f(A a0, A a1) {
return a0 / a1;
}
With g++-8.2 -O3 -march=skylake
translates into the following assembly:
f(Vc_1::SimdArray<int, 8ul, Vc_1::Vector<int, Vc_1::VectorAbi::Avx>, 8ul>, Vc_1::SimdArray<int, 8ul, Vc_1::Vector<int, Vc_1::VectorAbi::Avx>, 8ul>):
vcvtdq2pd ymm3, xmm1
vcvtdq2pd ymm2, xmm0
vextracti128 xmm1, ymm1, 0x1
vextracti128 xmm0, ymm0, 0x1
vcvtdq2pd ymm1, xmm1
vdivpd ymm2, ymm2, ymm3
vcvtdq2pd ymm0, xmm0
vdivpd ymm0, ymm0, ymm1
vcvttpd2dq xmm2, ymm2
vcvttpd2dq xmm0, ymm0
vinserti128 ymm0, ymm2, xmm0, 0x1
ret
Note that there are no simd instructions in the x86 instruction set for integer division.