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>
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;
Bitboard passedPawns[2];
Bitboard pawnAttacks[2];
Square kingSquares[2];
+ int castleRights[2];
Score value;
int halfOpenFiles[2];
Score kingSafety[2];
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)
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;
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);
}