I've heard that the x86 comparison instruction: cmp x, y
does a subtraction and sets various flags based on the result.
Now, what if I just want to test if the two operands are equal? Wound't doing an XOR instead of a subtraction be much faster? My question is, is there an instruction that does a comparison with an XOR to tell if the two operands are equal? Maybe it would look like this: cmpeq x, y
or cmpxor x, y
.
I would guess that if I just want to test for equality, using a cmpxor
would be faster than the cmp
, which does a subtraction. Is there such an instruction like cmpxor
that would give me a speedup?
I also want to say that I'm aware that xor
will set the zero flag
. But if I do xor x, y
, it will change x
. I don't want that. I want a comparison instruction that will leave both the operands alone.
Basic machine operations such as XOR, SUB, CMP, TEST are all simple enough so they all operate extremely fast. They also set the same condition code bits. From the point of view of compare-for-equal, these all set the Z bit the same way; other bits are set differently because these operations compute different results.
For the x86 CPUs, there is no difference in execution times of these, because they all use identical pathways through the chip. Consequently you can use any of them without performance penalty where it computes the answer that you want. (Technically a SUB should take longer than XOR because the carry has to "ripple" through all the bits, whereas XOR is bit-by-bit parallel. The CPU designers have figured out ways to build extremely fast carry-computing logic so the effective time difference isn't significant. They have huge motivation to do so, since most of what a computer does is "add"]).
As a style convention, if you think you are "comparing two (machine-word-sized) values", you should probably use the CMP instruction, because that communicates what you are thinking to the reader of your code. And it has the advantage that it doesn't destroy one of the operands, which you will find ultimately a very persuasive argument for using it instead of XOR, once you've written enough code. (TEST has this nice property, useful for checking bits, too).
There are compares of other kinds of values for which other x86 instructions are better: floating compares, string compares, vector register compares, etc. These instructions take different times than the basic operations because they must do more complicated things (like comparing multiple data words).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With