If-Then-Else Statements In A Loop; If-Then-Else C Code - Texas Instruments TMS320C6000 Programmer's Manual

Hide thumbs Also See for TMS320C6000:
Table of Contents

Advertisement

If-Then-Else Statements in a Loop

6.8 If-Then-Else Statements in a Loop
6.8.1

If-Then-Else C Code

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
sum += a[i];
else
sum –= a[i];
mask = mask << 1;
}
return(sum);
}
6-86
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.
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.
==
!(!(cond)))
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.

Hide quick links:

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the TMS320C6000 and is the answer not in the manual?

Questions and answers

Subscribe to Our Youtube Channel

Table of Contents