If-Then-Else Statements in a Loop
6-86
6.8
If-Then-Else Statements in a Loop
If-then-else statements in C cause certain instructions to execute when the if
condition is true and other instructions to execute when it is false. One way to
accomplish this in linear assembly code is with conditional instructions. Be-
cause all ’C6000 instructions can be conditional on one of five general-pur-
pose registers on the ’C62x and ’C67x and one of 6 on the ’C64x. Conditional
instructions can handle both the true and false cases of the if-then-else C
statement.
6.8.1
If-Then-Else C Code
Example 6–47 contains a loop with an if-then-else statement. You either add
a[i] to sum or subtract a[i] from sum.
Example 6–47. If-Then-Else C Code
int if_then(short a[], int codeword, int mask, short theta)
{
int i,sum, cond;
sum = 0;
for (i = 0; i < 32; i++){
cond = codeword & mask;
if (theta == !(!(cond)))
sum += a[i];
else
sum –= a[i];
mask = mask << 1;
}
return(sum);
}
Branching is one way to execute the if-then-else statement: branch to the ADD
when the if statement is true and branch to the SUB when the if statement is
false. However, because each branch has five delay slots, this method
requires additional cycles. Furthermore, branching within the loop makes soft-
ware pipelining almost impossible.
Using conditional instructions, on the other hand, eliminates the need to
branch to the appropriate piece of code after checking whether the condition
is true or false. Simply program both the ADD and SUB as usual, but make
them conditional on the zero and nonzero values of a condition register. This
method also allows you to software pipeline the loop and achieve much better
performance than you would with branching.