User Guide

64-Bit Media Programming 221
24592—Rev. 3.15—November 2009 AMD64 Technology
The PCMPEQx and PCMPGTx instructions compare corresponding bytes, words, or doubleword in
the first and second operands. The instructions then write a mask of all 1s or 0s for each compare into
the corresponding, same-sized element of the destination.
For the PCMPEQx instructions, if the compared values are equal, the result mask is all 1s. If the values
are not equal, the result mask is all 0s. For the PCMPGTx instructions, if the signed value in the first
operand is greater than the signed value in the second operand, the result mask is all 1s. If the value in
the first operand is less than or equal to the value in the second operand, the result mask is all 0s.
PCMPEQx can be used to set the bits in an MMX register to all 1s by specifying the same register for
both operands.
By specifying the same register for both operands, PCMPEQx can be used to set the bits in an MMX
register to all 1s.
Figure 5-5 on page 198 shows an example of a non-branching sequence that implements a two-way
multiplexer—one that is equivalent to the following sequence of ternary operators in C or C++:
r0=a0>b0?a0:b0
r1=a1>b1?a1:b1
r2=a2>b2?a2:b2
r3=a3>b3?a3:b3
Assuming mmx0 contains a, and mmx1 contains b, the above C sequence can be implemented with the
following assembler sequence:
MOVQ mmx3, mmx0
PCMPGTW mmx3, mmx2 ;a>b?0xffff : 0
PAND mmx0, mmx3 ;a>b?a:0
PANDN mmx3, mmx1 ;a>b>0:b
POR mmx0, mmx3 ;r=a>b?a:b
In the above sequence, PCMPGTW, PAND, PANDN, and POR operate, in parallel, on all four
elements of the vectors.
Compare and Write Minimum or Maximum
PMAXUB—Packed Maximum Unsigned Bytes
PMINUB—Packed Minimum Unsigned Bytes
PMAXSW—Packed Maximum Signed Words
PMINSW—Packed Minimum Signed Words
The PMAXUB and PMINUB instructions compare each of the 8-bit unsigned integer values in the
first operand with the corresponding 8-bit unsigned integer values in the second operand. The
instructions then write the maximum (PMAXUB) or minimum (PMINUB) of the two values for each
comparison into the corresponding byte of the destination.
The PMAXSW and PMINSW instructions perform operations analogous to the PMAXUB and
PMINUB instructions, except on 16-bit signed integer values.