Download "TMS320C6000 Optimizing C Compiler User's Guide"

Transcript
Interfacing C and C++ With Assembly Language
The TMS320C6000 Programmer’s Guide states that one of the ways to refine
C/C++ code is to use word accesses to operate on 16-bit data stored in the
high and low parts of a 32-bit register. Examples using casts to int pointers are
shown with the use of intrinsics to use certain instructions like _mpyh. This can
be automated by using the _nassert( ); intrinsic to specify that 16-bit short arrays are aligned on a 32-bit (word) boundary.
The following two examples generate the same assembly code:
- Example 1
int dot_product(short *x, short *y, short z)
{
int *w_x = (int *)x;
int *w_y = (int *)y;
int sum1 = 0, sum2 = 0, i;
for (i = 0; i < z/2; i++)
{
sum1 += _mpy(w_x[i], w_y[i]);
sum2 += _mpyh(w_x[i], w_y[i]);
}
return (sum1 + sum2);
}
- Example 2
int dot_product(short *x, short *y, short z)
{
int sum = 0, i;
_nassert (((int)(x) & 0x3) == 0);
_nassert (((int)(y) & 0x3) == 0);
#pragma MUST_ITERATE(20, , 4);
for (i = 0; i < z; i++) sum += x[i] * y[i];
return sum;
}
Note: C++ Syntax for _nassert
In C++ code, _nassert is part of the standard namespace. Thus, the correct
syntax is std::_nassert().
8-36