Refining C/C++ Code
3-27
Optimizing C/C++ Code
3.4.2.1
Using Word Access in Dot Product
Other intrinsics that are useful for reading short data as words are the multiply
intrinsics. Example 3–11 is a dot product example that reads word-aligned
short data and uses the _mpy( ) and _mpyh( ) intrinsics. The _mpyh( ) intrin-
sic uses the ’C6000 instruction MPYH, which multiplies the high 16 bits of two
registers, giving a 32-bit result.
This example also uses two sum variables (sum1 and sum2). Using only one
sum variable inhibits parallelism by creating a dependency between the write
from the first sum calculation and the read in the second sum calculation. With-
in a small loop body, avoid writing to the same variable, because it inhibits par-
allelism and creates dependencies.
Example 3–11. Dot Product Using Intrinsics
int dotprod(const short *restrict a, const short *restrict b, unsigned int N)
{
int i, sum1 = 0, sum2 = 0;
const int *restrict i_a = (const int *)a;
const int *restrict i_b = (const int *)b;
for (i = 0; i < (N >> 1); i++)
{
sum1 = sum1 + _mpy (i_a[i], i_b[i]);
sum2 = sum2 + _mpyh(i_a[i], i_b[i]);
}
return sum1 + sum2;
}