AMD Athlon™ Processor x86 Code Optimization

22007E/0 — November 1999

Consider the Sign of Integer Operands

In many cases, the data stored in integer variables determines whether a signed or an unsigned integer type is appropriate. For example, to record the weight of a person in pounds, no negative numbers are required so an unsigned type is appropriate. However, recording temperatures in degrees Celsius may require both positive and negative numbers so a signed type is needed.

Where there is a choice of using either a signed or an unsigned type, it should be considered that certain operations are faster with unsigned types while others are faster for signed types.

Integer-to-floating-point conversion using integers larger than 16-bit is faster with signed types, as the x86 FPU provides instructions for converting signed integers to floating-point, but has no instructions for converting unsigned integers. In a typical case, a 32-bit integer is converted as follows:

Example 1 (Avoid):

double

x;

====>

MOV

[temp+4],

0

unsigned int i;

 

MOV

EAX, i

 

 

 

 

MOV

[temp], eax

x = i;

 

 

FILD

QWORD

PTR [temp]

 

 

 

FSTP

QWORD

PTR [x]

This code is slow not only because of the number of instructions but also because a size mismatch prevents store-to-load- forwarding to the FILD instruction.

Example (Preferred):

double x;

====>

FILD

DWORD

PTR [i]

int

i;

 

FSTP

QWORD

PTR [x]

x =

i;

 

 

 

 

Computing quotients and remainders in integer division by constants are faster when performed on unsigned types. In a typical case, a 32-bit integer is divided by four as follows:

14

Consider the Sign of Integer Operands

Page 30
Image 30
AMD x86 manual Consider the Sign of Integer Operands, Example 1 Avoid, Example Preferred