hgm wrote: ↑Thu Sep 16, 2021 3:49 pm
klx wrote: ↑Wed Sep 15, 2021 4:54 pm
Code: Select all
bool is_rook_move_emanuel(int from, int to) {
return 0x1010101010101fe & 1ULL << (from ^ to);
}
That is a LOAD (of the constant 1ull), a LOAD of the 'magic number' (which would need a ULL suffix, btw, and must be loaded from memory even though it is a constant due to its size), a SHIFT plus an AND. That is twice as many instructions than kind[from^to]. So learn to count.
With all due respect, you are wrong on all accounts.
1. No it does not need a ULL suffix.
2. On x64, "movabs" loads a 64 bit immediate into a register. It does not go through memory.
3. As I wrote in another post, at least on x64 systems and with a decent compiler, the specific expression I wrote will compile to the BT (bit test) instruction. There's no loading 1ULL, no shifting. There's essentially only MOV, XOR and BT, and then the result is in the carry flag for whatever use case you have. And if this check is repeated many times (which is likely to be the case if it needs to be optimized), the constant would be kept in a register, in which case there's only XOR and BT.
Your version is not bad, but a memory lookup for each test, even if L1 cached, will nonetheless be slower.
Best regards / Emanuel