X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fmovepick.h;h=db0ba3c465e4e8b5aacc832c6bff57955480ba9d;hp=afd573ea459badabd1aa208b5d6dd498d49d455b;hb=e10255339fc7cb54bb0466945f759646f442f4f0;hpb=b40e45c1cc51bd228898314264d6d241478b23d8 diff --git a/src/movepick.h b/src/movepick.h index afd573ea..db0ba3c4 100644 --- a/src/movepick.h +++ b/src/movepick.h @@ -28,13 +28,23 @@ #include "types.h" /// StatBoards is a generic 2-dimensional array used to store various statistics -template +template struct StatBoards : public std::array, 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(abs(bonus) <= D); // Ensure range is [-32 * D, 32 * D] + assert(abs(32 * D) < INT16_MAX); // Ensure we don't overflow + + 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 @@ -50,15 +60,7 @@ typedef StatBoards PieceToBoards; struct ButterflyHistory : public ButterflyBoards { void update(Color c, Move m, int bonus) { - - const int D = 324; - auto& entry = (*this)[c][from_to(m)]; - - assert(abs(bonus) <= D); // Consistency check for below formula - - entry += bonus * 32 - entry * abs(bonus) / D; - - assert(abs(entry) <= 32 * D); + StatBoards::update((*this)[c][from_to(m)], bonus, 324); } }; @@ -66,15 +68,7 @@ struct ButterflyHistory : public ButterflyBoards { struct PieceToHistory : public PieceToBoards { void update(Piece pc, Square to, int bonus) { - - const int D = 936; - auto& entry = (*this)[pc][to]; - - assert(abs(bonus) <= D); // Consistency check for below formula - - entry += bonus * 32 - entry * abs(bonus) / D; - - assert(abs(entry) <= 32 * D); + StatBoards::update((*this)[pc][to], bonus, 936); } };