Loop Unrolling
6-94
6.9
Loop Unrolling
Even though the performance of the previous example is good, it can be im-
proved. When resources are not fully used, you can improve performance by
unrolling the loop. In Example 6–52, only nine instructions execute every two
cycles. If you unroll the loop and analyze the new minimum iteration interval,
you have room to add instructions. A minimum iteration interval of 3 provides
a 25% improvement in throughput: three cycles to do two iterations, rather
than the four cycles required in Example 6–51.
6.9.1
Unrolled If-Then-Else C Code
Example 6–52 shows the unrolled version of the if-then-else C code in
Example 6–47 on page 6-86.
Example 6–52. If-Then-Else C Code (Unrolled)
int unrolled_if_then(short a[], int codeword, int mask, short theta)
{
int i,sum, cond;
sum = 0;
for (i = 0; i < 32; i+=2){
cond = codeword & mask;
if (theta == !(!(cond)))
sum += a[i];
else
sum –= a[i];
mask = mask << 1;
cond = codeword & mask;
if (theta == !(!(cond)))
sum += a[i+1];
else
sum –= a[i+1];
mask = mask << 1;
}
return(sum);
}