]> git.sesse.net Git - stockfish/commitdiff
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 241970f834c398b765069d89f261185e700db6d4..6f3a0048226eb620e6e9c07d9c4185145ad27b68 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 83a0c9123c708b2e739ecb9a737f51fa2ab1fa9d..79fe1ae37ad5105427b88b85d9c7bff8ac63f5a9 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 0ffcc79447bb67db6cdde1f322a685782fa43d03..b10ea9ea49c787bab1b08670917505cb3cb5023e 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 f71b54bc7d0fa6e22dc539b3b25f48d53c2ac666..03df6d7802547e76f171a85b1369449493311217 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