Live data from Hacker News

That XOR Trick (2020)

florian.github.io

241–243 of 243 posts

Re: That XOR Trick (2020)

#241

Earlier quoted context omitted.

Prefer pythonic style std::tie(b, a) = std::make_tuple(a, b);

I know normally python is simpler and easier to read, but this is not one of those times. The example C++ is much easier to read and more intuitive as to what it does.

To be fair, the Python syntax is just:

a, b = b, a

The "std::tie(b, a) = std::make_tuple(a, b)" is an attempt to replicate the Python style in C++... which, I agree, is certainly not clear, and, unlike using the "swap" algorithm, it's also unclear whether it will result in efficient code.

Re: That XOR Trick (2020)

#242

Earlier quoted context omitted.

It's not overhead, it's about dependency breaking. 32-bit xors on a single register are universally recognized as a zeroing idiom, which means the CPU doesn't have to wait for the results of previous operations in order to set the value of the applicable register to zero. In modern CPUs zero'ing idioms aren't even executed, they only get as far as the register allocater. The register allocater will allocate a zero'd…

Could you share a source about register allocation describing optimizations such as the one you described?

The magic words for this is "zeroing idiom".

Re: That XOR Trick (2020)

#243
post #67

Surprised this doesn't have my favorite XOR trick: implementing a double-linked list with one pointer! Store predecessor XOR successor in each node. I suspect this clicks already with everyone, and I don't need to explain forward and backward iteration.

What it doesn't click with is GC. Any pointer xor trick will fail when trying to add a GC to the app, because the pointers don't point at the right thing anymore.

GCs don't typically let you work with raw pointers.
Post reply on HN