Skip to content

Commit

Permalink
Micro-optimize pop_lsb() for 64bit case
Browse files Browse the repository at this point in the history
On Intel, perhaps due to 'lea' instruction this way of
zeroing the lsb of *b seems faster than a shift+negate.

On perft (where any speed difference is magnified) I
got a 6% speed up on my Intel i5 64bit.

Suggested by Hongzhi Cheng.

No functional change.
  • Loading branch information
mcostalba committed Nov 2, 2012
1 parent e3b0327 commit 94ecdef
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/bitboard.h
Expand Up @@ -280,7 +280,7 @@ FORCE_INLINE Square msb(Bitboard b) {

FORCE_INLINE Square pop_lsb(Bitboard* b) {
const Square s = lsb(*b);
*b &= ~(1ULL << s);
*b &= *b - 1;
return s;
}

Expand Down

2 comments on commit 94ecdef

@hongzhicheng
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

Here is another issue that might need your attention, which is quite similar to one of those issues I pointed out before.

In function connected_moves(), Stockfish has

// Case 4: The destination square for m2 is defended by the moving piece in m1
p1 = pos.piece_on(t1);
if (pos.attacks_from(p1, t1) & t2)
    return true;

It is possible that square f2 will block an x-ray attack from p1, so it would be correct if we modify the above code as:

// Case 4: The destination square for m2 is defended by the moving piece in m1
p1 = pos.piece_on(t1);
if (pos.attacks_from(p1, t1, pos.pieces() ^ f2) & t2)
    return true;

Thanks a lot

Hongzhi

@mcostalba
Copy link
Owner Author

@mcostalba mcostalba commented on 94ecdef Nov 2, 2012 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.