From 48b74142efa52f0da9d4e27f9bbcbe9520499524 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Wed, 23 Sep 2009 21:45:32 +0100 Subject: [PATCH] Micro optimization of generate_piece_moves() This patch make the piece list always terminated by SQ_NONE, so that we can use a simpler and faster loop in move generation. Speedup is about 0.6%. No functional change. Signed-off-by: Marco Costalba --- src/movegen.cpp | 8 ++++---- src/position.cpp | 3 +++ src/position.h | 5 +++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/movegen.cpp b/src/movegen.cpp index edb36fea..f83dded7 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -476,10 +476,10 @@ namespace { Square from; Bitboard b; + const Square* ptr = pos.piece_list_begin(us, Piece); - for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++) + while ((from = *ptr++) != SQ_NONE) { - from = pos.piece_list(us, Piece, i); b = pos.attacks_from(from) & target; SERIALIZE_MOVES(b); } @@ -502,10 +502,10 @@ namespace { Color us, Bitboard target, Bitboard pinned) { Square from; Bitboard b; + const Square* ptr = pos.piece_list_begin(us, Piece); - for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++) + while ((from = *ptr++) != SQ_NONE) { - from = pos.piece_list(us, Piece, i); if (pinned && bit_is_set(pinned, from)) continue; diff --git a/src/position.cpp b/src/position.cpp index e759b642..443ba881 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -838,6 +838,7 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) { Square lastPawnSquare = pieceList[us][PAWN][pieceCount[us][PAWN]]; index[lastPawnSquare] = index[to]; pieceList[us][PAWN][index[lastPawnSquare]] = lastPawnSquare; + pieceList[us][PAWN][pieceCount[us][PAWN]] = SQ_NONE; index[to] = pieceCount[us][promotion] - 1; pieceList[us][promotion][index[to]] = to; @@ -943,6 +944,7 @@ void Position::do_capture_move(Bitboard& key, PieceType capture, Color them, Squ Square lastPieceSquare = pieceList[them][capture][pieceCount[them][capture]]; index[lastPieceSquare] = index[capsq]; pieceList[them][capture][index[lastPieceSquare]] = lastPieceSquare; + pieceList[them][capture][pieceCount[them][capture]] = SQ_NONE; // Reset rule 50 counter st->rule50 = 0; @@ -1099,6 +1101,7 @@ void Position::undo_move(Move m) { Square lastPromotionSquare = pieceList[us][promotion][pieceCount[us][promotion]]; index[lastPromotionSquare] = index[to]; pieceList[us][promotion][index[lastPromotionSquare]] = lastPromotionSquare; + pieceList[us][promotion][pieceCount[us][promotion]] = SQ_NONE; index[to] = pieceCount[us][PAWN] - 1; pieceList[us][PAWN][index[to]] = to; } diff --git a/src/position.h b/src/position.h index 88ee8fe2..370bb1d4 100644 --- a/src/position.h +++ b/src/position.h @@ -193,6 +193,7 @@ public: // Piece lists Square piece_list(Color c, PieceType pt, int index) const; + const Square* piece_list_begin(Color c, PieceType pt) const; // Information about attacks to or from a given square Bitboard attackers_to(Square s) const; @@ -403,6 +404,10 @@ inline Square Position::piece_list(Color c, PieceType pt, int index) const { return pieceList[c][pt][index]; } +inline const Square* Position::piece_list_begin(Color c, PieceType pt) const { + return pieceList[c][pt]; +} + inline Square Position::ep_square() const { return st->epSquare; } -- 2.39.2