Clearing registers, Compares, Clearing registers -87 compares -87 – Intel ARCHITECTURE IA-32 User Manual
Page 159

General Optimization Guidelines
2
2-87
Clearing Registers
Pentium 4 processor provides special support to
xor
,
sub
, or
pxor
operations when executed within the same register. This recognizes that
clearing a register does not depend on the old value of the register. The
xorps
and
xorpd
instructions do not have this special support. They
cannot be used to break dependence chains.
In Intel Core Solo and Intel Core Duo processors; the
xor
,
sub
,
xorps
,
or
pxor
instructions can be used to clear execution dependencies on the
zero evaluation of the destination register.
Assembly/Compiler Coding Rule 51. (M impact, ML generality) Use
xor
,
sub
, or
pxor
to set a register to
0
, or to break a false dependence chain
resulting from re-use of registers. In contexts where the condition codes must
be preserved, move
0
into the register instead. This requires more code space
than using
xor
and
sub
, but avoids setting the condition codes.
Compares
Use
test
when comparing a value in a register with zero.
Test
essentially
and
s the operands together without writing to a destination
register.
Test
is preferred over
and
because
and
produces an extra result
register.
Test
is better than
cmp ..., 0
because the instruction size is
smaller.
Use
test
when comparing the result of a logical
and
with an immediate
constant for equality or inequality if the register is
eax
for cases such
as:
if (avar & 8) { }
The
test
instruction can also be used to detect rollover of modulo a
power of 2. For example, the C code:
if ( (avar % 16) == 0 ) { }
can be implemented using:
test eax, 0x0F
jnz AfterIf