-inline void PawnInfo::clear() {
- mgValue = egValue = 0;
- passedPawns = EmptyBoardBB;
- ksStormValue[WHITE] = ksStormValue[BLACK] = 0;
- qsStormValue[WHITE] = qsStormValue[BLACK] = 0;
- halfOpenFiles[WHITE] = halfOpenFiles[BLACK] = 0xFF;
+/// PawnInfo::updateShelter() calculates and caches king shelter. It is called
+/// only when king square changes, about 20% of total king_shelter() calls.
+template<Color Us>
+Score PawnInfo::updateShelter(const Position& pos, Square ksq) {
+
+ const int Shift = (Us == WHITE ? 8 : -8);
+
+ Bitboard pawns;
+ int r, shelter = 0;
+
+ if (relative_rank(Us, ksq) <= RANK_4)
+ {
+ pawns = pos.pieces(PAWN, Us) & this_and_neighboring_files_bb(ksq);
+ r = square_rank(ksq) * 8;
+ for (int i = 1; i < 4; i++)
+ {
+ r += Shift;
+ shelter += BitCount8Bit[(pawns >> r) & 0xFF] * (128 >> i);
+ }
+ }
+ kingSquares[Us] = ksq;
+ kingShelters[Us] = make_score(shelter, 0);
+ return kingShelters[Us];