The unroll[n]pragma tells the compiler how many times to unroll a counted loop. The general syntax for this pragma is shown below:
Syntax |
---|
#pragma unroll #pragma unroll(n) #pragma nounroll |
where n is an integer constant from 0 through 255.
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 (Linux*) or /O3 (Windows*) is used. The unroll pragma overrides any setting of loop unrolling from the command line.
Currently, the pragma can be applied only for the innermost loop nest. If applied to the outer loop nests, it is ignored. The compiler generates correct code by comparing n and the loop count.
Example |
---|
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; } } |