Consider The Sign Of Integer Operands - AMD Athlon Processor x86 Optimization Manual

X86 code optimization
Table of Contents

Advertisement

AMD Athlon™ Processor x86 Code Optimization

Consider the Sign of Integer Operands

14
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;
unsigned int i;
x = i;
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;
int i;
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:
====>
MOV
[temp+4], 0
MOV
EAX, i
MOV
[temp], eax
FILD QWORD PTR [temp]
FSTP QWORD PTR [x]
====>
FILD DWORD PTR [i]
FSTP QWORD PTR [x]
Consider the Sign of Integer Operands
22007E/0—November 1999

Advertisement

Table of Contents
loading

Table of Contents