From: Marco Costalba Date: Sat, 11 Jun 2011 11:41:20 +0000 (+0100) Subject: Micro-optimize castling handling in do_move() X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=b21a5e2f0638a55daeaa98ba95afc6c016ea0b6e;ds=sidebyside Micro-optimize castling handling in do_move() And better self-document the code. No functional change. Signed-off-by: Marco Costalba --- diff --git a/src/move.cpp b/src/move.cpp index 4a0a43b3..0a620781 100644 --- a/src/move.cpp +++ b/src/move.cpp @@ -59,7 +59,7 @@ const string move_to_uci(Move m, bool chess960) { return from == SQ_E1 ? "e1c1" : "e8c8"; if (move_is_promotion(m)) - promotion = char(tolower(piece_type_to_char(move_promotion_piece(m)))); + promotion = char(tolower(piece_type_to_char(promotion_piece_type(m)))); return square_to_string(from) + square_to_string(to) + promotion; } @@ -157,7 +157,7 @@ const string move_to_san(Position& pos, Move m) { if (move_is_promotion(m)) { san += '='; - san += piece_type_to_char(move_promotion_piece(m)); + san += piece_type_to_char(promotion_piece_type(m)); } } diff --git a/src/move.h b/src/move.h index 8c5ce22a..42d3bbe9 100644 --- a/src/move.h +++ b/src/move.h @@ -161,7 +161,7 @@ inline bool move_is_long_castle(Move m) { return move_is_castle(m) && (move_to(m) < move_from(m)); } -inline PieceType move_promotion_piece(Move m) { +inline PieceType promotion_piece_type(Move m) { return PieceType(((m >> 12) & 3) + 2); } diff --git a/src/position.cpp b/src/position.cpp index ef90913c..8f70b5df 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -270,7 +270,7 @@ bool Position::set_castling_rights(char token) { for (Square sq = sqH; sq >= sqA; sq--) if (piece_on(sq) == rook) { - do_allow_oo(c); + set_castle_kingside(c); initialKRFile = square_file(sq); break; } @@ -280,7 +280,7 @@ bool Position::set_castling_rights(char token) { for (Square sq = sqA; sq <= sqH; sq++) if (piece_on(sq) == rook) { - do_allow_ooo(c); + set_castle_queenside(c); initialQRFile = square_file(sq); break; } @@ -290,12 +290,12 @@ bool Position::set_castling_rights(char token) { File rookFile = File(token - 'A') + FILE_A; if (rookFile < initialKFile) { - do_allow_ooo(c); + set_castle_queenside(c); initialQRFile = rookFile; } else { - do_allow_oo(c); + set_castle_kingside(c); initialKRFile = rookFile; } } @@ -637,7 +637,7 @@ bool Position::move_is_pl(const Move m) const { return move_is_pl_slow(m); // Is not a promotion, so promotion piece must be empty - if (move_promotion_piece(m) - 2 != PIECE_TYPE_NONE) + if (promotion_piece_type(m) - 2 != PIECE_TYPE_NONE) return false; // If the from square is not occupied by a piece belonging to the side to @@ -788,7 +788,7 @@ bool Position::move_gives_check(Move m, const CheckInfo& ci) const { { clear_bit(&b, from); - switch (move_promotion_piece(m)) + switch (promotion_piece_type(m)) { case KNIGHT: return bit_is_set(attacks_from(to), ci.ksq); @@ -949,13 +949,12 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI st->epSquare = SQ_NONE; } - // Update castle rights, try to shortcut a common case - int cm = castleRightsMask[from] & castleRightsMask[to]; - if (cm != ALL_CASTLES && ((cm & st->castleRights) != st->castleRights)) + // Update castle rights if needed + if ( st->castleRights != CASTLES_NONE + && (castleRightsMask[from] & castleRightsMask[to]) != ALL_CASTLES) { key ^= zobCastle[st->castleRights]; - st->castleRights &= castleRightsMask[from]; - st->castleRights &= castleRightsMask[to]; + st->castleRights &= castleRightsMask[from] & castleRightsMask[to]; key ^= zobCastle[st->castleRights]; } @@ -998,7 +997,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI if (pm) // promotion ? { - PieceType promotion = move_promotion_piece(m); + PieceType promotion = promotion_piece_type(m); assert(promotion >= KNIGHT && promotion <= QUEEN); @@ -1278,7 +1277,7 @@ void Position::undo_move(Move m) { if (pm) // promotion ? { - PieceType promotion = move_promotion_piece(m); + PieceType promotion = promotion_piece_type(m); pt = PAWN; assert(promotion >= KNIGHT && promotion <= QUEEN); @@ -1851,10 +1850,10 @@ void Position::flip() { sideToMove = opposite_color(pos.side_to_move()); // Castling rights - if (pos.can_castle_kingside(WHITE)) do_allow_oo(BLACK); - if (pos.can_castle_queenside(WHITE)) do_allow_ooo(BLACK); - if (pos.can_castle_kingside(BLACK)) do_allow_oo(WHITE); - if (pos.can_castle_queenside(BLACK)) do_allow_ooo(WHITE); + if (pos.can_castle_kingside(WHITE)) set_castle_kingside(BLACK); + if (pos.can_castle_queenside(WHITE)) set_castle_queenside(BLACK); + if (pos.can_castle_kingside(BLACK)) set_castle_kingside(WHITE); + if (pos.can_castle_queenside(BLACK)) set_castle_queenside(WHITE); initialKFile = pos.initialKFile; initialKRFile = pos.initialKRFile; diff --git a/src/position.h b/src/position.h index 6c48b54c..56f55469 100644 --- a/src/position.h +++ b/src/position.h @@ -257,8 +257,8 @@ private: void clear(); void detach(); void put_piece(Piece p, Square s); - void do_allow_oo(Color c); - void do_allow_ooo(Color c); + void set_castle_kingside(Color c); + void set_castle_queenside(Color c); bool set_castling_rights(char token); bool move_is_pl_slow(const Move m) const; @@ -406,16 +406,24 @@ inline Square Position::king_square(Color c) const { return pieceList[c][KING][0]; } -inline bool Position::can_castle_kingside(Color side) const { - return st->castleRights & (1+int(side)); +inline bool Position::can_castle_kingside(Color c) const { + return st->castleRights & (WHITE_OO << c); } -inline bool Position::can_castle_queenside(Color side) const { - return st->castleRights & (4+4*int(side)); +inline bool Position::can_castle_queenside(Color c) const { + return st->castleRights & (WHITE_OOO << c); } -inline bool Position::can_castle(Color side) const { - return can_castle_kingside(side) || can_castle_queenside(side); +inline bool Position::can_castle(Color c) const { + return st->castleRights & ((WHITE_OO | WHITE_OOO) << c); +} + +inline void Position::set_castle_kingside(Color c) { + st->castleRights |= (WHITE_OO << c); +} + +inline void Position::set_castle_queenside(Color c) { + st->castleRights |= (WHITE_OOO << c); } inline Square Position::initial_kr_square(Color c) const { @@ -542,12 +550,4 @@ inline int Position::thread() const { return threadID; } -inline void Position::do_allow_oo(Color c) { - st->castleRights |= (1 + int(c)); -} - -inline void Position::do_allow_ooo(Color c) { - st->castleRights |= (4 + 4*int(c)); -} - #endif // !defined(POSITION_H_INCLUDED)