Skip to content

Commit

Permalink
Consolidate CastlingSide and CastlingRights
Browse files Browse the repository at this point in the history
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 #2265

No functional change
  • Loading branch information
protonspring authored and snicolet committed Aug 23, 2019
1 parent a016626 commit 3984b8f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/movegen.cpp
Expand Up @@ -219,8 +219,8 @@ namespace {
template<Color Us, GenType Type>
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<Us, Type>(pos, moveList, target);
Expand All @@ -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<CASTLING>(ksq, pos.castling_rook_square(OO));
Expand Down
4 changes: 2 additions & 2 deletions src/pawns.cpp
Expand Up @@ -238,10 +238,10 @@ Score Entry::do_king_safety(const Position& pos) {
evaluate_shelter<Us>(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<Us>(pos, relative_square(Us, SQ_G1), shelter);

if (pos.can_castle(Us | QUEEN_SIDE))
if (pos.can_castle(Us & QUEEN_SIDE))
evaluate_shelter<Us>(pos, relative_square(Us, SQ_C1), shelter);

return shelter - make_score(0, 16 * minPawnDist);
Expand Down
17 changes: 8 additions & 9 deletions src/position.cpp
Expand Up @@ -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<KING>(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);
Expand Down Expand Up @@ -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<KING>(c)] & (c | s)) != (c | s))
if ( piece_on(castlingRookSquare[cr]) != make_piece(c, ROOK)
|| castlingRightsMask[castlingRookSquare[cr]] != (cr)
|| (castlingRightsMask[square<KING>(c)] & (cr)) != (cr))
assert(0 && "pos_is_ok: Castling");
}

Expand Down
12 changes: 6 additions & 6 deletions src/position.h
Expand Up @@ -100,9 +100,9 @@ class Position {

// 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;
Expand Down Expand Up @@ -268,19 +268,19 @@ 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;
}

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];
}

Expand Down
16 changes: 7 additions & 9 deletions src/types.h
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 3984b8f

Please sign in to comment.