AMD Athlon™ Processor x86 Code Optimization

22007E/0 — November 1999

Example 1 (Avoid):

//assumes pointers are different and q!=r void isqrt ( unsigned long a,

unsigned long *q, unsigned long *r)

{

 

*q = a;

 

if (a

> 0)

{

 

while

(*q > (*r = a / *q))

 

{

 

*q = (*q + *r) >> 1;

 

}

}

 

*r = a -

*q * *q;

}

 

Example 2 (Preferred):

//assumes pointers are different and q!=r void isqrt ( unsigned long a,

unsigned long *q, unsigned long *r)

{

unsigned long qq, rr; qq = a;

if (a > 0)

{

while (qq > (rr = a / qq))

{

qq= (qq + rr) >> 1;

}

}

rr= a - qq * qq; *q = qq;

*r = rr;

}

32

Copy Frequently De-referenced Pointer Arguments to Local Variables

Page 48
Image 48
AMD manual AMD Athlon Processor x86 Code Optimization