问题
I'm trying to use the Visual C++ 2013 auto-vectorizer to make the following loop vectorized (/arch:AVX2
) but the compiler refuses and gives the following message:
info C5002: loop not vectorized due to reason '1100'
This reason code means
Loop contains control flow—for example, "if" or "?".
I have tried to split the comparisons and the final assignment into a separate loop but that seems inefficient when there are intrinsics available for performing comparisons on floating point values.
Why should the compiler treat comparisons as flow control, and what can I change in the implementation so that the compiler will vectorize this function?
void triplets_positive(
const std::uint64_t count,
double * const a,
double * const b,
double * const c,
std::uint64_t * const all_positive)
{
for (std::uint64_t i = 0; i < count; ++i)
{
// These >= operations make the loop not vectorisable because
// they introduce control flow.
std::uint64_t a_pos = (a[i] >= 0.0);
std::uint64_t b_pos = (b[i] >= 0.0);
std::uint64_t c_pos = (c[i] >= 0.0);
all_positive[i] = a_pos & b_pos & c_pos;
}
}
回答1:
Unfortunately, this appears to be either a bug or limitation in the Visual C++ 2013 compiler. Other compilers make use either of the CMPPD instruction (AVX/AVX2) or CMP*PD instructions (SSE2).
Compilers that successfully vectorise this loop include:
- Visual C++ 2017
- Visual C++ 2015
- Clang + LLVM (
Apple LLVM version 8.1.0 (clang-802.0.42)
)
While it's theoretically possible to write the comparison as bitwise operations, that's counterproductive and the best option is to upgrade to another compiler.
来源:https://stackoverflow.com/questions/45339998/auto-vectorization-of-loop-containing-comparisons