From: Andrey Neporada Date: Sat, 3 Dec 2016 08:37:07 +0000 (+0400) Subject: Help GCC to optimize msb() to single instruction X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=bf8b45fe6305c89a9a30f660de34164cf3bdcdf2;ds=sidebyside 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. --- diff --git a/src/bitboard.h b/src/bitboard.h index 715f6c4a..f41abeb2 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -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)