It does for me (gcc 3.3.4). I get a warning that the constant will be clipped. So your code is not portable to 32-bit architectures.
Also note that when 'bool' would not be a 64-bit int type, the result would get clipped to the length of the bool.
Good catch. I thought immediate operands were limited to 32 bits in x64, but MOV appears to be an exception I was not aware of.2. On x64, "movabs" loads a 64 bit immediate into a register. It does not go through memory.
Not that it matters in terms of instruction count, though: loading from a fixed address in a 'constant table' is also a single instruction. Of course you cannot afford to keep the constant in a register permanently, in a real-life chess p'engine.
I did actually compile your code, (-O3 optimization), and unlike what you say, it does generate the shift: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
Code: Select all
.file "bit.c"
.text
.p2align 4,,15
.globl test
.type test, @function
test:
.LFB0:
.cfi_startproc
movabsq $1157442765409227006, %rax
xorl %esi, %edi
movl %edi, %ecx
shrq %cl, %rax
andl $1, %eax
ret
.cfi_endproc
.LFE0:
.size test, .-test
.ident "GCC: (Ubuntu 8.3.0-6ubuntu1~18.10.1) 8.3.0"
.section .note.GNU-stack,"",@progbits
Code: Select all
.file "bit2.c"
.text
.p2align 4,,15
.globl test
.type test, @function
test:
.LFB0:
.cfi_startproc
xorl %esi, %edi
leaq kind(%rip), %rax
movslq %edi, %rsi
movzbl (%rax,%rsi), %eax
ret
.cfi_endproc
.LFE0:
.size test, .-test
.ident "GCC: (Ubuntu 8.3.0-6ubuntu1~18.10.1) 8.3.0"
.section .note.GNU-stack,"",@progbits