AMD 250 Computer Hardware User Manual


 
134 Branch Optimizations Chapter 6
25112 Rev. 3.06 September 2005
Software Optimization Guide for AMD64 Processors
Avoid
long fac(long a)
{
if (a == 0) {
return (1);
} else {
myp(a); // Can cause returns to be mispredicted
return (a * fac(a - 1));
}
}
void myp(long a)
{
printf("myp ");
return;
}
Because the function fac, in the following example, is end-recursive, it can be converted to iterative
code. A recursive function is classified as end-recursive when the function call to itself is at the end of
the code. The following listing shows the rewritten code:
Preferred
long fac1(long a)
{
long t = 1;
while (a > 0) {
myp(a);
t *= a;
a--;
}
return (t);
}