From: Marco Costalba Date: Sun, 4 Mar 2012 22:26:08 +0000 (+0100) Subject: Introduce single_bit() helper X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=19540c9ee824abc156d5a12ab353c250a083da4b Introduce single_bit() helper Self-documenting code instead of a tricky bitwise tweak, not known by everybody. No functional change. Signed-off-by: Marco Costalba --- diff --git a/src/bitboard.h b/src/bitboard.h index 241970f8..6f3a0048 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -207,6 +207,14 @@ inline Bitboard same_color_squares(Square s) { } +/// single_bit() returns true if in the 'b' bitboard is set a single bit (or if +/// b == 0). + +inline bool single_bit(Bitboard b) { + return !(b & (b - 1)); +} + + /// first_1() finds the least significant nonzero bit in a nonzero bitboard. /// pop_1st_bit() finds and clears the least significant nonzero bit in a /// nonzero bitboard. diff --git a/src/evaluate.cpp b/src/evaluate.cpp index 83a0c912..79fe1ae3 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -517,7 +517,7 @@ namespace { assert(b); - if (!(b & (b - 1)) && (b & pos.pieces(Them))) + if (single_bit(b) && (b & pos.pieces(Them))) score += ThreatBonus[Piece][type_of(pos.piece_on(first_1(b)))] / 2; } diff --git a/src/position.cpp b/src/position.cpp index 0ffcc794..b10ea9ea 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -379,8 +379,7 @@ Bitboard Position::hidden_checkers() const { { b = squares_between(ksq, pop_1st_bit(&pinners)) & occupied_squares(); - // Only one bit set and is an our piece? - if (b && !(b & (b - 1)) && (b & pieces(sideToMove))) + if (b && single_bit(b) && (b & pieces(sideToMove))) result |= b; } return result; diff --git a/src/search.cpp b/src/search.cpp index f71b54bc..03df6d78 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -1351,7 +1351,7 @@ split_point_start: // At split points actual search starts from here // Rule 1. Checks which give opponent's king at most one escape square are dangerous b = kingAtt & ~pos.pieces(them) & ~newAtt & ~(1ULL << to); - if (!(b && (b & (b - 1)))) + if (single_bit(b)) // Catches also !b return true; // Rule 2. Queen contact check is very dangerous