1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-07-11 19:49:14 +00:00

Help GCC to optimize msb() to single instruction

GCC compiles builtin_clzll to “63 ^ BSR”. BSR is processor instruction "Bit Scan Reverse".
So old msb() function is basically 63 - 63 ^ BSR.
Unfortunately, GCC fails to simplify this expression.

Old function compiles to

    bsrq    %rdi, %rdi
    movl    $63, %eax
    xorq    $63, %rdi
    subl    %edi, %eax
    ret

New function compiles to

    bsrq    %rdi, %rax
    ret

BTW, Clang compiles both function to the same (optimal) code.

No functional change.
This commit is contained in:
Andrey Neporada 2016-12-03 12:37:07 +04:00 committed by Marco Costalba
parent e70da0d2eb
commit bf8b45fe63

View file

@ -291,7 +291,7 @@ inline Square lsb(Bitboard b) {
inline Square msb(Bitboard b) {
assert(b);
return Square(63 - __builtin_clzll(b));
return Square(63 ^ __builtin_clzll(b));
}
#elif defined(_WIN64) && defined(_MSC_VER)