From bf8b45fe6305c89a9a30f660de34164cf3bdcdf2 Mon Sep 17 00:00:00 2001 From: Andrey Neporada Date: Sat, 3 Dec 2016 12:37:07 +0400 Subject: [PATCH] Help GCC to optimize msb() to single instruction MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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. --- src/bitboard.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) -- 2.39.2