2 ms·
The advantage is not having to use a third register to swap two values around. On systems like x86, registers are in very short supply. It's not quite as releva
by globular-toast 5y ago
The advantage is not having to use a third register to swap two values around. On systems like x86, registers are in very short supply. It's not quite as relevant now as x86 has had XCHG[0] for a long time now. Compilers are quite good at detecting when it should be used so you don't often have to explicitly write the XOR trick. XCHG also doesn't corrupt your flags register.
[0] https://c9x.me/x86/html/file_module_x86_id_328.html https://c9x.me/x86/html/file_module_x86_id_328.html
- Someone 5y agoThe XOR trick also introduces data dependencies that makes it slower on some CPUs (can’t start the second XOR until the result of the first is available, or the third one until the result of the second is available) When using a third register, CPUs with register renaming need not even move data at all. They could move the labels “register A”, “register B” around.