From: Marco Costalba Date: Fri, 27 Apr 2012 09:30:35 +0000 (+0100) Subject: Fix wrong condition in PawnEntry::king_safety() X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=2aac860db3d04881103a3b0d09e864ac77b80697 Fix wrong condition in PawnEntry::king_safety() Since revision 374c9e6b63d0e233371 we use also castling information to calculate king safety. So before to reuse the cached king safety score we have to veify not only king position, but also castling rights are the same of the pre-calculated ones. This is a very subtle bug, found only becuase even after previous patch, consecutives runs of 'bench' _still_ showed different numbers. Pawn tables are not cleared between 'bench' runs and in the second run a king safety score, previously evaluated under some castling rights, was reused by another position with different castling rights instead of being recalculated from scratch. Bug spotted and tracked down by Balint Pfliegel Signed-off-by: Marco Costalba --- diff --git a/src/pawns.cpp b/src/pawns.cpp index 6897969c..6923d20b 100644 --- a/src/pawns.cpp +++ b/src/pawns.cpp @@ -260,6 +260,7 @@ template Score PawnEntry::update_safety(const Position& pos, Square ksq) { kingSquares[Us] = ksq; + castleRights[Us] = pos.can_castle(Us); if (relative_rank(Us, ksq) > RANK_4) return kingSafety[Us] = SCORE_ZERO; diff --git a/src/pawns.h b/src/pawns.h index 0376ce61..4fee9316 100644 --- a/src/pawns.h +++ b/src/pawns.h @@ -59,6 +59,7 @@ private: Bitboard passedPawns[2]; Bitboard pawnAttacks[2]; Square kingSquares[2]; + int castleRights[2]; Score value; int halfOpenFiles[2]; Score kingSafety[2]; @@ -106,7 +107,8 @@ inline int PawnEntry::has_open_file_to_right(Color c, File f) const { template inline Score PawnEntry::king_safety(const Position& pos, Square ksq) { - return kingSquares[Us] == ksq ? kingSafety[Us] : update_safety(pos, ksq); + return kingSquares[Us] == ksq && castleRights[Us] == pos.can_castle(Us) + ? kingSafety[Us] : update_safety(pos, ksq); } #endif // !defined(PAWNS_H_INCLUDED) diff --git a/src/position.h b/src/position.h index 8f11a394..e67dfbd4 100644 --- a/src/position.h +++ b/src/position.h @@ -111,8 +111,8 @@ public: int piece_count(Color c, PieceType pt) const; // Castling - bool can_castle(CastleRight f) const; - bool can_castle(Color c) const; + int can_castle(CastleRight f) const; + int can_castle(Color c) const; bool castle_impeded(Color c, CastlingSide s) const; Square castle_rook_square(Color c, CastlingSide s) const; @@ -297,11 +297,11 @@ inline Square Position::king_square(Color c) const { return pieceList[c][KING][0]; } -inline bool Position::can_castle(CastleRight f) const { +inline int Position::can_castle(CastleRight f) const { return st->castleRights & f; } -inline bool Position::can_castle(Color c) const { +inline int Position::can_castle(Color c) const { return st->castleRights & ((WHITE_OO | WHITE_OOO) << c); }