Linear Assembly Considerations
8-47
’C64x Programming Considerations
Example 8–20. Loop Trip Count Using BDEC
.global _count_loop_iterations
_count_loop_iterations .cproc count
.reg i, iters
ZERO iters ; Initialize our return value to 0.
SUB count, 1, i ; i = count – 1;
BDEC loop, i ; Do not iterate if count < 1.
does_not_iterate:
.return iters ; Loop does not iterate, just return 0.
loop: .trip 1 ; This loop is guaranteed to iterate at
; least once.
ADD iters, 1, iters ; iters++
BDEC loop, i ; while (i–– >= 0);
.return iters ; Return our number of iterations.
.endproc
Another approach to using BDEC is to allow the loop to execute extra itera-
tions, and then compensate for these iterations after the loop. This is particu-
larly effective in cases where the cost of the conditional flow before the loop
is greater than the cost of executing the body of the loop, as in the example
above. Example 8–21 shows one way to apply this modification.
Example 8–21. Loop Tip Count Using BDEC With Extra Loop Iterations
.global _count_loop_iterations
_count_loop_iterations .cproc count
.reg i, iters
MVK –1, iters ; Loop executes exactly 1 extra iteration,
; so start with the iteration count == –1.
SUB count, 1, i ; Force ”count==0” to iterate exactly once.
loop: .trip 1 ; This loop is guaranteed to iterate at
; least once.
ADD iters, 1, iters ; iters++
BDEC loop, i ; while (i–– >= 0);
.return iters ; Return our number of iterations.
.endproc