Author: Omid David Tabibi
Date: 05:22:47 09/27/03
From AMD Athlon¢â Processor x86 Code Optimization Guide:
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, take into consideration that certain operations are faster
with unsigned types while others are faster for signed types.
...
Computing quotients and remainders in integer division by
constants are faster when performed on unsigned types. The
following typical case is the compiler output for a 32-bit integer
divided by four:
Example 2 (Avoid):
int i; ====> MOV EAX, i
CDQ
i = i / 4; AND EDX, 3
ADD EAX, EDX
SAR EAX, 2
MOV i, EAX
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-float conversion
========
In a typical chess program, almost all the int variables can be converted
unsigned int. This will certainly not incur any speed loss, but in how much
speed increase will this result?
Isn't it better to always use unsigned int instead of int, for example:
for (unsigned int i; i < numner; i++)
instead of
for (int i; i < numner; i++)
?
This page took 0.01 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.