bpo-29782: Consolidate _Py_Bit_Length() by niklasf · Pull Request #20739 · python/cpython (original) (raw)
Using gcc 10 -O3 -flto, I get the following machine code in test_bit_length():
# input = rax
# if (x != 0)
test rax,rax
je 0x7fffeaa99978 <test_bit_length+504>
# return (int)sizeof(unsigned long) * 8 - __builtin_clzl(x);
# return 0x40 - bsr(rax) ^ 0x3f
bsr rax,rax
mov ecx,0x40
xor rax,0x3f
mov r9d,ecx
sub r9d,eax
# result = r9d
x86 BSR (Bit Scan Reverse) instruction is used as expected.