Contents    Page-10    Prev    Next    Page+10    Index   

Loop Unrolling

Loop unrolling is the compile-time expansion of a loop into repetitions of the code, with the loop index replaced by its value in each instance.

for (i = 0; i < 3; i++)  disp[i] = c2[i] - c1[i];
is expanded into:

disp[0] = c2[0] - c1[0];
disp[1] = c2[1] - c1[1];
disp[2] = c2[2] - c1[2];

Loop: Unrolled:
Instructions: 20 12
Executed: 57 12

The second form runs faster, and it may generate less code. This is a useful optimization when the size of the loop is known to be a small constant at compile time.

Modulo unrolling unrolls a loop modulo some chosen constant. This can significantly reduce the loop overhead without expanding code size too much.