From 1ee2838214fcbadc352fb17c8c6fa1142ae5dcb0 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sat, 27 Aug 2016 10:05:42 +0200 Subject: [PATCH] Retire CheckInfo Move its content directly under StateInfo. Verified for no speed regression. No functional change. --- src/movegen.cpp | 4 ++-- src/position.cpp | 42 +++++++++++++++++++++--------------------- src/position.h | 25 ++++++++----------------- src/search.cpp | 4 ++-- 4 files changed, 33 insertions(+), 42 deletions(-) diff --git a/src/movegen.cpp b/src/movegen.cpp index 322d2a65..efa47bd5 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -238,7 +238,7 @@ namespace { if (Checks) { if ( (Pt == BISHOP || Pt == ROOK || Pt == QUEEN) - && !(PseudoAttacks[Pt][from] & target & pos.check_info().checkSquares[Pt])) + && !(PseudoAttacks[Pt][from] & target & pos.check_squares(Pt))) continue; if (pos.discovered_check_candidates() & from) @@ -248,7 +248,7 @@ namespace { Bitboard b = pos.attacks_from(from) & target; if (Checks) - b &= pos.check_info().checkSquares[Pt]; + b &= pos.check_squares(Pt); while (b) *moveList++ = make_move(from, pop_lsb(&b)); diff --git a/src/position.cpp b/src/position.cpp index 5b2efabe..bc8e92a1 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -294,19 +294,19 @@ void Position::set_castling_right(Color c, Square rfrom) { /// Position::set_check_info() sets king attacks to detect if a move gives check -void Position::set_check_info(CheckInfo* ci) const { +void Position::set_check_info(StateInfo* si) const { - ci->blockersForKing[WHITE] = slider_blockers(pieces(BLACK), square(WHITE)); - ci->blockersForKing[BLACK] = slider_blockers(pieces(WHITE), square(BLACK)); + si->blockersForKing[WHITE] = slider_blockers(pieces(BLACK), square(WHITE)); + si->blockersForKing[BLACK] = slider_blockers(pieces(WHITE), square(BLACK)); - Square ksq = ci->ksq = square(~sideToMove); + Square ksq = square(~sideToMove); - ci->checkSquares[PAWN] = attacks_from(ksq, ~sideToMove); - ci->checkSquares[KNIGHT] = attacks_from(ksq); - ci->checkSquares[BISHOP] = attacks_from(ksq); - ci->checkSquares[ROOK] = attacks_from(ksq); - ci->checkSquares[QUEEN] = ci->checkSquares[BISHOP] | ci->checkSquares[ROOK]; - ci->checkSquares[KING] = 0; + si->checkSquares[PAWN] = attacks_from(ksq, ~sideToMove); + si->checkSquares[KNIGHT] = attacks_from(ksq); + si->checkSquares[BISHOP] = attacks_from(ksq); + si->checkSquares[ROOK] = attacks_from(ksq); + si->checkSquares[QUEEN] = si->checkSquares[BISHOP] | si->checkSquares[ROOK]; + si->checkSquares[KING] = 0; } @@ -322,7 +322,7 @@ void Position::set_state(StateInfo* si) const { si->psq = SCORE_ZERO; si->checkersBB = attackers_to(square(sideToMove)) & pieces(~sideToMove); - set_check_info(&si->ci); + set_check_info(si); for (Bitboard b = pieces(); b; ) { @@ -587,12 +587,12 @@ bool Position::gives_check(Move m) const { Square to = to_sq(m); // Is there a direct check? - if (st->ci.checkSquares[type_of(piece_on(from))] & to) + if (st->checkSquares[type_of(piece_on(from))] & to) return true; // Is there a discovered check? if ( (discovered_check_candidates() & from) - && !aligned(from, to, st->ci.ksq)) + && !aligned(from, to, square(~sideToMove))) return true; switch (type_of(m)) @@ -601,7 +601,7 @@ bool Position::gives_check(Move m) const { return false; case PROMOTION: - return attacks_bb(Piece(promotion_type(m)), to, pieces() ^ from) & st->ci.ksq; + return attacks_bb(Piece(promotion_type(m)), to, pieces() ^ from) & square(~sideToMove); // En passant capture with check? We have already handled the case // of direct checks and ordinary discovered check, so the only case we @@ -612,8 +612,8 @@ bool Position::gives_check(Move m) const { Square capsq = make_square(file_of(to), rank_of(from)); Bitboard b = (pieces() ^ from ^ capsq) | to; - return (attacks_bb< ROOK>(st->ci.ksq, b) & pieces(sideToMove, QUEEN, ROOK)) - | (attacks_bb(st->ci.ksq, b) & pieces(sideToMove, QUEEN, BISHOP)); + return (attacks_bb< ROOK>(square(~sideToMove), b) & pieces(sideToMove, QUEEN, ROOK)) + | (attacks_bb(square(~sideToMove), b) & pieces(sideToMove, QUEEN, BISHOP)); } case CASTLING: { @@ -622,8 +622,8 @@ bool Position::gives_check(Move m) const { Square kto = relative_square(sideToMove, rfrom > kfrom ? SQ_G1 : SQ_C1); Square rto = relative_square(sideToMove, rfrom > kfrom ? SQ_F1 : SQ_D1); - return (PseudoAttacks[ROOK][rto] & st->ci.ksq) - && (attacks_bb(rto, (pieces() ^ kfrom ^ rfrom) | rto | kto) & st->ci.ksq); + return (PseudoAttacks[ROOK][rto] & square(~sideToMove)) + && (attacks_bb(rto, (pieces() ^ kfrom ^ rfrom) | rto | kto) & square(~sideToMove)); } default: assert(false); @@ -799,8 +799,8 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) { sideToMove = ~sideToMove; - // Update CheckInfo - set_check_info(&st->ci); + // Update king attacks used for fast check detection + set_check_info(st); assert(pos_is_ok()); } @@ -915,7 +915,7 @@ void Position::do_null_move(StateInfo& newSt) { sideToMove = ~sideToMove; - set_check_info(&st->ci); + set_check_info(st); assert(pos_is_ok()); } diff --git a/src/position.h b/src/position.h index d6efe711..7fce1dbb 100644 --- a/src/position.h +++ b/src/position.h @@ -42,16 +42,6 @@ namespace PSQT { } -/// CheckInfo struct keeps info used to detect if a move gives check - -struct CheckInfo { - - Bitboard blockersForKing[COLOR_NB]; - Bitboard checkSquares[PIECE_TYPE_NB]; - Square ksq; -}; - - /// StateInfo struct stores information needed to restore a Position object to /// its previous state when we retract a move. Whenever a move is made on the /// board (by calling Position::do_move), a StateInfo object must be passed. @@ -73,7 +63,8 @@ struct StateInfo { Bitboard checkersBB; PieceType capturedType; StateInfo* previous; - CheckInfo ci; + Bitboard blockersForKing[COLOR_NB]; + Bitboard checkSquares[PIECE_TYPE_NB]; }; // In a std::deque references to elements are unaffected upon resizing @@ -122,7 +113,7 @@ public: Bitboard checkers() const; Bitboard discovered_check_candidates() const; Bitboard pinned_pieces(Color c) const; - const CheckInfo& check_info() const; + Bitboard check_squares(PieceType pt) const; // Attacks to/from a given square Bitboard attackers_to(Square s) const; @@ -184,7 +175,7 @@ private: // Initialization helpers (used while setting up a position) void set_castling_right(Color c, Square rfrom); void set_state(StateInfo* si) const; - void set_check_info(CheckInfo* ci) const; + void set_check_info(StateInfo* si) const; // Other helpers void put_piece(Color c, PieceType pt, Square s); @@ -311,15 +302,15 @@ inline Bitboard Position::checkers() const { } inline Bitboard Position::discovered_check_candidates() const { - return st->ci.blockersForKing[~sideToMove] & pieces(sideToMove); + return st->blockersForKing[~sideToMove] & pieces(sideToMove); } inline Bitboard Position::pinned_pieces(Color c) const { - return st->ci.blockersForKing[c] & pieces(c); + return st->blockersForKing[c] & pieces(c); } -inline const CheckInfo& Position::check_info() const { - return st->ci; +inline Bitboard Position::check_squares(PieceType pt) const { + return st->checkSquares[pt]; } inline bool Position::pawn_passed(Color c, Square s) const { diff --git a/src/search.cpp b/src/search.cpp index ce8c4b7a..ff31e036 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -883,7 +883,7 @@ moves_loop: // When in check search starts from here moved_piece = pos.moved_piece(move); givesCheck = type_of(move) == NORMAL && !pos.discovered_check_candidates() - ? pos.check_info().checkSquares[type_of(pos.piece_on(from_sq(move)))] & to_sq(move) + ? pos.check_squares(type_of(pos.piece_on(from_sq(move)))) & to_sq(move) : pos.gives_check(move); moveCountPruning = depth < 16 * ONE_PLY @@ -1285,7 +1285,7 @@ moves_loop: // When in check search starts from here assert(is_ok(move)); givesCheck = type_of(move) == NORMAL && !pos.discovered_check_candidates() - ? pos.check_info().checkSquares[type_of(pos.piece_on(from_sq(move)))] & to_sq(move) + ? pos.check_squares(type_of(pos.piece_on(from_sq(move)))) & to_sq(move) : pos.gives_check(move); // Futility pruning -- 2.39.2