]> git.sesse.net Git - stockfish/blobdiff - src/movepick.h
Unify stats update()
[stockfish] / src / movepick.h
index 34cac635cfb01d83d763e8bc55850b1eee15c91e..d6451f6bf0ad4575212802a1708c4792a2ce5887 100644 (file)
 #include "types.h"
 
 /// StatBoards is a generic 2-dimensional array used to store various statistics
-template<int Size1, int Size2, typename T = int>
+template<int Size1, int Size2, typename T = int16_t>
 struct StatBoards : public std::array<std::array<T, Size2>, Size1> {
 
   void fill(const T& v) {
     T* p = &(*this)[0][0];
     std::fill(p, p + sizeof(*this) / sizeof(*p), v);
   }
+
+  void update(T& entry, int bonus, const int D) {
+
+    assert([&]{
+      int v = entry + bonus * 32 - entry * abs(bonus) / D;
+      return INT16_MIN < v && v < INT16_MAX;
+    }());
+
+    assert(abs(bonus) <= D); // Consistency check for below formula
+
+    entry += bonus * 32 - entry * abs(bonus) / D;
+
+    assert(abs(entry) <= 32 * D);
+  }
 };
 
 /// ButterflyBoards are 2 tables (one for each color) indexed by the move's from
@@ -49,42 +63,27 @@ typedef StatBoards<PIECE_NB, SQUARE_NB> PieceToBoards;
 /// ordering decisions. It uses ButterflyBoards as backing store.
 struct ButterflyHistory : public ButterflyBoards {
 
-  void update(Color c, Move m, int v) {
-
-    const int D = 324;
-    int& entry = (*this)[c][from_to(m)];
-
-    assert(abs(v) <= D); // Consistency check for below formula
-
-    entry += v * 32 - entry * abs(v) / D;
-
-    assert(abs(entry) <= 32 * D);
+  void update(Color c, Move m, int bonus) {
+    StatBoards::update((*this)[c][from_to(m)], bonus, 324);
   }
 };
 
 /// PieceToHistory is like ButterflyHistory, but is based on PieceToBoards
 struct PieceToHistory : public PieceToBoards {
 
-  void update(Piece pc, Square to, int v) {
-
-    const int D = 936;
-    int& entry = (*this)[pc][to];
-
-    assert(abs(v) <= D); // Consistency check for below formula
-
-    entry += v * 32 - entry * abs(v) / D;
-
-    assert(abs(entry) <= 32 * D);
+  void update(Piece pc, Square to, int bonus) {
+    StatBoards::update((*this)[pc][to], bonus, 936);
   }
 };
 
-/// CounterMoveStat stores counter moves indexed by [piece][to] of the previous
+/// CounterMoveHistory stores counter moves indexed by [piece][to] of the previous
 /// move, see chessprogramming.wikispaces.com/Countermove+Heuristic
-typedef StatBoards<PIECE_NB, SQUARE_NB, Move> CounterMoveStat;
+typedef StatBoards<PIECE_NB, SQUARE_NB, Move> CounterMoveHistory;
 
-/// CounterMoveHistoryStat is like CounterMoveStat but instead of a move it
-/// stores a full history (based on PieceTo boards instead of ButterflyBoards).
-typedef StatBoards<PIECE_NB, SQUARE_NB, PieceToHistory> CounterMoveHistoryStat;
+/// ContinuationHistory is the history of a given pair of moves, usually the
+/// current one given a previous one. History table is based on PieceToBoards
+/// instead of ButterflyBoards.
+typedef StatBoards<PIECE_NB, SQUARE_NB, PieceToHistory> ContinuationHistory;
 
 
 /// MovePicker class is used to pick one pseudo legal move at a time from the
@@ -93,17 +92,14 @@ typedef StatBoards<PIECE_NB, SQUARE_NB, PieceToHistory> CounterMoveHistoryStat;
 /// when MOVE_NONE is returned. In order to improve the efficiency of the alpha
 /// beta algorithm, MovePicker attempts to return the moves which are most likely
 /// to get a cut-off first.
-namespace Search { struct Stack; }
 
 class MovePicker {
 public:
   MovePicker(const MovePicker&) = delete;
   MovePicker& operator=(const MovePicker&) = delete;
-
   MovePicker(const Position&, Move, Value);
-  MovePicker(const Position&, Move, Depth, Square);
-  MovePicker(const Position&, Move, Depth, Search::Stack*);
-
+  MovePicker(const Position&, Move, Depth, const ButterflyHistory*, const PieceToHistory**, Square);
+  MovePicker(const Position&, Move, Depth, const ButterflyHistory*, const PieceToHistory**, Move, Move*);
   Move next_move(bool skipQuiets = false);
 
 private:
@@ -112,15 +108,14 @@ private:
   ExtMove* end() { return endMoves; }
 
   const Position& pos;
-  const Search::Stack* ss;
-  Move killers[2];
-  Move countermove;
-  Depth depth;
-  Move ttMove;
+  const ButterflyHistory* mainHistory;
+  const PieceToHistory** contHistory;
+  Move ttMove, countermove, killers[2];
+  ExtMove *cur, *endMoves, *endBadCaptures;
+  int stage;
   Square recaptureSquare;
   Value threshold;
-  int stage;
-  ExtMove *cur, *endMoves, *endBadCaptures;
+  Depth depth;
   ExtMove moves[MAX_MOVES];
 };