Skip to content

Commit

Permalink
bench 1257882
Browse files Browse the repository at this point in the history
  • Loading branch information
Vizvezdenec committed Oct 27, 2023
1 parent 0024133 commit 465d1b6
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 11 deletions.
13 changes: 11 additions & 2 deletions src/movepick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@ MovePicker::MovePicker(const Position& p,
const ButterflyHistory* mh,
const CapturePieceToHistory* cph,
const PieceToHistory** ch,
const PawnHistory& ph,
Move cm,
const Move* killers) :
pos(p),
mainHistory(mh),
captureHistory(cph),
continuationHistory(ch),
pawnHistory(ph),
ttMove(ttm),
refutations{{killers[0], 0}, {killers[1], 0}, {cm, 0}},
depth(d) {
Expand All @@ -110,11 +112,13 @@ MovePicker::MovePicker(const Position& p,
const ButterflyHistory* mh,
const CapturePieceToHistory* cph,
const PieceToHistory** ch,
const PawnHistory& ph,
Square rs) :
pos(p),
mainHistory(mh),
captureHistory(cph),
continuationHistory(ch),
pawnHistory(ph),
ttMove(ttm),
recaptureSquare(rs),
depth(d) {
Expand All @@ -125,9 +129,11 @@ MovePicker::MovePicker(const Position& p,

// Constructor for ProbCut: we generate captures with SEE greater
// than or equal to the given threshold.
MovePicker::MovePicker(const Position& p, Move ttm, Value th, const CapturePieceToHistory* cph) :
MovePicker::MovePicker(
const Position& p, Move ttm, Value th, const CapturePieceToHistory* cph, const PawnHistory& ph) :
pos(p),
captureHistory(cph),
pawnHistory(ph),
ttMove(ttm),
threshold(th) {
assert(!pos.checkers());
Expand Down Expand Up @@ -203,6 +209,8 @@ void MovePicker::score() {
: pt != PAWN ? bool(to & threatenedByPawn) * 15000
: 0)
: 0;

m.value += pawnHistory.get(pos, pos.pawn_key(), m);
}

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

Expand Down
40 changes: 38 additions & 2 deletions src/movepick.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@

#include "movegen.h"
#include "types.h"
#include "position.h"

namespace Stockfish {
class Position;

// 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
Expand Down Expand Up @@ -89,6 +89,39 @@ 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 Down Expand Up @@ -135,6 +168,7 @@ class MovePicker {
const ButterflyHistory*,
const CapturePieceToHistory*,
const PieceToHistory**,
const PawnHistory&,
Move,
const Move*);
MovePicker(const Position&,
Expand All @@ -143,8 +177,9 @@ class MovePicker {
const ButterflyHistory*,
const CapturePieceToHistory*,
const PieceToHistory**,
const PawnHistory&,
Square);
MovePicker(const Position&, Move, Value, const CapturePieceToHistory*);
MovePicker(const Position&, Move, Value, const CapturePieceToHistory*, const PawnHistory&);
Move next_move(bool skipQuiets = false);

private:
Expand All @@ -159,6 +194,7 @@ class MovePicker {
const ButterflyHistory* mainHistory;
const CapturePieceToHistory* captureHistory;
const PieceToHistory** continuationHistory;
const PawnHistory& pawnHistory;
Move ttMove;
ExtMove refutations[3], *cur, *endMoves, *endBadCaptures;
int stage;
Expand Down
17 changes: 14 additions & 3 deletions src/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace Zobrist {
Key psq[PIECE_NB][SQUARE_NB];
Key enpassant[FILE_NB];
Key castling[CASTLING_RIGHT_NB];
Key side;
Key side, noPawns;
}

namespace {
Expand Down Expand Up @@ -128,7 +128,8 @@ void Position::init() {
for (int cr = NO_CASTLING; cr <= ANY_CASTLING; ++cr)
Zobrist::castling[cr] = rng.rand<Key>();

Zobrist::side = rng.rand<Key>();
Zobrist::side = rng.rand<Key>();
Zobrist::noPawns = rng.rand<Key>();

// Prepare the cuckoo tables
std::memset(cuckoo, 0, sizeof(cuckoo));
Expand Down Expand Up @@ -337,6 +338,7 @@ void Position::set_check_info() const {
void Position::set_state() const {

st->key = st->materialKey = 0;
st->pawnKey = Zobrist::noPawns;
st->nonPawnMaterial[WHITE] = st->nonPawnMaterial[BLACK] = VALUE_ZERO;
st->checkersBB = attackers_to(square<KING>(sideToMove)) & pieces(~sideToMove);

Expand All @@ -348,7 +350,10 @@ void Position::set_state() const {
Piece pc = piece_on(s);
st->key ^= Zobrist::psq[pc][s];

if (type_of(pc) != KING && type_of(pc) != PAWN)
if (type_of(pc) == PAWN)
st->pawnKey ^= Zobrist::psq[pc][s];

else if (type_of(pc) != KING)
st->nonPawnMaterial[color_of(pc)] += PieceValue[pc];
}

Expand Down Expand Up @@ -728,6 +733,8 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
assert(piece_on(to) == NO_PIECE);
assert(piece_on(capsq) == make_piece(them, PAWN));
}

st->pawnKey ^= Zobrist::psq[captured][capsq];
}
else
st->nonPawnMaterial[them] -= PieceValue[captured];
Expand Down Expand Up @@ -806,13 +813,17 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {

// Update hash keys
k ^= Zobrist::psq[pc][to] ^ Zobrist::psq[promotion][to];
st->pawnKey ^= Zobrist::psq[pc][to];
st->materialKey ^=
Zobrist::psq[promotion][pieceCount[promotion] - 1] ^ Zobrist::psq[pc][pieceCount[pc]];

// Update material
st->nonPawnMaterial[us] += PieceValue[promotion];
}

// Update pawn hash key
st->pawnKey ^= Zobrist::psq[pc][from] ^ Zobrist::psq[pc][to];

// Reset rule 50 draw counter
st->rule50 = 0;
}
Expand Down
4 changes: 4 additions & 0 deletions src/position.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct StateInfo {

// Copied when making a move
Key materialKey;
Key pawnKey;
Value nonPawnMaterial[COLOR_NB];
int castlingRights;
int rule50;
Expand Down Expand Up @@ -146,6 +147,7 @@ class Position {
Key key() const;
Key key_after(Move m) const;
Key material_key() const;
Key pawn_key() const;

// Other properties of the position
Color side_to_move() const;
Expand Down Expand Up @@ -293,6 +295,8 @@ inline Key Position::adjust_key50(Key k) const {
return st->rule50 < 14 - AfterMove ? k : k ^ make_key((st->rule50 - (14 - AfterMove)) / 8);
}

inline Key Position::pawn_key() const { return st->pawnKey; }

inline Key Position::material_key() const { return st->materialKey; }

inline Value Position::non_pawn_material(Color c) const { return st->nonPawnMaterial[c]; }
Expand Down
13 changes: 9 additions & 4 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,8 @@ Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, boo
{
assert(probCutBeta < VALUE_INFINITE);

MovePicker mp(pos, ttMove, probCutBeta - ss->staticEval, &captureHistory);
MovePicker mp(pos, ttMove, probCutBeta - ss->staticEval, &captureHistory,
thisThread->pawnHistory);

while ((move = mp.next_move()) != MOVE_NONE)
if (move != excludedMove && pos.legal(move))
Expand Down Expand Up @@ -904,7 +905,7 @@ Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, boo
prevSq != SQ_NONE ? thisThread->counterMoves[pos.piece_on(prevSq)][prevSq] : MOVE_NONE;

MovePicker mp(pos, ttMove, depth, &thisThread->mainHistory, &captureHistory, contHist,
countermove, ss->killers);
thisThread->pawnHistory, countermove, ss->killers);

value = bestValue;
moveCountPruning = singularQuietLMR = false;
Expand Down Expand Up @@ -988,7 +989,8 @@ 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)];
+ (*contHist[3])[movedPiece][to_sq(move)]
+ thisThread->pawnHistory.get(pos, pos.pawn_key(), move);

// Continuation history based pruning (~2 Elo)
if (lmrDepth < 6 && history < -3498 * depth)
Expand Down Expand Up @@ -1463,7 +1465,7 @@ Value qsearch(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth) {
// will be generated.
Square prevSq = is_ok((ss - 1)->currentMove) ? to_sq((ss - 1)->currentMove) : SQ_NONE;
MovePicker mp(pos, ttMove, depth, &thisThread->mainHistory, &thisThread->captureHistory,
contHist, prevSq);
contHist, thisThread->pawnHistory, prevSq);

int quietCheckEvasions = 0;

Expand Down Expand Up @@ -1672,9 +1674,12 @@ 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;

// 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->mainHistory[us][from_to(quietsSearched[i])] << -bestMoveBonus;
update_continuation_histories(ss, pos.moved_piece(quietsSearched[i]),
to_sq(quietsSearched[i]), -bestMoveBonus);
Expand Down
1 change: 1 addition & 0 deletions src/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ void Thread::clear() {
counterMoves.fill(MOVE_NONE);
mainHistory.fill(0);
captureHistory.fill(0);
pawnHistory.fill(0);

for (bool inCheck : {false, true})
for (StatsType c : {NoCaptures, Captures})
Expand Down
1 change: 1 addition & 0 deletions src/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Thread {
ButterflyHistory mainHistory;
CapturePieceToHistory captureHistory;
ContinuationHistory continuationHistory[2][2];
PawnHistory pawnHistory;
};


Expand Down

0 comments on commit 465d1b6

Please sign in to comment.