mirror of
https://github.com/sockspls/badfish
synced 2025-05-02 01:29:36 +00:00
Unify patch series summary
This patch seems bigger then what actually is. It just moves some code around and adds a bit of coding style fixes to do_move() and undo_move() so to have uniformity of naming in both functions. The diffstat for the whole patch series is 239 insertions(+), 426 deletions(-) And final MSVC pgo build is even a bit faster: Before 448.051 nodes/sec After 453.810 nodes/sec (+1.3%) No functional change (tested on more then 100M of nodes) Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
parent
05e70d6740
commit
fbec55e52e
2 changed files with 237 additions and 223 deletions
128
src/position.cpp
128
src/position.cpp
|
@ -696,6 +696,8 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
|
||||||
assert(is_ok());
|
assert(is_ok());
|
||||||
assert(move_is_ok(m));
|
assert(move_is_ok(m));
|
||||||
|
|
||||||
|
Bitboard key = st->key;
|
||||||
|
|
||||||
// Copy some fields of old state to our new StateInfo object except the
|
// Copy some fields of old state to our new StateInfo object except the
|
||||||
// ones which are recalculated from scratch anyway, then switch our state
|
// ones which are recalculated from scratch anyway, then switch our state
|
||||||
// pointer to point to the new, ready to be updated, state.
|
// pointer to point to the new, ready to be updated, state.
|
||||||
|
@ -714,19 +716,23 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
|
||||||
|
|
||||||
// Save the current key to the history[] array, in order to be able to
|
// Save the current key to the history[] array, in order to be able to
|
||||||
// detect repetition draws.
|
// detect repetition draws.
|
||||||
history[gamePly] = st->key;
|
history[gamePly] = key;
|
||||||
|
gamePly++;
|
||||||
|
|
||||||
|
// Update side to move
|
||||||
|
key ^= zobSideToMove;
|
||||||
|
|
||||||
// Increment the 50 moves rule draw counter. Resetting it to zero in the
|
// Increment the 50 moves rule draw counter. Resetting it to zero in the
|
||||||
// case of non-reversible moves is taken care of later.
|
// case of non-reversible moves is taken care of later.
|
||||||
st->rule50++;
|
st->rule50++;
|
||||||
|
|
||||||
// Update side to move
|
|
||||||
st->key ^= zobSideToMove;
|
|
||||||
|
|
||||||
if (move_is_castle(m))
|
if (move_is_castle(m))
|
||||||
do_castle_move(m);
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
|
st->key = key;
|
||||||
|
do_castle_move(m);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Color us = side_to_move();
|
Color us = side_to_move();
|
||||||
Color them = opposite_color(us);
|
Color them = opposite_color(us);
|
||||||
Square from = move_from(m);
|
Square from = move_from(m);
|
||||||
|
@ -745,29 +751,29 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
|
||||||
st->capture = ep ? PAWN : type_of_piece_on(to);
|
st->capture = ep ? PAWN : type_of_piece_on(to);
|
||||||
|
|
||||||
if (st->capture)
|
if (st->capture)
|
||||||
do_capture_move(st->capture, them, to, ep);
|
do_capture_move(key, st->capture, them, to, ep);
|
||||||
|
|
||||||
// Update hash key
|
// Update hash key
|
||||||
st->key ^= zobrist[us][pt][from] ^ zobrist[us][pt][to];
|
key ^= zobrist[us][pt][from] ^ zobrist[us][pt][to];
|
||||||
|
|
||||||
// Reset en passant square
|
// Reset en passant square
|
||||||
if (st->epSquare != SQ_NONE)
|
if (st->epSquare != SQ_NONE)
|
||||||
{
|
{
|
||||||
st->key ^= zobEp[st->epSquare];
|
key ^= zobEp[st->epSquare];
|
||||||
st->epSquare = SQ_NONE;
|
st->epSquare = SQ_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update castle rights, try to shortcut a common case
|
// Update castle rights, try to shortcut a common case
|
||||||
if ((castleRightsMask[from] & castleRightsMask[to]) != ALL_CASTLES)
|
if ((castleRightsMask[from] & castleRightsMask[to]) != ALL_CASTLES)
|
||||||
{
|
{
|
||||||
st->key ^= zobCastle[st->castleRights];
|
key ^= zobCastle[st->castleRights];
|
||||||
st->castleRights &= castleRightsMask[from];
|
st->castleRights &= castleRightsMask[from];
|
||||||
st->castleRights &= castleRightsMask[to];
|
st->castleRights &= castleRightsMask[to];
|
||||||
st->key ^= zobCastle[st->castleRights];
|
key ^= zobCastle[st->castleRights];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prefetch TT access as soon as we know key is updated
|
// Prefetch TT access as soon as we know key is updated
|
||||||
TT.prefetch(st->key);
|
TT.prefetch(key);
|
||||||
|
|
||||||
// Move the piece
|
// Move the piece
|
||||||
Bitboard move_bb = make_move_bb(from, to);
|
Bitboard move_bb = make_move_bb(from, to);
|
||||||
|
@ -778,6 +784,14 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
|
||||||
board[to] = board[from];
|
board[to] = board[from];
|
||||||
board[from] = EMPTY;
|
board[from] = EMPTY;
|
||||||
|
|
||||||
|
// If the moving piece was a king, update the king square
|
||||||
|
if (pt == KING)
|
||||||
|
kingSquare[us] = to;
|
||||||
|
|
||||||
|
// Update piece lists
|
||||||
|
pieceList[us][pt][index[from]] = to;
|
||||||
|
index[to] = index[from];
|
||||||
|
|
||||||
// If the moving piece was a pawn do some special extra work
|
// If the moving piece was a pawn do some special extra work
|
||||||
if (pt == PAWN)
|
if (pt == PAWN)
|
||||||
{
|
{
|
||||||
|
@ -794,7 +808,7 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
|
||||||
|| (us == BLACK && (pawn_attacks(BLACK, from + DELTA_S) & pawns(WHITE))))
|
|| (us == BLACK && (pawn_attacks(BLACK, from + DELTA_S) & pawns(WHITE))))
|
||||||
{
|
{
|
||||||
st->epSquare = Square((int(from) + int(to)) / 2);
|
st->epSquare = Square((int(from) + int(to)) / 2);
|
||||||
st->key ^= zobEp[st->epSquare];
|
key ^= zobEp[st->epSquare];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -803,14 +817,6 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
|
||||||
st->mgValue += pst_delta<MidGame>(piece, from, to);
|
st->mgValue += pst_delta<MidGame>(piece, from, to);
|
||||||
st->egValue += pst_delta<EndGame>(piece, from, to);
|
st->egValue += pst_delta<EndGame>(piece, from, to);
|
||||||
|
|
||||||
// If the moving piece was a king, update the king square
|
|
||||||
if (pt == KING)
|
|
||||||
kingSquare[us] = to;
|
|
||||||
|
|
||||||
// Update piece lists
|
|
||||||
pieceList[us][pt][index[from]] = to;
|
|
||||||
index[to] = index[from];
|
|
||||||
|
|
||||||
if (pm)
|
if (pm)
|
||||||
{
|
{
|
||||||
PieceType promotion = move_promotion_piece(m);
|
PieceType promotion = move_promotion_piece(m);
|
||||||
|
@ -822,14 +828,6 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
|
||||||
set_bit(&(byTypeBB[promotion]), to);
|
set_bit(&(byTypeBB[promotion]), to);
|
||||||
board[to] = piece_of_color_and_type(us, promotion);
|
board[to] = piece_of_color_and_type(us, promotion);
|
||||||
|
|
||||||
// Partially revert hash keys update
|
|
||||||
st->key ^= zobrist[us][PAWN][to] ^ zobrist[us][promotion][to];
|
|
||||||
st->pawnKey ^= zobrist[us][PAWN][to];
|
|
||||||
|
|
||||||
// Update material key
|
|
||||||
st->materialKey ^= zobMaterial[us][PAWN][pieceCount[us][PAWN]];
|
|
||||||
st->materialKey ^= zobMaterial[us][promotion][pieceCount[us][promotion]+1];
|
|
||||||
|
|
||||||
// Update piece counts
|
// Update piece counts
|
||||||
pieceCount[us][PAWN]--;
|
pieceCount[us][PAWN]--;
|
||||||
pieceCount[us][promotion]++;
|
pieceCount[us][promotion]++;
|
||||||
|
@ -840,6 +838,14 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
|
||||||
pieceList[us][promotion][pieceCount[us][promotion] - 1] = to;
|
pieceList[us][promotion][pieceCount[us][promotion] - 1] = to;
|
||||||
index[to] = pieceCount[us][promotion] - 1;
|
index[to] = pieceCount[us][promotion] - 1;
|
||||||
|
|
||||||
|
// Partially revert hash keys update
|
||||||
|
key ^= zobrist[us][PAWN][to] ^ zobrist[us][promotion][to];
|
||||||
|
st->pawnKey ^= zobrist[us][PAWN][to];
|
||||||
|
|
||||||
|
// Update material key
|
||||||
|
st->materialKey ^= zobMaterial[us][PAWN][pieceCount[us][PAWN]];
|
||||||
|
st->materialKey ^= zobMaterial[us][promotion][pieceCount[us][promotion]+1];
|
||||||
|
|
||||||
// Partially revert and update incremental scores
|
// Partially revert and update incremental scores
|
||||||
st->mgValue -= pst<MidGame>(us, PAWN, to);
|
st->mgValue -= pst<MidGame>(us, PAWN, to);
|
||||||
st->mgValue += pst<MidGame>(us, promotion, to);
|
st->mgValue += pst<MidGame>(us, promotion, to);
|
||||||
|
@ -850,12 +856,15 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
|
||||||
st->npMaterial[us] += piece_value_midgame(promotion);
|
st->npMaterial[us] += piece_value_midgame(promotion);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update the key with the final value
|
||||||
|
st->key = key;
|
||||||
|
|
||||||
// Update checkers bitboard, piece must be already moved
|
// Update checkers bitboard, piece must be already moved
|
||||||
if (ep || pm)
|
if (ep | pm)
|
||||||
st->checkersBB = attacks_to(king_square(them), us);
|
st->checkersBB = attacks_to(king_square(them), us);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
st->checkersBB = EmptyBoardBB; // FIXME EP ?
|
st->checkersBB = EmptyBoardBB;
|
||||||
Square ksq = king_square(them);
|
Square ksq = king_square(them);
|
||||||
switch (pt)
|
switch (pt)
|
||||||
{
|
{
|
||||||
|
@ -868,23 +877,19 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
|
||||||
default: assert(false); break;
|
default: assert(false); break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Finish
|
// Finish
|
||||||
sideToMove = opposite_color(sideToMove);
|
sideToMove = opposite_color(sideToMove);
|
||||||
gamePly++;
|
|
||||||
|
|
||||||
st->mgValue += (sideToMove == WHITE)? TempoValueMidgame : -TempoValueMidgame;
|
st->mgValue += (sideToMove == WHITE)? TempoValueMidgame : -TempoValueMidgame;
|
||||||
st->egValue += (sideToMove == WHITE)? TempoValueEndgame : -TempoValueEndgame;
|
st->egValue += (sideToMove == WHITE)? TempoValueEndgame : -TempoValueEndgame;
|
||||||
|
|
||||||
assert(is_ok());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Position::do_capture_move() is a private method used to update captured
|
/// Position::do_capture_move() is a private method used to update captured
|
||||||
/// piece info. It is called from the main Position::do_move function.
|
/// piece info. It is called from the main Position::do_move function.
|
||||||
|
|
||||||
void Position::do_capture_move(PieceType capture, Color them, Square to, bool ep) {
|
void Position::do_capture_move(Bitboard& key, PieceType capture, Color them, Square to, bool ep) {
|
||||||
|
|
||||||
assert(capture != KING);
|
assert(capture != KING);
|
||||||
|
|
||||||
|
@ -897,7 +902,7 @@ void Position::do_capture_move(PieceType capture, Color them, Square to, bool ep
|
||||||
assert(to == st->epSquare);
|
assert(to == st->epSquare);
|
||||||
assert(relative_rank(opposite_color(them), to) == RANK_6);
|
assert(relative_rank(opposite_color(them), to) == RANK_6);
|
||||||
assert(piece_on(to) == EMPTY);
|
assert(piece_on(to) == EMPTY);
|
||||||
//assert(piece_on(from) == piece_of_color_and_type(us, PAWN));
|
assert(piece_on(from) == piece_of_color_and_type(opposite_color(them), PAWN));
|
||||||
assert(piece_on(capsq) == piece_of_color_and_type(them, PAWN));
|
assert(piece_on(capsq) == piece_of_color_and_type(them, PAWN));
|
||||||
|
|
||||||
board[capsq] = EMPTY;
|
board[capsq] = EMPTY;
|
||||||
|
@ -906,10 +911,10 @@ void Position::do_capture_move(PieceType capture, Color them, Square to, bool ep
|
||||||
// Remove captured piece
|
// Remove captured piece
|
||||||
clear_bit(&(byColorBB[them]), capsq);
|
clear_bit(&(byColorBB[them]), capsq);
|
||||||
clear_bit(&(byTypeBB[capture]), capsq);
|
clear_bit(&(byTypeBB[capture]), capsq);
|
||||||
clear_bit(&(byTypeBB[0]), capsq); // HACK: byTypeBB[0] == occupied squares
|
clear_bit(&(byTypeBB[0]), capsq);
|
||||||
|
|
||||||
// Update hash key
|
// Update hash key
|
||||||
st->key ^= zobrist[them][capture][capsq];
|
key ^= zobrist[them][capture][capsq];
|
||||||
|
|
||||||
// If the captured piece was a pawn, update pawn hash key
|
// If the captured piece was a pawn, update pawn hash key
|
||||||
if (capture == PAWN)
|
if (capture == PAWN)
|
||||||
|
@ -1025,6 +1030,12 @@ void Position::do_castle_move(Move m) {
|
||||||
|
|
||||||
// Update checkers BB
|
// Update checkers BB
|
||||||
st->checkersBB = attacks_to(king_square(them), us);
|
st->checkersBB = attacks_to(king_square(them), us);
|
||||||
|
|
||||||
|
// Finish
|
||||||
|
sideToMove = opposite_color(sideToMove);
|
||||||
|
|
||||||
|
st->mgValue += (sideToMove == WHITE)? TempoValueMidgame : -TempoValueMidgame;
|
||||||
|
st->egValue += (sideToMove == WHITE)? TempoValueEndgame : -TempoValueEndgame;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1040,9 +1051,11 @@ void Position::undo_move(Move m) {
|
||||||
sideToMove = opposite_color(sideToMove);
|
sideToMove = opposite_color(sideToMove);
|
||||||
|
|
||||||
if (move_is_castle(m))
|
if (move_is_castle(m))
|
||||||
undo_castle_move(m);
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
|
undo_castle_move(m);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Color us = side_to_move();
|
Color us = side_to_move();
|
||||||
Color them = opposite_color(us);
|
Color them = opposite_color(us);
|
||||||
Square from = move_from(m);
|
Square from = move_from(m);
|
||||||
|
@ -1050,7 +1063,7 @@ void Position::undo_move(Move m) {
|
||||||
bool ep = move_is_ep(m);
|
bool ep = move_is_ep(m);
|
||||||
bool pm = move_is_promotion(m);
|
bool pm = move_is_promotion(m);
|
||||||
|
|
||||||
PieceType piece = type_of_piece_on(to);
|
PieceType pt = type_of_piece_on(to);
|
||||||
|
|
||||||
assert(square_is_empty(from));
|
assert(square_is_empty(from));
|
||||||
assert(color_of_piece_on(to) == us);
|
assert(color_of_piece_on(to) == us);
|
||||||
|
@ -1062,41 +1075,41 @@ void Position::undo_move(Move m) {
|
||||||
if (pm)
|
if (pm)
|
||||||
{
|
{
|
||||||
PieceType promotion = move_promotion_piece(m);
|
PieceType promotion = move_promotion_piece(m);
|
||||||
|
pt = PAWN;
|
||||||
|
|
||||||
assert(piece_on(to) == piece_of_color_and_type(us, promotion));
|
|
||||||
assert(promotion >= KNIGHT && promotion <= QUEEN);
|
assert(promotion >= KNIGHT && promotion <= QUEEN);
|
||||||
|
assert(piece_on(to) == piece_of_color_and_type(us, promotion));
|
||||||
|
|
||||||
// Replace promoted piece with a pawn
|
// Replace promoted piece with a pawn
|
||||||
clear_bit(&(byTypeBB[promotion]), to);
|
clear_bit(&(byTypeBB[promotion]), to);
|
||||||
set_bit(&(byTypeBB[PAWN]), to);
|
set_bit(&(byTypeBB[PAWN]), to);
|
||||||
|
|
||||||
// Update piece list replacing promotion piece with a pawn
|
|
||||||
pieceList[us][promotion][index[to]] = pieceList[us][promotion][pieceCount[us][promotion] - 1];
|
|
||||||
index[pieceList[us][promotion][index[to]]] = index[to];
|
|
||||||
pieceList[us][PAWN][pieceCount[us][PAWN]] = to;
|
|
||||||
index[to] = pieceCount[us][PAWN];
|
|
||||||
|
|
||||||
// Update piece counts
|
// Update piece counts
|
||||||
pieceCount[us][promotion]--;
|
pieceCount[us][promotion]--;
|
||||||
pieceCount[us][PAWN]++;
|
pieceCount[us][PAWN]++;
|
||||||
|
|
||||||
piece = PAWN;
|
// Update piece list replacing promotion piece with a pawn
|
||||||
|
pieceList[us][promotion][index[to]] = pieceList[us][promotion][pieceCount[us][promotion]];
|
||||||
|
index[pieceList[us][promotion][index[to]]] = index[to];
|
||||||
|
pieceList[us][PAWN][pieceCount[us][PAWN] - 1] = to;
|
||||||
|
index[to] = pieceCount[us][PAWN] - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put the piece back at the source square
|
// Put the piece back at the source square
|
||||||
Bitboard move_bb = make_move_bb(to, from);
|
Bitboard move_bb = make_move_bb(to, from);
|
||||||
do_move_bb(&(byColorBB[us]), move_bb);
|
do_move_bb(&(byColorBB[us]), move_bb);
|
||||||
do_move_bb(&(byTypeBB[piece]), move_bb);
|
do_move_bb(&(byTypeBB[pt]), move_bb);
|
||||||
do_move_bb(&(byTypeBB[0]), move_bb); // HACK: byTypeBB[0] == occupied squares
|
do_move_bb(&(byTypeBB[0]), move_bb); // HACK: byTypeBB[0] == occupied squares
|
||||||
board[from] = piece_of_color_and_type(us, piece);
|
|
||||||
|
board[from] = piece_of_color_and_type(us, pt);
|
||||||
board[to] = EMPTY;
|
board[to] = EMPTY;
|
||||||
|
|
||||||
// If the moving piece was a king, update the king square
|
// If the moving piece was a king, update the king square
|
||||||
if (piece == KING)
|
if (pt == KING)
|
||||||
kingSquare[us] = from;
|
kingSquare[us] = from;
|
||||||
|
|
||||||
// Update piece list
|
// Update piece list
|
||||||
pieceList[us][piece][index[to]] = from;
|
pieceList[us][pt][index[to]] = from;
|
||||||
index[from] = index[to];
|
index[from] = index[to];
|
||||||
|
|
||||||
if (st->capture)
|
if (st->capture)
|
||||||
|
@ -1113,6 +1126,7 @@ void Position::undo_move(Move m) {
|
||||||
set_bit(&(byColorBB[them]), capsq);
|
set_bit(&(byColorBB[them]), capsq);
|
||||||
set_bit(&(byTypeBB[st->capture]), capsq);
|
set_bit(&(byTypeBB[st->capture]), capsq);
|
||||||
set_bit(&(byTypeBB[0]), capsq);
|
set_bit(&(byTypeBB[0]), capsq);
|
||||||
|
|
||||||
board[capsq] = piece_of_color_and_type(them, st->capture);
|
board[capsq] = piece_of_color_and_type(them, st->capture);
|
||||||
|
|
||||||
// Update piece list
|
// Update piece list
|
||||||
|
@ -1122,12 +1136,9 @@ void Position::undo_move(Move m) {
|
||||||
// Update piece count
|
// Update piece count
|
||||||
pieceCount[them][st->capture]++;
|
pieceCount[them][st->capture]++;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Finally point our state pointer back to the previous state
|
// Finally point our state pointer back to the previous state
|
||||||
st = st->previous;
|
st = st->previous;
|
||||||
|
|
||||||
assert(is_ok());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1189,6 +1200,9 @@ void Position::undo_castle_move(Move m) {
|
||||||
int tmp = index[rto]; // Necessary because we may have rto == kfrom in FRC.
|
int tmp = index[rto]; // Necessary because we may have rto == kfrom in FRC.
|
||||||
index[kfrom] = index[kto];
|
index[kfrom] = index[kto];
|
||||||
index[rfrom] = tmp;
|
index[rfrom] = tmp;
|
||||||
|
|
||||||
|
// Finally point our state pointer back to the previous state
|
||||||
|
st = st->previous;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -310,7 +310,7 @@ private:
|
||||||
void allow_ooo(Color c);
|
void allow_ooo(Color c);
|
||||||
|
|
||||||
// Helper functions for doing and undoing moves
|
// Helper functions for doing and undoing moves
|
||||||
void do_capture_move(PieceType capture, Color them, Square to, bool ep);
|
void do_capture_move(Bitboard& key, PieceType capture, Color them, Square to, bool ep);
|
||||||
void do_castle_move(Move m);
|
void do_castle_move(Move m);
|
||||||
void undo_castle_move(Move m);
|
void undo_castle_move(Move m);
|
||||||
void find_checkers();
|
void find_checkers();
|
||||||
|
|
Loading…
Add table
Reference in a new issue