From 3984b8f8f0e1f53c737020c936f2a8372029545d Mon Sep 17 00:00:00 2001 From: protonspring Date: Mon, 12 Aug 2019 08:42:28 -0600 Subject: [PATCH] Consolidate CastlingSide and CastlingRights MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This is a non-functional simplification that removes CastlingSide and implements the functionality in CastlingRights (thanks to Jörg Oster for a comment on the first version of this patch). STC LLR: 2.96 (-2.94,2.94) [-3.00,1.00] Total: 53854 W: 12077 L: 12019 D: 29758 http://tests.stockfishchess.org/tests/view/5d517b940ebc5925cf107474 Closes https://github.com/official-stockfish/Stockfish/pull/2265 No functional change --- src/movegen.cpp | 6 +++--- src/pawns.cpp | 4 ++-- src/position.cpp | 17 ++++++++--------- src/position.h | 12 ++++++------ src/types.h | 16 +++++++--------- 5 files changed, 26 insertions(+), 29 deletions(-) diff --git a/src/movegen.cpp b/src/movegen.cpp index 4c609352..fc99ec26 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -219,8 +219,8 @@ namespace { template ExtMove* generate_all(const Position& pos, ExtMove* moveList, Bitboard target) { - constexpr CastlingRight OO = Us | KING_SIDE; - constexpr CastlingRight OOO = Us | QUEEN_SIDE; + constexpr CastlingRights OO = Us & KING_SIDE; + constexpr CastlingRights OOO = Us & QUEEN_SIDE; constexpr bool Checks = Type == QUIET_CHECKS; // Reduce template instantations moveList = generate_pawn_moves(pos, moveList, target); @@ -236,7 +236,7 @@ namespace { while (b) *moveList++ = make_move(ksq, pop_lsb(&b)); - if (Type != CAPTURES && pos.can_castle(CastlingRight(OO | OOO))) + if (Type != CAPTURES && pos.can_castle(CastlingRights(OO | OOO))) { if (!pos.castling_impeded(OO) && pos.can_castle(OO)) *moveList++ = make(ksq, pos.castling_rook_square(OO)); diff --git a/src/pawns.cpp b/src/pawns.cpp index 5df175b2..1cacc1e3 100644 --- a/src/pawns.cpp +++ b/src/pawns.cpp @@ -238,10 +238,10 @@ Score Entry::do_king_safety(const Position& pos) { evaluate_shelter(pos, ksq, shelter); // If we can castle use the bonus after the castling if it is bigger - if (pos.can_castle(Us | KING_SIDE)) + if (pos.can_castle(Us & KING_SIDE)) evaluate_shelter(pos, relative_square(Us, SQ_G1), shelter); - if (pos.can_castle(Us | QUEEN_SIDE)) + if (pos.can_castle(Us & QUEEN_SIDE)) evaluate_shelter(pos, relative_square(Us, SQ_C1), shelter); return shelter - make_score(0, 16 * minPawnDist); diff --git a/src/position.cpp b/src/position.cpp index 4358f968..db66f416 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -330,16 +330,15 @@ Position& Position::set(const string& fenStr, bool isChess960, StateInfo* si, Th void Position::set_castling_right(Color c, Square rfrom) { Square kfrom = square(c); - CastlingSide cs = kfrom < rfrom ? KING_SIDE : QUEEN_SIDE; - CastlingRight cr = (c | cs); + CastlingRights cr = c & (kfrom < rfrom ? KING_SIDE: QUEEN_SIDE); st->castlingRights |= cr; castlingRightsMask[kfrom] |= cr; castlingRightsMask[rfrom] |= cr; castlingRookSquare[cr] = rfrom; - Square kto = relative_square(c, cs == KING_SIDE ? SQ_G1 : SQ_C1); - Square rto = relative_square(c, cs == KING_SIDE ? SQ_F1 : SQ_D1); + Square kto = relative_square(c, cr & KING_SIDE ? SQ_G1 : SQ_C1); + Square rto = relative_square(c, cr & KING_SIDE ? SQ_F1 : SQ_D1); castlingPath[cr] = (between_bb(rfrom, rto) | between_bb(kfrom, kto) | rto | kto) & ~(square_bb(kfrom) | rfrom); @@ -1300,14 +1299,14 @@ bool Position::pos_is_ok() const { } for (Color c : { WHITE, BLACK }) - for (CastlingSide s : {KING_SIDE, QUEEN_SIDE}) + for (CastlingRights cr : {c & KING_SIDE, c & QUEEN_SIDE}) { - if (!can_castle(c | s)) + if (!can_castle(cr)) continue; - if ( piece_on(castlingRookSquare[c | s]) != make_piece(c, ROOK) - || castlingRightsMask[castlingRookSquare[c | s]] != (c | s) - || (castlingRightsMask[square(c)] & (c | s)) != (c | s)) + if ( piece_on(castlingRookSquare[cr]) != make_piece(c, ROOK) + || castlingRightsMask[castlingRookSquare[cr]] != (cr) + || (castlingRightsMask[square(c)] & (cr)) != (cr)) assert(0 && "pos_is_ok: Castling"); } diff --git a/src/position.h b/src/position.h index 2106414b..a0a9a306 100644 --- a/src/position.h +++ b/src/position.h @@ -100,9 +100,9 @@ public: // Castling int castling_rights(Color c) const; - bool can_castle(CastlingRight cr) const; - bool castling_impeded(CastlingRight cr) const; - Square castling_rook_square(CastlingRight cr) const; + bool can_castle(CastlingRights cr) const; + bool castling_impeded(CastlingRights cr) const; + Square castling_rook_square(CastlingRights cr) const; // Checking Bitboard checkers() const; @@ -268,7 +268,7 @@ inline bool Position::is_on_semiopen_file(Color c, Square s) const { return !(pieces(c, PAWN) & file_bb(s)); } -inline bool Position::can_castle(CastlingRight cr) const { +inline bool Position::can_castle(CastlingRights cr) const { return st->castlingRights & cr; } @@ -276,11 +276,11 @@ inline int Position::castling_rights(Color c) const { return st->castlingRights & (c == WHITE ? WHITE_CASTLING : BLACK_CASTLING); } -inline bool Position::castling_impeded(CastlingRight cr) const { +inline bool Position::castling_impeded(CastlingRights cr) const { return byTypeBB[ALL_PIECES] & castlingPath[cr]; } -inline Square Position::castling_rook_square(CastlingRight cr) const { +inline Square Position::castling_rook_square(CastlingRights cr) const { return castlingRookSquare[cr]; } diff --git a/src/types.h b/src/types.h index b0c333b8..3559d72b 100644 --- a/src/types.h +++ b/src/types.h @@ -131,19 +131,17 @@ enum Color { WHITE, BLACK, COLOR_NB = 2 }; -enum CastlingSide { - KING_SIDE, QUEEN_SIDE, CASTLING_SIDE_NB = 2 -}; - -enum CastlingRight { +enum CastlingRights { NO_CASTLING, WHITE_OO, WHITE_OOO = WHITE_OO << 1, BLACK_OO = WHITE_OO << 2, BLACK_OOO = WHITE_OO << 3, - WHITE_CASTLING = WHITE_OO | WHITE_OOO, - BLACK_CASTLING = BLACK_OO | BLACK_OOO, + KING_SIDE = WHITE_OO | BLACK_OO, + QUEEN_SIDE = WHITE_OOO | BLACK_OOO, + WHITE_CASTLING = WHITE_OO | WHITE_OOO, + BLACK_CASTLING = BLACK_OO | BLACK_OOO, ANY_CASTLING = WHITE_CASTLING | BLACK_CASTLING, CASTLING_RIGHT_NB = 16 @@ -363,8 +361,8 @@ constexpr Piece operator~(Piece pc) { return Piece(pc ^ 8); // Swap color of piece B_KNIGHT -> W_KNIGHT } -constexpr CastlingRight operator|(Color c, CastlingSide s) { - return CastlingRight(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c)); +constexpr CastlingRights operator&(Color c, CastlingRights cr) { + return CastlingRights((c == WHITE ? WHITE_CASTLING : BLACK_CASTLING) & cr); } constexpr Value mate_in(int ply) { -- 2.39.2