3.23 Conditional statements and efficient vectorization

For efficient vectorization, loops must contain mostly assignment statements and must limit the use of if and switch statements.

Loop invariant conditions are simple conditions that do not change between iterations of the loop. The compiler can move loop invariant conditions before the loop so that they are executed once, rather than on each loop iteration.

The compiler can vectorize more complex conditional operations by computing all pathways in vector mode and merging the results. If there is significant conditional computation, then performance might suffer.

The following example uses conditional statements in a way that is acceptable for vectorization.

float a[99], b[99], c[99];
int i, n;
...
for (i = 0; i < n; i++)
{
    if (c[i] > 0) a[i] = b[i] - 5.0f;
    else a[i] = b[i] * 2.0;
};
Non-ConfidentialPDF file icon PDF versionARM DUI0472M
Copyright © 2010-2016 ARM Limited or its affiliates. All rights reserved.