Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Aw, that's an unsatisfying answer!

I'm a total .NET noob, but this attempt in Compiler Explorer shows it not using the xor trick.

https://godbolt.org/z/jhzqxEKzT

Maybe I need some extra compiler flags or something.



> I’m a total .NET noob, but this attempt in Compiler Explorer shows it not using the xor trick.

It better not. The CLR trick introduces a data dependency that slows down the code on modern CPUs.

Compilers sometimes also can compile this down to zero instructions. Nothing says the variable-to-register mapping has to be constant in a single function.


That would just load each global variable into registers and write them back to memory, swapping them.

If the compiler had decided to put two locals located already in registers, and had the swap in a branch, like so:

    if (foo()) {
        bar();
        (a, b) = (b, a);
    }
Then the registers might get swapped with an xchg instruction or something, I don’t know. The compiler’s goal is to have pipelined execution be as fast as possible, so there’s no way it’s going to use bit-mangling operations that would get in the way of that.


The xor trick has no practical value. Sorry.


It was useful in single accumulator processor architectures since it could avoid one memory store.


I agree it isn't normally helpful on a modern computer, but that's a strong statement. What if you need to swap large storage spaces with no extra memory? Isn't there still old hardware out there especially in aerospace that could benefit?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: