Refining C/C++ Code
3-48
Example 3–31 shows how the compiler can perform simple loop unrolling of
replicating the loop body. The MUST_ITERATE pragma tells the compiler that
the loop will execute an even number of 20 or more times. This compiler will
unroll the loop once to take advantage of the performance gain that results
from the unrolling.
Example 3–31. Vector Sum
void vecsum(short *restrict a, const short *restrict b, const short *restrict
c, int n)
{
int i;
#pragma MUST_ITERATE (20, , 2);
for (i = 0; i < n; i++) a[i] = b[i] + c[i];
}
<compiler output for above code>
L2: ; PIPED LOOP KERNEL
ADD .L1X B7,A3,A3 ; |5|
|| [ B0] B .S1 L2 ; @|5|
|| LDH .D1T1 *++A4(4),A3 ; @@|5|
|| LDH .D2T2 *++B4(4),B7 ; @@|5|
[!A1] STH .D1T1 A3,*++A0(4) ; |5|
|| ADD .L2X B6,A5,B6 ; |5|
|| LDH .D2T2 *+B4(2),B6 ; @@|5|
[ A1] SUB .L1 A1,1,A1 ;
|| [!A1] STH .D2T2 B6,*++B5(4) ; |5|
|| [ B0] SUB .L2 B0,1,B0 ; @@|5|
|| LDH .D1T1 *+A4(2),A5 ; @@|5|
Note:
When the interrupt threshold option is used, unrolling can be used
to regain lost performance. Refer to section 7.4.4
Getting the Most Perfor-
mance Out of Interruptible Code.
If the compiler does not automatically unroll the loop, you can suggest that the
compiler unroll the loop by using the UNROLL pragma. See the
TMS320C6000 Optimizing C/C++ Compiler User’s Guide for more informa-
tion.