Refining C/C++ Code
3-50
In the loop in Example 3–32, there is an early exit. If dist0 or dist1 is less than
distance, then execution breaks out of the loop early. If the compiler could not
perform transformations to the loop to software pipeline the loop, you would
have to modify the code. Example 3–33 shows how the code would be modi-
fied so the compiler could software pipeline this loop. In this case however, the
compiler can actually perform some transformations and software pipeline this
loop better than it can the modified code in Example 3–33.
Example 3–32. Use of If Statements in Float Collision Detection (Original Code)
int colldet(const float *restrict x, const float *restrict p, float point,
float distance)
{
int I, retval = 0;
float sum0, sum1, dist0, dist1;
for (I = 0; I < (28 * 3); I += 6)
{
sum0 = x[I+0]*p[0] + x[I+1]*p[1] + x[I+2]*p[2];
sum1 = x[I+3]*p[0] + x[I+4]*p[1] + x[I+5]*p[2];
dist0 = sum0 - point;
dist1 = sum1 - point;
dist0 = fabs(dist0);
dist1 = fabs(dist1);
if (dist0 < distance)
{
retval = (int)&x[I + 0];
break;
}
if (dist1 < distance)
{
retval = (int)&x[I + 3];
break;
}
}
return retval;
}