Skip to content

Commit

Permalink
Merge pull request #2 from locutus2/pawnHistStuffPR_refactored
Browse files Browse the repository at this point in the history
Pawn history refactoring
  • Loading branch information
Vizvezdenec authored Oct 27, 2023
2 parents 7c456fd + c280808 commit 3bd9722
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/movepick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ void MovePicker::score() {
: 0)
: 0;

m.value += pawnHistory.get(pos, pos.pawn_key(), m);
m.value += pawnHistory[pawn_structure(pos)][pc][to];
}

else // Type == EVASIONS
Expand All @@ -221,7 +221,7 @@ void MovePicker::score() {
else
m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
+ (*continuationHistory[0])[pos.moved_piece(m)][to_sq(m)]
+ pawnHistory.get(pos, pos.pawn_key(), m);
+ pawnHistory[pawn_structure(pos)][pos.moved_piece(m)][to_sq(m)];
}
}

Expand Down
39 changes: 6 additions & 33 deletions src/movepick.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@

namespace Stockfish {

constexpr int PAWN_HISTORY_SIZE = 512;

inline int pawn_structure(const Position& pos) { return pos.pawn_key() & (PAWN_HISTORY_SIZE - 1); }

// StatsEntry stores the stat table value. It is usually a number but could
// be a move or even a nested history. We use a class instead of a naked value
// to directly call history update operator<<() on the entry so to use stats
Expand Down Expand Up @@ -90,39 +94,6 @@ enum StatsType {
Captures
};

class PawnHistory {
static constexpr int max_size = 512;
using pawn_type = Stats<int16_t, 8192, COLOR_NB, max_size, PIECE_TYPE_NB, SQUARE_NB>;

public:
auto& get(const Position& pos, Key pawn_key, Move m) {
const int pawn_index = pawn_key & (max_size - 1);
const int pt = type_of(pos.moved_piece(m));
const int to = to_sq(m);

assert(pawn_index >= 0 && pawn_index < max_size);
assert(pt >= 0 && pt < PIECE_TYPE_NB);
assert(to >= 0 && to < SQUARE_NB);
return entry[pos.side_to_move()][pawn_index][pt][to];
}

const auto& get(const Position& pos, Key pawn_key, Move m) const {
const int pawn_index = pawn_key & (max_size - 1);
const int pt = type_of(pos.moved_piece(m));
const int to = to_sq(m);

assert(pawn_index >= 0 && pawn_index < max_size);
assert(pt >= 0 && pt < PIECE_TYPE_NB);
assert(to >= 0 && to < SQUARE_NB);
return entry[pos.side_to_move()][pawn_index][pt][to];
}

void fill(int val) { entry.fill(val); }

private:
pawn_type entry;
};

// ButterflyHistory records how often quiet moves have been successful or
// unsuccessful during the current search, and is used for reduction and move
// ordering decisions. It uses 2 tables (one for each color) indexed by
Expand All @@ -146,6 +117,8 @@ using PieceToHistory = Stats<int16_t, 29952, PIECE_NB, SQUARE_NB>;
// (~63 elo)
using ContinuationHistory = Stats<PieceToHistory, NOT_USED, PIECE_NB, SQUARE_NB>;

// PawnStructureHistory is addressed by the pawn structure and a move's [piece][to]
using PawnHistory = Stats<int16_t, 8192, PAWN_HISTORY_SIZE, PIECE_NB, SQUARE_NB>;

// MovePicker class is used to pick one pseudo-legal move at a time from the
// current position. The most important method is next_move(), which returns a
Expand Down
10 changes: 6 additions & 4 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, boo
int history = (*contHist[0])[movedPiece][to_sq(move)]
+ (*contHist[1])[movedPiece][to_sq(move)]
+ (*contHist[3])[movedPiece][to_sq(move)]
+ thisThread->pawnHistory.get(pos, pos.pawn_key(), move);
+ thisThread->pawnHistory[pawn_structure(pos)][movedPiece][to_sq(move)];

// Continuation history based pruning (~2 Elo)
if (lmrDepth < 6 && history < -3498 * depth)
Expand Down Expand Up @@ -1673,13 +1673,15 @@ void update_all_stats(const Position& pos,

// Increase stats for the best move in case it was a quiet move
update_quiet_stats(pos, ss, bestMove, bestMoveBonus);

thisThread->pawnHistory.get(pos, pos.pawn_key(), bestMove) << quietMoveBonus;
thisThread->pawnHistory[pawn_structure(pos)][moved_piece][to_sq(bestMove)]
<< quietMoveBonus;

// Decrease stats for all non-best quiet moves
for (int i = 0; i < quietCount; ++i)
{
thisThread->pawnHistory.get(pos, pos.pawn_key(), quietsSearched[i]) << -bestMoveBonus;
thisThread->pawnHistory[pawn_structure(pos)][pos.moved_piece(quietsSearched[i])]
[to_sq(quietsSearched[i])]
<< -bestMoveBonus;
thisThread->mainHistory[us][from_to(quietsSearched[i])] << -bestMoveBonus;
update_continuation_histories(ss, pos.moved_piece(quietsSearched[i]),
to_sq(quietsSearched[i]), -bestMoveBonus);
Expand Down

0 comments on commit 3bd9722

Please sign in to comment.