]> git.sesse.net Git - stockfish/commitdiff
Fix wrong condition in PawnEntry::king_safety()
authorMarco Costalba <mcostalba@gmail.com>
Fri, 27 Apr 2012 09:30:35 +0000 (10:30 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Fri, 27 Apr 2012 09:59:18 +0000 (10:59 +0100)
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 <mcostalba@gmail.com>
src/pawns.cpp
src/pawns.h
src/position.h

index 6897969cb08398414470f902b839a9e67faec84b..6923d20b31d8bbfdf87ac26c7bd36e3555b032f4 100644 (file)
@@ -260,6 +260,7 @@ template<Color Us>
 Score PawnEntry::update_safety(const Position& pos, Square ksq) {
 
   kingSquares[Us] = ksq;
 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;
 
   if (relative_rank(Us, ksq) > RANK_4)
       return kingSafety[Us] = SCORE_ZERO;
index 0376ce617f098e7c151dd45cf6816640936f11d8..4fee93165f187a4cd400a91cc8c774348c8c6c18 100644 (file)
@@ -59,6 +59,7 @@ private:
   Bitboard passedPawns[2];
   Bitboard pawnAttacks[2];
   Square kingSquares[2];
   Bitboard passedPawns[2];
   Bitboard pawnAttacks[2];
   Square kingSquares[2];
+  int castleRights[2];
   Score value;
   int halfOpenFiles[2];
   Score kingSafety[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<Color Us>
 inline Score PawnEntry::king_safety(const Position& pos, Square ksq) {
 
 template<Color Us>
 inline Score PawnEntry::king_safety(const Position& pos, Square ksq) {
-  return kingSquares[Us] == ksq ? kingSafety[Us] : update_safety<Us>(pos, ksq);
+  return kingSquares[Us] == ksq && castleRights[Us] == pos.can_castle(Us)
+       ? kingSafety[Us] : update_safety<Us>(pos, ksq);
 }
 
 #endif // !defined(PAWNS_H_INCLUDED)
 }
 
 #endif // !defined(PAWNS_H_INCLUDED)
index 8f11a394102098ef7362b6a5428bb98c2c47ced0..e67dfbd49e165fb295af33d5691062cbdb0a5038 100644 (file)
@@ -111,8 +111,8 @@ public:
   int piece_count(Color c, PieceType pt) const;
 
   // Castling
   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;
 
   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];
 }
 
   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;
 }
 
   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);
 }
 
   return st->castleRights & ((WHITE_OO | WHITE_OOO) << c);
 }