25112 Rev. 3.06

September 2005

Software Optimization Guide for AMD64 Processors

Listing 20. Example 2 (Avoid)

 

int i;

====>

mov eax, i

 

 

 

cdq

 

i = i / 4;

 

and edx, 3

 

 

 

add eax, edx

 

 

 

sar eax, 2

 

 

 

mov i, eax

 

Listing 21. Example 2 (Preferred)

unsigned int i; ====> shr i, 2

i = i / 4;

In summary, use unsigned types for:

Division and remainders

Loop counters

Array indexing

Use signed types for:

Integer-to-floating-point conversion

Chapter 2

C and C++ Source-Level Optimizations

49

Page 65
Image 65
AMD 250 manual Listing 20. Example 2 Avoid