mirror of
https://github.com/sockspls/badfish
synced 2025-07-11 19:49:14 +00:00
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 <mcostalba@gmail.com>
This commit is contained in:
parent
595c7d75a2
commit
3e40bd0648
2 changed files with 22 additions and 15 deletions
|
@ -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.
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue