It might be because XOR is rarely (in terms of static count, dynamically it surely appears a lot in some hot loops) used for anything else, so it is easier to spot and identify as "special" if you are writing manual assembly.
And helps with SMT Edit: this is apparently not the case, see @tliltocatl's comment down the thread
XOR'ing a register with itself is the idiom for zeroing it out. Why not sub?
21–30 of 231 posts
Re: XOR'ing a register with itself is the idiom for zeroing it out. Why not sub?
#22The obvious answer is that XOR is faster. To do a subtract, you have to propagate the carry bit from the least-significant bit to the most-significant bit. In XOR you don't have to do that because the output of every bit is independent of the other adjacent bits. Probably, there are ALU pipeline designs where you don't pay an explicit penalty. But not all, and so XOR is faster. Surely, someone as awesome as Raymond C…
E.g. on Z80 and 6502 both have the same cycle count.
Re: XOR'ing a register with itself is the idiom for zeroing it out. Why not sub?
#23Re: XOR'ing a register with itself is the idiom for zeroing it out. Why not sub?
#24The obvious answer is that XOR is faster. To do a subtract, you have to propagate the carry bit from the least-significant bit to the most-significant bit. In XOR you don't have to do that because the output of every bit is independent of the other adjacent bits. Probably, there are ALU pipeline designs where you don't pay an explicit penalty. But not all, and so XOR is faster. Surely, someone as awesome as Raymond C…
Re: XOR'ing a register with itself is the idiom for zeroing it out. Why not sub?
#25The obvious answer is that XOR is faster. To do a subtract, you have to propagate the carry bit from the least-significant bit to the most-significant bit. In XOR you don't have to do that because the output of every bit is independent of the other adjacent bits. Probably, there are ALU pipeline designs where you don't pay an explicit penalty. But not all, and so XOR is faster. Surely, someone as awesome as Raymond C…
> The answer is so obvious A tangent, but what is Obvious depends on what you know. Often experts don't explain the things they think are Obvious, but those things are only Obvious to them, because they are the expert. We should all kind, and explain also the Obvious things those who do not know.
Re: XOR'ing a register with itself is the idiom for zeroing it out. Why not sub?
#26The obvious answer is that XOR is faster. To do a subtract, you have to propagate the carry bit from the least-significant bit to the most-significant bit. In XOR you don't have to do that because the output of every bit is independent of the other adjacent bits. Probably, there are ALU pipeline designs where you don't pay an explicit penalty. But not all, and so XOR is faster. Surely, someone as awesome as Raymond C…
The operation is slightly more complex yes, but has there ever been an x86 CPU where SUB or XOR takes more than a single CPU cycle?
I mean, not for zeroing because we know from the TFA that it's special-cased anyway. But maybe if you test on different registers?
Re: XOR'ing a register with itself is the idiom for zeroing it out. Why not sub?
#27"Bonus bonus chatter: The xor trick doesn’t work for Itanium because mathematical operations don’t reset the NaT bit. Fortunately, Itanium also has a dedicated zero register, so you don’t need this trick. You can just move zero into your desired destination." Will remember for the next time I write asm for Itanium!
Re: XOR'ing a register with itself is the idiom for zeroing it out. Why not sub?
#28The obvious answer is that XOR is faster. To do a subtract, you have to propagate the carry bit from the least-significant bit to the most-significant bit. In XOR you don't have to do that because the output of every bit is independent of the other adjacent bits. Probably, there are ALU pipeline designs where you don't pay an explicit penalty. But not all, and so XOR is faster. Surely, someone as awesome as Raymond C…
It's like 0.5 cycles vs 0.9 cycles. So both are 1 cycle, considering synchronization.
Re: XOR'ing a register with itself is the idiom for zeroing it out. Why not sub?
#29Re: XOR'ing a register with itself is the idiom for zeroing it out. Why not sub?
#30The obvious answer is that XOR is faster. To do a subtract, you have to propagate the carry bit from the least-significant bit to the most-significant bit. In XOR you don't have to do that because the output of every bit is independent of the other adjacent bits. Probably, there are ALU pipeline designs where you don't pay an explicit penalty. But not all, and so XOR is faster. Surely, someone as awesome as Raymond C…
The non-obvious bit is why there isn't an even faster and shorter "mov ,0" instructions - the processors started short-circuiting xor , much later.