]> git.sesse.net Git - stockfish/commitdiff
Simplify king shelter cache handling
authorMarco Costalba <mcostalba@gmail.com>
Fri, 24 Jul 2009 10:16:18 +0000 (12:16 +0200)
committerMarco Costalba <mcostalba@gmail.com>
Fri, 24 Jul 2009 13:13:13 +0000 (14:13 +0100)
This is more similar to how get_material_info() and
get_pawn_info() work and also removes some clutter from
evaluate_king().

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
src/bitboard.cpp
src/bitboard.h
src/evaluate.cpp
src/pawns.cpp
src/pawns.h

index 18b7bb28206d5db0d0a23c026b2caf12558fc466..9bd16d2f66a96e60be496258ea550fc6bcb6277a 100644 (file)
@@ -233,6 +233,8 @@ Bitboard BishopPseudoAttacks[64];
 Bitboard RookPseudoAttacks[64];
 Bitboard QueenPseudoAttacks[64];
 
+uint8_t BitCount8Bit[256];
+
 
 ////
 //// Local definitions
@@ -382,6 +384,9 @@ namespace {
           in_front_bb(c, s) & this_and_neighboring_files_bb(s);
         OutpostMask[c][s] = in_front_bb(c, s) & neighboring_files_bb(s);
       }
+
+    for (Bitboard b = 0ULL; b < 256ULL; b++)
+        BitCount8Bit[b] = (uint8_t)count_1s(b);
   }
 
 
index f06fbe8a8a3739ab96556bd4b7531e177d073394..d2b623f609ea584a1fcee81ff87917179360889e 100644 (file)
@@ -93,6 +93,8 @@ extern Bitboard BishopPseudoAttacks[64];
 extern Bitboard RookPseudoAttacks[64];
 extern Bitboard QueenPseudoAttacks[64];
 
+extern uint8_t BitCount8Bit[256];
+
 
 ////
 //// Inline functions
index 13bc9751f051e651010521333f51e5baed2a5c4a..7f7df3c5465e491bc78775338f12de908fd3d6ab 100644 (file)
@@ -266,9 +266,6 @@ namespace {
   const int PawnTableSize = 16384;
   const int MaterialTableSize = 1024;
 
-  // Array which gives the number of nonzero bits in an 8-bit integer
-  uint8_t BitCount8Bit[256];
-
   // Function prototypes
   template<bool HasPopCnt>
   Value do_evaluate(const Position& pos, EvalInfo& ei, int threadID);
@@ -498,12 +495,6 @@ void init_eval(int threads) {
     if (!MaterialTable[i])
         MaterialTable[i] = new MaterialInfoTable(MaterialTableSize);
   }
-
-  for (Bitboard b = 0ULL; b < 256ULL; b++)
-  {
-      assert(count_1s(b) == int(uint8_t(count_1s(b))));
-      BitCount8Bit[b] = (uint8_t)count_1s(b);
-  }
 }
 
 
@@ -714,10 +705,6 @@ namespace {
     }
   }
 
-  inline Bitboard shiftRowsDown(const Bitboard& b, int num) {
-
-    return b >> (num << 3);
-  }
 
   // evaluate_king<>() assigns bonuses and penalties to a king of a given color.
 
@@ -730,19 +717,7 @@ namespace {
     // King shelter
     if (relative_rank(us, s) <= RANK_4)
     {
-        // Shelter cache lookup
-        shelter = ei.pi->kingShelter(us, s);
-        if (shelter == -1)
-        {
-            shelter = 0;
-            Bitboard pawns = p.pawns(us) & this_and_neighboring_files_bb(s);
-            Rank r = square_rank(s);
-            for (int i = 1; i < 4; i++)
-                shelter += BitCount8Bit[shiftRowsDown(pawns, r+i*sign) & 0xFF] * (128 >> i);
-
-            // Cache shelter value in pawn info
-            ei.pi->setKingShelter(us, s, shelter);
-        }
+        shelter = ei.pi->get_king_shelter(p, us, s);
         ei.mgValue += sign * Value(shelter);
     }
 
index d0523ab6ae6980cd4ca42693b4cd407cade6c81a..17ca9d1e3228181a365cf203ea20e5f674645b9f 100644 (file)
@@ -385,3 +385,21 @@ PawnInfo *PawnInfoTable::get_pawn_info(const Position &pos) {
   pi->egValue = int16_t(egValue[WHITE] - egValue[BLACK]);
   return pi;
 }
+
+
+/// PawnInfo::updateShelter calculates and caches king shelter. It is called
+/// only when king square changes, about 20% of total get_king_shelter() calls.
+int PawnInfo::updateShelter(const Position& pos, Color c, Square ksq) {
+
+  int shelter = 0;
+  Bitboard pawns = pos.pawns(c) & this_and_neighboring_files_bb(ksq);
+  unsigned r = ksq & (7 << 3);
+  for (int i = 1, k = (c ? -8 : 8); i < 4; i++)
+  {
+      r += k;
+      shelter += BitCount8Bit[(pawns >> r) & 0xFF] * (128 >> i);
+  }
+  kingSquares[c] = ksq;
+  kingShelters[c] = shelter;
+  return shelter;
+}
index 4b4a8a55f3d0c4e4de6437bdffc017e506ee8373..ba94a72319ef665728206d89aa68ac39e5785c0a 100644 (file)
@@ -55,11 +55,11 @@ public:
   int file_is_half_open(Color c, File f) const;
   int has_open_file_to_left(Color c, File f) const;
   int has_open_file_to_right(Color c, File f) const;
-  int kingShelter(Color c, Square ksq) const;
-  void setKingShelter(Color c, Square ksq, int value);
+  int get_king_shelter(const Position& pos, Color c, Square ksq);
 
 private:
   inline void clear();
+  int updateShelter(const Position& pos, Color c, Square ksq);
 
   Key key;
   Bitboard passedPawns;
@@ -124,13 +124,8 @@ inline int PawnInfo::has_open_file_to_right(Color c, File f) const {
   return halfOpenFiles[c] & ~((1 << int(f+1)) - 1);
 }
 
-inline int PawnInfo::kingShelter(Color c, Square ksq) const {
-  return (kingSquares[c] == ksq ?  kingShelters[c] : -1);
-}
-
-inline void PawnInfo::setKingShelter(Color c, Square ksq, int value) {
-  kingSquares[c] = ksq;
-  kingShelters[c] = (int16_t)value;
+inline int PawnInfo::get_king_shelter(const Position& pos, Color c, Square ksq) {
+  return (kingSquares[c] == ksq ? kingShelters[c] : updateShelter(pos, c, ksq));
 }
 
 inline void PawnInfo::clear() {