Refining C/C++ Code
3-28
3.4.2.2
Using Word Access in FIR Filter
Example 3–12 shows an FIR filter that can be optimized with word reads of
short data and multiply intrinsics.
Example 3–12. FIR Filter—Original Form
void fir1(const short x[restrict], const short h[restrict], short y[restrict],
int n, int m, int s)
{
int i, j;
long y0;
long round = 1L << (s – 1);
for (j = 0; j < m; j++)
{
y0 = round;
for (i = 0; i < n; i++)
y0 += x[i + j] * h[i];
y[j] = (int) (y0 >> s);
}
}
Example 3–13 shows an optimized version of Example 3–12. The optimized
version passes an int array instead of casting the short arrays to int arrays and,
therefore, helps ensure that data passed to the function is word-aligned. As-
suming that a prototype is used, each invocation of the function ensures that
the input arrays are word-aligned by forcing you to insert a cast or by using int
arrays that contain short data.