If-Then-Else Statements in a Loop
6-92
6.8.7
Comparing Performance
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).
Example 6–51. Assembly Code for If-Then-Else With Loop Count Greater Than 3
B
.S1
LOOP
; for LOOP
||
LDH
.D1
*A4++,A5
; a[i]
||
MVK
.S2
29,B0
; set up loop counter
SHL
.S1
A6,1,A6
; mask = mask << 1;
||
AND
.S2X
B4,A6,B2
; cond = codeword & mask
[B2] MVK
.S2
1,B2
; !(!(cond))
||
B
.S1
LOOP
;* for LOOP
||
LDH
.D1
*A4++,A5
;* a[i]
CMPEQ
.L2
B6,B2,B1
; (theta == !(!(cond)))
||
SHL
.S1
A6,1,A6
;* mask = mask << 1;
||
AND
.S2X
B4,A6,B2
;* cond = codeword & mask
||
ZERO
.L1
A7
; zero out accumulator
LOOP:
[B0] ADD
.L2
–1,B0,B0
; decrement counter
||[B2] MVK
.S2
1,B2
;* !(!(cond))
||[B0] B
.S1
LOOP
;** for LOOP
||
LDH
.D1
*A4++,A5
;** a[i]
[B1] ADD
.L1
A7,A5,A7
; sum += a[i]
||[!B1]SUB
.D1
A7,A5,A7
; sum –= a[i]
||
CMPEQ
.L2
B6,B2,B1
;* (theta == !(!(cond)))
||
SHL
.S1
A6,1,A6
;** mask = mask << 1;
||
AND
.S2X
B4,A6,B2
;** cond = codeword & mask
; Branch occurs here
Example 6–51 shows the improved loop with a cycle count of 68 (2
32 + 4).
Table 6–19 compares the performance of Example 6–50 and Example 6–51.