sideToMove = ~sideToMove;
if (type_of(m) == CASTLE)
- {
- do_castle_move<false>(m);
- return;
- }
+ return do_castle_move<false>(m);
Color us = sideToMove;
Color them = ~us;
byColorBB[us] ^= k_from_to_bb ^ r_from_to_bb;
// Update board
- Piece king = make_piece(us, KING);
- Piece rook = make_piece(us, ROOK);
board[kfrom] = board[rfrom] = NO_PIECE;
- board[kto] = king;
- board[rto] = rook;
+ board[kto] = make_piece(us, KING);
+ board[rto] = make_piece(us, ROOK);
// Update piece lists
pieceList[us][KING][index[kfrom]] = kto;
st->capturedType = NO_PIECE_TYPE;
// Update incremental scores
- st->psqScore += psq_delta(king, kfrom, kto);
- st->psqScore += psq_delta(rook, rfrom, rto);
+ st->psqScore += psq_delta(make_piece(us, KING), kfrom, kto);
+ st->psqScore += psq_delta(make_piece(us, ROOK), rfrom, rto);
// Update hash key
st->key ^= Zobrist::psq[us][KING][kfrom] ^ Zobrist::psq[us][KING][kto];
}
-/// Position::do_null_move() is used to do/undo a "null move": It flips the side
-/// to move and updates the hash key without executing any move on the board.
-template<bool Do>
-void Position::do_null_move(StateInfo& backupSt) {
+/// Position::do(undo)_null_move() is used to do(undo) a "null move": It flips
+/// the side to move without executing any move on the board.
- assert(!checkers());
+void Position::do_null_move(StateInfo& newSt) {
- // Back up the information necessary to undo the null move to the supplied
- // StateInfo object. Note that differently from normal case here backupSt
- // is actually used as a backup storage not as the new state. This reduces
- // the number of fields to be copied.
- StateInfo* src = Do ? st : &backupSt;
- StateInfo* dst = Do ? &backupSt : st;
+ assert(!checkers());
- dst->key = src->key;
- dst->epSquare = src->epSquare;
- dst->psqScore = src->psqScore;
- dst->rule50 = src->rule50;
- dst->pliesFromNull = src->pliesFromNull;
+ memcpy(&newSt, st, sizeof(StateInfo)); // Fully copy here
- sideToMove = ~sideToMove;
+ newSt.previous = st;
+ st = &newSt;
- if (Do)
+ if (st->epSquare != SQ_NONE)
{
- if (st->epSquare != SQ_NONE)
- st->key ^= Zobrist::enpassant[file_of(st->epSquare)];
-
- st->key ^= Zobrist::side;
- prefetch((char*)TT.first_entry(st->key));
-
+ st->key ^= Zobrist::enpassant[file_of(st->epSquare)];
st->epSquare = SQ_NONE;
- st->rule50++;
- st->pliesFromNull = 0;
}
+ st->key ^= Zobrist::side;
+ prefetch((char*)TT.first_entry(st->key));
+
+ st->rule50++;
+ st->pliesFromNull = 0;
+
+ sideToMove = ~sideToMove;
+
assert(pos_is_ok());
}
-// Explicit template instantiations
-template void Position::do_null_move<false>(StateInfo& backupSt);
-template void Position::do_null_move<true>(StateInfo& backupSt);
+void Position::undo_null_move() {
+
+ assert(!checkers());
+
+ st = st->previous;
+ sideToMove = ~sideToMove;
+}
/// Position::see() is a static exchange evaluator: It tries to estimate the