]> git.sesse.net Git - stockfish/commitdiff
Introduce do_move_bb() to update bitboards after a move
authorMarco Costalba <mcostalba@gmail.com>
Thu, 21 May 2009 08:55:23 +0000 (10:55 +0200)
committerMarco Costalba <mcostalba@gmail.com>
Thu, 21 May 2009 08:55:23 +0000 (10:55 +0200)
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 <mcostalba@gmail.com>
src/bitboard.h
src/position.cpp

index a7b7717e71b4700b5d9d7c4bd3b408698b38c41f..d3a4ae53e5ed93137e44df2d6fc329729a2ff223 100644 (file)
@@ -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.
index 4d132e033244e602fd0f1edfeedfe3244a7759fe..93c42291b658d6d90ec9236d2a8642c3073c6037 100644 (file)
@@ -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);