Refining C/C++ Code
3-26
If a vecsum( ) function is needed to handle short-aligned data and odd-num-
bered loop counters, then you must add code within the function to check for
these cases. Knowing what type of data is passed to a function can improve
performance considerably. It may be useful to write different functions that can
handle different types of data. If your short-data operations always operate on
even-numbered word-aligned arrays, then the performance of your applica-
tion can be improved. However, Example 3–10 provides a generic vecsum( )
function that handles all types of alignments and array sizes.
Example 3–10. Vector Sum With restrict Keywords, MUST_ITERATE pragma, and Word
Reads (Generic Version)
void vecsum5(short *restrict sum, const short *restrict in1, const short *re-
strict in2, unsigned int N)
{
int i;
/* test to see if sum, in2, and in1 are aligned to a word boundary */
if (((int)sum | (int)in2 | (int)in1) & 0x2)
{
#pragma MUST_ITERATE (20);
for (i = 0; i < N; i++)
sum[i] = in1[i] + in2[i];
}
else
{
const int *restrict i_in1 = (const int *)in1;
const int *restrict i_in2 = (const int *)in2;
int *restrict i_sum = (int *)sum;
#pragma MUST_ITERATE (10);
for (i = 0; i < (N/2); i++)
i_sum[i] = _add2(i_in1[i], i_in2[i]);
if (N & 0x1) sum[i] = in1[i] + in2[i];
}
}
Caution
With the exception of char, avoid casting a pointer to a different
type. Perform memory accesses on the specific object using
multiple types.