Skip to content

Commit

Permalink
Help GCC to optimize msb() to single instruction
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nepal authored and mcostalba committed Dec 3, 2016
1 parent e70da0d commit bf8b45f
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/bitboard.h
Expand Up @@ -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)
Expand Down

0 comments on commit bf8b45f

Please sign in to comment.