Introduce single_bit() helper
authorMarco Costalba <mcostalba@gmail.com>
Sun, 4 Mar 2012 22:26:08 +0000 (23:26 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Sun, 4 Mar 2012 22:37:44 +0000 (23:37 +0100)
Self-documenting code instead of a tricky
bitwise tweak, not known by everybody.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>

src/bitboard.h
src/evaluate.cpp
src/position.cpp
src/search.cpp

index 241970f..6f3a004 100644 (file)
@@ -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.
index 83a0c91..79fe1ae 100644 (file)
@@ -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;
         }
 
index 0ffcc79..b10ea9e 100644 (file)
@@ -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;
index f71b54b..03df6d7 100644 (file)
@@ -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