Loops can be formed with the usual for and while constructs. The 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 (i < 50) // The next statement is a second exit // that allows an early exit from the loop. break; ++i; } } |