If-Then-Else Statements in a Loop
6.8.7
Comparing Performance
Example 6–51. Assembly Code for If-Then-Else With Loop Count Greater Than 3
B
.S1
||
LDH
.D1
||
MVK
.S2
SHL
.S1
||
AND
.S2X
[B2] MVK
.S2
||
B
.S1
||
LDH
.D1
CMPEQ .L2
||
SHL
.S1
||
AND
.S2X
||
ZERO
.L1
LOOP:
[B0] ADD
.L2
||[B2] MVK
.S2
||[B0] B
.S1
||
LDH
.D1
[B1] ADD
.L1
||[!B1]SUB
.D1
||
CMPEQ .L2
||
SHL
.S1
||
AND
.S2X
; Branch occurs here
6-92
You can improve the performance of the code in Example 6–50 if you know
that the loop count is at least 3. If the loop count is at least 3, remove the decre-
ment counter instructions outside the loop and put the MVK (for setting up the
loop counter) in parallel with the first branch. These two changes save two
cycles at the beginning of the loop prolog.
The first two branches are now unconditional, because the loop count is at
least 3 and you know that the first two branches must execute. To account for
the removal of the three decrement-loop-counter instructions, set the loop
counter to 3 fewer than the actual number of times you want the loop to
execute: in this case, 29 (32 – 3).
LOOP
; for LOOP
*A4++,A5
; a[i]
29,B0
; set up loop counter
A6,1,A6
; mask = mask << 1;
B4,A6,B2
; cond = codeword & mask
1,B2
; !(!(cond))
LOOP
;* for LOOP
*A4++,A5
;* a[i]
B6,B2,B1
; (theta == !(!(cond)))
A6,1,A6
;* mask = mask << 1;
B4,A6,B2
;* cond = codeword & mask
A7
; zero out accumulator
–1,B0,B0
; decrement counter
1,B2
;* !(!(cond))
LOOP
;** for LOOP
*A4++,A5
;** a[i]
A7,A5,A7
; sum += a[i]
A7,A5,A7
; sum –= a[i]
B6,B2,B1
;* (theta == !(!(cond)))
A6,1,A6
;** mask = mask << 1;
B4,A6,B2
;** cond = codeword & mask
Example 6–51 shows the improved loop with a cycle count of 68 (2
Table 6–19 compares the performance of Example 6–50 and Example 6–51.
32 + 4).
Need help?
Do you have a question about the TMS320C6000 and is the answer not in the manual?
Questions and answers