Skip to content

Commit

Permalink
Unify stats update()
Browse files Browse the repository at this point in the history
Now that is a bit bigger makes sense to
unify some duplicated code.

No functional change.
  • Loading branch information
mcostalba committed Aug 17, 2017
1 parent ae6a4eb commit 9001f55
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions src/movepick.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ struct StatBoards : public std::array<std::array<T, Size2>, Size1> {
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
Expand All @@ -50,39 +64,15 @@ typedef StatBoards<PIECE_NB, SQUARE_NB> 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
assert([&]{
int v = entry + bonus * 32 - entry * abs(bonus) / D;
return INT16_MIN < v && v < INT16_MAX;
}());

entry += bonus * 32 - entry * abs(bonus) / D;

assert(abs(entry) <= 32 * D);
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 bonus) {

const int D = 936;
auto& entry = (*this)[pc][to];

assert(abs(bonus) <= D); // Consistency check for below formula
assert([&]{
int v = entry + bonus * 32 - entry * abs(bonus) / D;
return INT16_MIN < v && v < INT16_MAX;
}());

entry += bonus * 32 - entry * abs(bonus) / D;

assert(abs(entry) <= 32 * D);
StatBoards::update((*this)[pc][to], bonus, 936);
}
};

Expand Down

0 comments on commit 9001f55

Please sign in to comment.