Developer Guide and Reference


Loop Constructs

Loops can be formed with the usual
Loops must have a single entry and a single exit to be vectorized. The following examples illustrate loop constructs that can and cannot be vectorized.
Example: Vectorizable structure
void vec(float a[], float b[], float c[]) { int i = 0; while (i < 100) { // The if branch is inside body of loop. a[i] = b[i] * c[i]; if (a[i] < 0.0) a[i] = 0.0; i++; } }
The following example shows a loop that cannot be vectorized because of the inherent potential for an early exit from the loop.
Example: Non-vectorizable structure
void no_vec(float a[], float b[], float c[]) { int i = 0; while (i < 100) { if (a[i] < 50) // The next statement is a second exit // that allows an early exit from the loop. break; ++i; } }

Loop Exit Conditions

Loop exit conditions determine the number of iterations a loop executes. For example, fixed indexes for loops determine the iterations. The loop iterations must be countable; in other words, the number of iterations must be expressed as one of the following:
  • A constant.
  • A loop invariant term.
  • A linear function of outermost loop indices.
In the case where a loops exit depends on computation, the loops are not countable. The examples below show loop constructs that are countable and non-countable.
Example: Countable Loop