From 3e40bd0648ab69c04e37da50fd3f3d4beb072df2 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Thu, 21 May 2009 10:55:23 +0200 Subject: [PATCH] Introduce do_move_bb() to update bitboards after a move Avoid a clear_bit() + set_bit() sequence but update bitboards with only one xor instructions. This is faster and simplifies the code. No functional change. Signed-off-by: Marco Costalba --- src/bitboard.h | 11 +++++++++++ src/position.cpp | 26 +++++++++++--------------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/bitboard.h b/src/bitboard.h index a7b7717e..d3a4ae53 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -205,6 +205,17 @@ inline void clear_bit(Bitboard *b, Square s) { } +/// Functions used to update a bitboard after a move. This is faster +/// then calling a sequence of clear_bit() + set_bit() + +inline Bitboard make_move_bb(Square from, Square to) { + return SetMaskBB[from] | SetMaskBB[to]; +} + +inline void do_move_bb(Bitboard *b, Bitboard move_bb) { + *b ^= move_bb; +} + /// rank_bb() and file_bb() gives a bitboard containing all squares on a given /// file or rank. It is also possible to pass a square as input to these /// functions. diff --git a/src/position.cpp b/src/position.cpp index 4d132e03..93c42291 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -741,12 +741,11 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) { do_capture_move(st->capture, them, to); // Move the piece - clear_bit(&(byColorBB[us]), from); - clear_bit(&(byTypeBB[piece]), from); - clear_bit(&(byTypeBB[0]), from); // HACK: byTypeBB[0] == occupied squares - set_bit(&(byColorBB[us]), to); - set_bit(&(byTypeBB[piece]), to); - set_bit(&(byTypeBB[0]), to); // HACK: byTypeBB[0] == occupied squares + Bitboard move_bb = make_move_bb(from, to); + 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[to] = board[from]; board[from] = EMPTY; @@ -838,6 +837,7 @@ void Position::do_capture_move(PieceType capture, Color them, Square to) { // Remove captured piece clear_bit(&(byColorBB[them]), to); clear_bit(&(byTypeBB[capture]), to); + clear_bit(&(byTypeBB[0]), to); // Update hash key st->key ^= zobrist[them][capture][to]; @@ -1170,17 +1170,13 @@ void Position::undo_move(Move m) { assert(color_of_piece_on(to) == us); // Put the piece back at the source square + Bitboard move_bb = make_move_bb(to, from); piece = type_of_piece_on(to); - set_bit(&(byColorBB[us]), from); - set_bit(&(byTypeBB[piece]), from); - set_bit(&(byTypeBB[0]), from); // HACK: byTypeBB[0] == occupied squares + 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); - // Clear the destination square - clear_bit(&(byColorBB[us]), to); - clear_bit(&(byTypeBB[piece]), to); - clear_bit(&(byTypeBB[0]), to); // HACK: byTypeBB[0] == occupied squares - // If the moving piece was a king, update the king square if (piece == KING) kingSquare[us] = from; @@ -1193,7 +1189,7 @@ void Position::undo_move(Move m) { { assert(st->capture != KING); - // Replace the captured piece + // Restore the captured piece set_bit(&(byColorBB[them]), to); set_bit(&(byTypeBB[st->capture]), to); set_bit(&(byTypeBB[0]), to); -- 2.39.2