Chapter 2 C and C++ Source-Level Optimizations 49
Software Optimization Guide for AMD64 Processors
25112 Rev. 3.06 September 2005
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