+
+
+/// 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_adjacent_files_bb(file_of(ksq));
+ r = ksq & (7 << 3);
+ for (int i = 0; i < 3; i++)
+ {
+ r += Shift;
+ shelter += BitCount8Bit[(pawns >> r) & 0xFF] << (6 - i);
+ }
+ }
+ kingSquares[Us] = ksq;
+ kingShelters[Us] = make_score(shelter, 0);
+ return kingShelters[Us];
+}
+
+// Explicit template instantiation
+template Score PawnInfo::updateShelter<WHITE>(const Position& pos, Square ksq);
+template Score PawnInfo::updateShelter<BLACK>(const Position& pos, Square ksq);