Indicates to the compiler to unroll or not to unroll a counted loop.
#pragma unroll #pragma unroll(n) #pragma nounroll |
n |
is the unrolling factor representing the number of times to unroll a loop; it is an integer constant from 0 through 255 |
The unroll[n] pragma tells the compiler how many times to unroll a counted loop.
The unroll pragma must precede the FOR statement for each FOR loop it affects. If n is specified, the optimizer unrolls the loop n times. If n is omitted or if it is outside the allowed range, the optimizer assigns the number of times to unroll the loop.
This pragma is supported only when option O3 is set. The unroll pragma overrides any setting of loop unrolling from the command line.
The pragma can be applied for the innermost loop nest as well as for the outer loop nest. If applied to outer loop nests, the current implementation supports complete outer loop unrolling. The loops inside the loop nest are either not unrolled at all or completely unrolled. The compiler generates correct code by comparing n and the loop count.
When unrolling a loop increases register pressure and code size it may be necessary to prevent unrolling of a loop. In such cases, use the nounroll pragma. The nounroll pragma instructs the compiler not to unroll a specified loop.
Example 1: Using unroll pragma for innermost loop unrolling
void unroll(int a[], int b[], int c[], int d[])
{
#pragma unroll(4)
for (int i = 1; i < 100; i++) {
b[i] = a[i] + 1;
d[i] = c[i] + 1;
}
}
Example 2: Using unroll pragma for outer loop unrolling
In Example 2, placing the #pragma unroll before the first for loop causes the compiler to unroll the outer loop completely. If a #pragma unroll is placed before the inner for loop as well as before the outer for loop, the compiler ignores the inner for loop unroll pragma. If the #pragma unroll is placed only for the innermost loop, the compiler unrolls the innermost loop according to some factor.
int m = 0;
int dir[4]= {1,2,3,4};
int data[10];
#pragma unroll (4) // outer loop unrolling
for (int i = 0; i < 4; i++)
{
for (int j = dir[i]; data[j]==N ; j+=dir[i])
m++;
}
Copyright © 1996-2011, Intel Corporation. All rights reserved.