SPRU514Z July 2001 – October 2023 SM320F28335-EP
In some cases the compiler may recognize a DMAC opportunity in C-language statements but not be able to verify that the data addresses passed to the computation are both 32-bit aligned. In this case, assertions placed in the code can enable the compiler to generate DMAC instructions. The following is an example:
int *src1, *src2; // src1 and src2 are pointers to int arrays of at least size N
// You must ensure that both are 32-bit aligned addresses
{...}
int i;
long res = 0;
_nassert((long)src1 % 2 == 0);
_nassert((long)src2 % 2 == 0);
for (i = 0; i < N; i++) // N must be a known even constant
res += (long)src1[i] * src2[i]; // src1 and src2 must be accessed via array indices
At optimization levels >= -O2, the compiler generates a RPT || DMAC instruction for the example code above if N is a known even constant.
The _nassert intrinsic generates no code and so is not a compiler intrinsic like those listed in Table 7-6. Instead, it tells the optimizer that the expression declared with the assert function is true. This can be used to give hints to the optimizer about what optimizations might be valid. In this example, _nassert is used to assert that the data addresses represented by the src1 and src2 pointers are 32-bit aligned. You are responsible for ensuring that only 32-bit aligned data addresses are passed via these pointers. The code will result in a run-time failure if the asserted conditions are not true.
DMAC instructions can also shift the product left by 1 or right by 1 to 6 before accumulation. For example:
for (i = 0; i < N; i++)
res += (long)src1[i] * src2[i] >> 1; // product shifted right by 1