assert(is_ok());
assert(move_is_ok(m));
+ Bitboard key = st->key;
+
// Copy some fields of old state to our new StateInfo object except the
// ones which are recalculated from scratch anyway, then switch our state
// pointer to point to the new, ready to be updated, state.
// Save the current key to the history[] array, in order to be able to
// 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
// case of non-reversible moves is taken care of later.
st->rule50++;
- // Update side to move
- st->key ^= zobSideToMove;
-
if (move_is_castle(m))
- do_castle_move(m);
- else
{
- Color us = side_to_move();
- Color them = opposite_color(us);
- Square from = move_from(m);
- Square to = move_to(m);
- bool ep = move_is_ep(m);
- bool pm = move_is_promotion(m);
+ st->key = key;
+ do_castle_move(m);
+ return;
+ }
- Piece piece = piece_on(from);
- PieceType pt = type_of_piece(piece);
+ Color us = side_to_move();
+ Color them = opposite_color(us);
+ Square from = move_from(m);
+ Square to = move_to(m);
+ bool ep = move_is_ep(m);
+ bool pm = move_is_promotion(m);
- assert(color_of_piece_on(from) == us);
- assert(color_of_piece_on(to) == them || square_is_empty(to));
- assert(!(ep || pm) || piece == piece_of_color_and_type(us, PAWN));
- assert(!pm || relative_rank(us, to) == RANK_8);
+ Piece piece = piece_on(from);
+ PieceType pt = type_of_piece(piece);
- st->capture = ep ? PAWN : type_of_piece_on(to);
+ assert(color_of_piece_on(from) == us);
+ assert(color_of_piece_on(to) == them || square_is_empty(to));
+ assert(!(ep || pm) || piece == piece_of_color_and_type(us, PAWN));
+ assert(!pm || relative_rank(us, to) == RANK_8);
- if (st->capture)
- do_capture_move(st->capture, them, to, ep);
+ st->capture = ep ? PAWN : type_of_piece_on(to);
- // Update hash key
- st->key ^= zobrist[us][pt][from] ^ zobrist[us][pt][to];
+ if (st->capture)
+ do_capture_move(key, st->capture, them, to, ep);
- // Reset en passant square
- if (st->epSquare != SQ_NONE)
- {
- st->key ^= zobEp[st->epSquare];
- st->epSquare = SQ_NONE;
- }
+ // Update hash key
+ key ^= zobrist[us][pt][from] ^ zobrist[us][pt][to];
- // Update castle rights, try to shortcut a common case
- if ((castleRightsMask[from] & castleRightsMask[to]) != ALL_CASTLES)
- {
- st->key ^= zobCastle[st->castleRights];
- st->castleRights &= castleRightsMask[from];
- st->castleRights &= castleRightsMask[to];
- st->key ^= zobCastle[st->castleRights];
- }
+ // Reset en passant square
+ if (st->epSquare != SQ_NONE)
+ {
+ key ^= zobEp[st->epSquare];
+ st->epSquare = SQ_NONE;
+ }
- // Prefetch TT access as soon as we know key is updated
- TT.prefetch(st->key);
+ // Update castle rights, try to shortcut a common case
+ if ((castleRightsMask[from] & castleRightsMask[to]) != ALL_CASTLES)
+ {
+ key ^= zobCastle[st->castleRights];
+ st->castleRights &= castleRightsMask[from];
+ st->castleRights &= castleRightsMask[to];
+ key ^= zobCastle[st->castleRights];
+ }
- // Move the piece
- Bitboard move_bb = make_move_bb(from, to);
- do_move_bb(&(byColorBB[us]), move_bb);
- do_move_bb(&(byTypeBB[pt]), move_bb);
- do_move_bb(&(byTypeBB[0]), move_bb); // HACK: byTypeBB[0] == occupied squares
+ // Prefetch TT access as soon as we know key is updated
+ TT.prefetch(key);
- board[to] = board[from];
- board[from] = EMPTY;
+ // Move the piece
+ Bitboard move_bb = make_move_bb(from, to);
+ do_move_bb(&(byColorBB[us]), move_bb);
+ do_move_bb(&(byTypeBB[pt]), move_bb);
+ do_move_bb(&(byTypeBB[0]), move_bb); // HACK: byTypeBB[0] == occupied squares
- // If the moving piece was a pawn do some special extra work
- if (pt == PAWN)
- {
- // Reset rule 50 draw counter
- st->rule50 = 0;
-
- // Update pawn hash key
- st->pawnKey ^= zobrist[us][PAWN][from] ^ zobrist[us][PAWN][to];
-
- // Set en passant square, only if moved pawn can be captured
- if (abs(int(to) - int(from)) == 16)
- {
- if ( (us == WHITE && (pawn_attacks(WHITE, from + DELTA_N) & pawns(BLACK)))
- || (us == BLACK && (pawn_attacks(BLACK, from + DELTA_S) & pawns(WHITE))))
- {
- st->epSquare = Square((int(from) + int(to)) / 2);
- st->key ^= zobEp[st->epSquare];
- }
- }
- }
+ board[to] = board[from];
+ board[from] = EMPTY;
- // Update incremental scores
- st->mgValue += pst_delta<MidGame>(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;
- // 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];
- // 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 (pt == PAWN)
+ {
+ // Reset rule 50 draw counter
+ st->rule50 = 0;
- if (pm)
- {
- PieceType promotion = move_promotion_piece(m);
+ // Update pawn hash key
+ st->pawnKey ^= zobrist[us][PAWN][from] ^ zobrist[us][PAWN][to];
- assert(promotion >= KNIGHT && promotion <= QUEEN);
+ // Set en passant square, only if moved pawn can be captured
+ if (abs(int(to) - int(from)) == 16)
+ {
+ if ( (us == WHITE && (pawn_attacks(WHITE, from + DELTA_N) & pawns(BLACK)))
+ || (us == BLACK && (pawn_attacks(BLACK, from + DELTA_S) & pawns(WHITE))))
+ {
+ st->epSquare = Square((int(from) + int(to)) / 2);
+ key ^= zobEp[st->epSquare];
+ }
+ }
+ }
- // Insert promoted piece instead of pawn
- clear_bit(&(byTypeBB[PAWN]), to);
- set_bit(&(byTypeBB[promotion]), to);
- board[to] = piece_of_color_and_type(us, promotion);
+ // Update incremental scores
+ st->mgValue += pst_delta<MidGame>(piece, from, to);
+ st->egValue += pst_delta<EndGame>(piece, from, to);
- // Partially revert hash keys update
- st->key ^= zobrist[us][PAWN][to] ^ zobrist[us][promotion][to];
- st->pawnKey ^= zobrist[us][PAWN][to];
+ if (pm)
+ {
+ PieceType promotion = move_promotion_piece(m);
- // Update material key
- st->materialKey ^= zobMaterial[us][PAWN][pieceCount[us][PAWN]];
- st->materialKey ^= zobMaterial[us][promotion][pieceCount[us][promotion]+1];
+ assert(promotion >= KNIGHT && promotion <= QUEEN);
- // Update piece counts
- pieceCount[us][PAWN]--;
- pieceCount[us][promotion]++;
+ // Insert promoted piece instead of pawn
+ clear_bit(&(byTypeBB[PAWN]), to);
+ set_bit(&(byTypeBB[promotion]), to);
+ board[to] = piece_of_color_and_type(us, promotion);
- // Update piece lists
- pieceList[us][PAWN][index[from]] = pieceList[us][PAWN][pieceCount[us][PAWN]];
- index[pieceList[us][PAWN][index[from]]] = index[from];
- pieceList[us][promotion][pieceCount[us][promotion] - 1] = to;
- index[to] = pieceCount[us][promotion] - 1;
+ // Update piece counts
+ pieceCount[us][PAWN]--;
+ pieceCount[us][promotion]++;
- // Partially revert and update incremental scores
- st->mgValue -= pst<MidGame>(us, PAWN, to);
- st->mgValue += pst<MidGame>(us, promotion, to);
- st->egValue -= pst<EndGame>(us, PAWN, to);
- st->egValue += pst<EndGame>(us, promotion, to);
+ // Update piece lists
+ pieceList[us][PAWN][index[from]] = pieceList[us][PAWN][pieceCount[us][PAWN]];
+ index[pieceList[us][PAWN][index[from]]] = index[from];
+ pieceList[us][promotion][pieceCount[us][promotion] - 1] = to;
+ index[to] = pieceCount[us][promotion] - 1;
- // Update material
- st->npMaterial[us] += piece_value_midgame(promotion);
- }
+ // Partially revert hash keys update
+ key ^= zobrist[us][PAWN][to] ^ zobrist[us][promotion][to];
+ st->pawnKey ^= zobrist[us][PAWN][to];
- // Update checkers bitboard, piece must be already moved
- if (ep || pm)
- st->checkersBB = attacks_to(king_square(them), us);
- else
- {
- st->checkersBB = EmptyBoardBB; // FIXME EP ?
- Square ksq = king_square(them);
- switch (pt)
- {
- case PAWN: update_checkers<PAWN>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
- case KNIGHT: update_checkers<KNIGHT>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
- case BISHOP: update_checkers<BISHOP>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
- case ROOK: update_checkers<ROOK>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
- case QUEEN: update_checkers<QUEEN>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
- case KING: update_checkers<KING>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
- default: assert(false); break;
- }
- }
+ // 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
+ st->mgValue -= pst<MidGame>(us, PAWN, to);
+ st->mgValue += pst<MidGame>(us, promotion, to);
+ st->egValue -= pst<EndGame>(us, PAWN, to);
+ st->egValue += pst<EndGame>(us, promotion, to);
+
+ // Update material
+ 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
+ if (ep | pm)
+ st->checkersBB = attacks_to(king_square(them), us);
+ else
+ {
+ st->checkersBB = EmptyBoardBB;
+ Square ksq = king_square(them);
+ switch (pt)
+ {
+ case PAWN: update_checkers<PAWN>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
+ case KNIGHT: update_checkers<KNIGHT>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
+ case BISHOP: update_checkers<BISHOP>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
+ case ROOK: update_checkers<ROOK>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
+ case QUEEN: update_checkers<QUEEN>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
+ case KING: update_checkers<KING>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
+ default: assert(false); break;
+ }
}
// Finish
sideToMove = opposite_color(sideToMove);
- gamePly++;
st->mgValue += (sideToMove == WHITE)? TempoValueMidgame : -TempoValueMidgame;
st->egValue += (sideToMove == WHITE)? TempoValueEndgame : -TempoValueEndgame;
-
- assert(is_ok());
}
/// Position::do_capture_move() is a private method used to update captured
/// 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(to == st->epSquare);
assert(relative_rank(opposite_color(them), to) == RANK_6);
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));
board[capsq] = EMPTY;
// Remove captured piece
clear_bit(&(byColorBB[them]), capsq);
clear_bit(&(byTypeBB[capture]), capsq);
- clear_bit(&(byTypeBB[0]), capsq); // HACK: byTypeBB[0] == occupied squares
+ clear_bit(&(byTypeBB[0]), capsq);
// 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 (capture == PAWN)
// Update checkers BB
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;
}
sideToMove = opposite_color(sideToMove);
if (move_is_castle(m))
- undo_castle_move(m);
- else
{
- Color us = side_to_move();
- Color them = opposite_color(us);
- Square from = move_from(m);
- Square to = move_to(m);
- bool ep = move_is_ep(m);
- bool pm = move_is_promotion(m);
+ undo_castle_move(m);
+ return;
+ }
- PieceType piece = type_of_piece_on(to);
+ Color us = side_to_move();
+ Color them = opposite_color(us);
+ Square from = move_from(m);
+ Square to = move_to(m);
+ bool ep = move_is_ep(m);
+ bool pm = move_is_promotion(m);
- assert(square_is_empty(from));
- assert(color_of_piece_on(to) == us);
- assert(!pm || relative_rank(us, to) == RANK_8);
- assert(!ep || to == st->previous->epSquare);
- assert(!ep || relative_rank(us, to) == RANK_6);
- assert(!ep || piece_on(to) == piece_of_color_and_type(us, PAWN));
+ PieceType pt = type_of_piece_on(to);
- if (pm)
- {
- PieceType promotion = move_promotion_piece(m);
+ assert(square_is_empty(from));
+ assert(color_of_piece_on(to) == us);
+ assert(!pm || relative_rank(us, to) == RANK_8);
+ assert(!ep || to == st->previous->epSquare);
+ assert(!ep || relative_rank(us, to) == RANK_6);
+ assert(!ep || piece_on(to) == piece_of_color_and_type(us, PAWN));
- assert(piece_on(to) == piece_of_color_and_type(us, promotion));
- assert(promotion >= KNIGHT && promotion <= QUEEN);
+ if (pm)
+ {
+ PieceType promotion = move_promotion_piece(m);
+ pt = PAWN;
- // Replace promoted piece with a pawn
- clear_bit(&(byTypeBB[promotion]), to);
- set_bit(&(byTypeBB[PAWN]), to);
+ assert(promotion >= KNIGHT && promotion <= QUEEN);
+ assert(piece_on(to) == piece_of_color_and_type(us, promotion));
- // 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];
+ // Replace promoted piece with a pawn
+ clear_bit(&(byTypeBB[promotion]), to);
+ set_bit(&(byTypeBB[PAWN]), to);
- // Update piece counts
- pieceCount[us][promotion]--;
- pieceCount[us][PAWN]++;
+ // Update piece counts
+ pieceCount[us][promotion]--;
+ 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
- Bitboard move_bb = make_move_bb(to, from);
- do_move_bb(&(byColorBB[us]), move_bb);
- do_move_bb(&(byTypeBB[piece]), move_bb);
- do_move_bb(&(byTypeBB[0]), move_bb); // HACK: byTypeBB[0] == occupied squares
- board[from] = piece_of_color_and_type(us, piece);
- board[to] = EMPTY;
+ // Put the piece back at the source square
+ Bitboard move_bb = make_move_bb(to, from);
+ do_move_bb(&(byColorBB[us]), move_bb);
+ do_move_bb(&(byTypeBB[pt]), move_bb);
+ do_move_bb(&(byTypeBB[0]), move_bb); // HACK: byTypeBB[0] == occupied squares
- // If the moving piece was a king, update the king square
- if (piece == KING)
- kingSquare[us] = from;
+ board[from] = piece_of_color_and_type(us, pt);
+ board[to] = EMPTY;
- // Update piece list
- pieceList[us][piece][index[to]] = from;
- index[from] = index[to];
+ // If the moving piece was a king, update the king square
+ if (pt == KING)
+ kingSquare[us] = from;
- if (st->capture)
- {
- Square capsq = to;
+ // Update piece list
+ pieceList[us][pt][index[to]] = from;
+ index[from] = index[to];
+
+ if (st->capture)
+ {
+ Square capsq = to;
- if (ep)
- capsq = (us == WHITE)? (to - DELTA_N) : (to - DELTA_S);
+ if (ep)
+ capsq = (us == WHITE)? (to - DELTA_N) : (to - DELTA_S);
- assert(st->capture != KING);
- assert(!ep || square_is_empty(capsq));
+ assert(st->capture != KING);
+ assert(!ep || square_is_empty(capsq));
- // Restore the captured piece
- set_bit(&(byColorBB[them]), capsq);
- set_bit(&(byTypeBB[st->capture]), capsq);
- set_bit(&(byTypeBB[0]), capsq);
- board[capsq] = piece_of_color_and_type(them, st->capture);
+ // Restore the captured piece
+ set_bit(&(byColorBB[them]), capsq);
+ set_bit(&(byTypeBB[st->capture]), capsq);
+ set_bit(&(byTypeBB[0]), capsq);
- // Update piece list
- pieceList[them][st->capture][pieceCount[them][st->capture]] = capsq;
- index[capsq] = pieceCount[them][st->capture];
+ board[capsq] = piece_of_color_and_type(them, st->capture);
- // Update piece count
- pieceCount[them][st->capture]++;
- }
+ // Update piece list
+ pieceList[them][st->capture][pieceCount[them][st->capture]] = capsq;
+ index[capsq] = pieceCount[them][st->capture];
+
+ // Update piece count
+ pieceCount[them][st->capture]++;
}
// Finally point our state pointer back to the previous state
st = st->previous;
-
- assert(is_ok());
}
int tmp = index[rto]; // Necessary because we may have rto == kfrom in FRC.
index[kfrom] = index[kto];
index[rfrom] = tmp;
+
+ // Finally point our state pointer back to the previous state
+ st = st->previous;
}