From 080a4995a3977a0fe7071e62151dc4d716a13302 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Fri, 24 Jul 2009 12:16:18 +0200 Subject: [PATCH] Simplify king shelter cache handling This is more similar to how get_material_info() and get_pawn_info() work and also removes some clutter from evaluate_king(). No functional change. Signed-off-by: Marco Costalba --- src/bitboard.cpp | 5 +++++ src/bitboard.h | 2 ++ src/evaluate.cpp | 27 +-------------------------- src/pawns.cpp | 18 ++++++++++++++++++ src/pawns.h | 13 ++++--------- 5 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/bitboard.cpp b/src/bitboard.cpp index 18b7bb28..9bd16d2f 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -233,6 +233,8 @@ Bitboard BishopPseudoAttacks[64]; Bitboard RookPseudoAttacks[64]; Bitboard QueenPseudoAttacks[64]; +uint8_t BitCount8Bit[256]; + //// //// Local definitions @@ -382,6 +384,9 @@ namespace { in_front_bb(c, s) & this_and_neighboring_files_bb(s); OutpostMask[c][s] = in_front_bb(c, s) & neighboring_files_bb(s); } + + for (Bitboard b = 0ULL; b < 256ULL; b++) + BitCount8Bit[b] = (uint8_t)count_1s(b); } diff --git a/src/bitboard.h b/src/bitboard.h index f06fbe8a..d2b623f6 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -93,6 +93,8 @@ extern Bitboard BishopPseudoAttacks[64]; extern Bitboard RookPseudoAttacks[64]; extern Bitboard QueenPseudoAttacks[64]; +extern uint8_t BitCount8Bit[256]; + //// //// Inline functions diff --git a/src/evaluate.cpp b/src/evaluate.cpp index 13bc9751..7f7df3c5 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -266,9 +266,6 @@ namespace { const int PawnTableSize = 16384; const int MaterialTableSize = 1024; - // Array which gives the number of nonzero bits in an 8-bit integer - uint8_t BitCount8Bit[256]; - // Function prototypes template Value do_evaluate(const Position& pos, EvalInfo& ei, int threadID); @@ -498,12 +495,6 @@ void init_eval(int threads) { if (!MaterialTable[i]) MaterialTable[i] = new MaterialInfoTable(MaterialTableSize); } - - for (Bitboard b = 0ULL; b < 256ULL; b++) - { - assert(count_1s(b) == int(uint8_t(count_1s(b)))); - BitCount8Bit[b] = (uint8_t)count_1s(b); - } } @@ -714,10 +705,6 @@ namespace { } } - inline Bitboard shiftRowsDown(const Bitboard& b, int num) { - - return b >> (num << 3); - } // evaluate_king<>() assigns bonuses and penalties to a king of a given color. @@ -730,19 +717,7 @@ namespace { // King shelter if (relative_rank(us, s) <= RANK_4) { - // Shelter cache lookup - shelter = ei.pi->kingShelter(us, s); - if (shelter == -1) - { - shelter = 0; - Bitboard pawns = p.pawns(us) & this_and_neighboring_files_bb(s); - Rank r = square_rank(s); - for (int i = 1; i < 4; i++) - shelter += BitCount8Bit[shiftRowsDown(pawns, r+i*sign) & 0xFF] * (128 >> i); - - // Cache shelter value in pawn info - ei.pi->setKingShelter(us, s, shelter); - } + shelter = ei.pi->get_king_shelter(p, us, s); ei.mgValue += sign * Value(shelter); } diff --git a/src/pawns.cpp b/src/pawns.cpp index d0523ab6..17ca9d1e 100644 --- a/src/pawns.cpp +++ b/src/pawns.cpp @@ -385,3 +385,21 @@ PawnInfo *PawnInfoTable::get_pawn_info(const Position &pos) { pi->egValue = int16_t(egValue[WHITE] - egValue[BLACK]); return pi; } + + +/// PawnInfo::updateShelter calculates and caches king shelter. It is called +/// only when king square changes, about 20% of total get_king_shelter() calls. +int PawnInfo::updateShelter(const Position& pos, Color c, Square ksq) { + + int shelter = 0; + Bitboard pawns = pos.pawns(c) & this_and_neighboring_files_bb(ksq); + unsigned r = ksq & (7 << 3); + for (int i = 1, k = (c ? -8 : 8); i < 4; i++) + { + r += k; + shelter += BitCount8Bit[(pawns >> r) & 0xFF] * (128 >> i); + } + kingSquares[c] = ksq; + kingShelters[c] = shelter; + return shelter; +} diff --git a/src/pawns.h b/src/pawns.h index 4b4a8a55..ba94a723 100644 --- a/src/pawns.h +++ b/src/pawns.h @@ -55,11 +55,11 @@ public: int file_is_half_open(Color c, File f) const; int has_open_file_to_left(Color c, File f) const; int has_open_file_to_right(Color c, File f) const; - int kingShelter(Color c, Square ksq) const; - void setKingShelter(Color c, Square ksq, int value); + int get_king_shelter(const Position& pos, Color c, Square ksq); private: inline void clear(); + int updateShelter(const Position& pos, Color c, Square ksq); Key key; Bitboard passedPawns; @@ -124,13 +124,8 @@ inline int PawnInfo::has_open_file_to_right(Color c, File f) const { return halfOpenFiles[c] & ~((1 << int(f+1)) - 1); } -inline int PawnInfo::kingShelter(Color c, Square ksq) const { - return (kingSquares[c] == ksq ? kingShelters[c] : -1); -} - -inline void PawnInfo::setKingShelter(Color c, Square ksq, int value) { - kingSquares[c] = ksq; - kingShelters[c] = (int16_t)value; +inline int PawnInfo::get_king_shelter(const Position& pos, Color c, Square ksq) { + return (kingSquares[c] == ksq ? kingShelters[c] : updateShelter(pos, c, ksq)); } inline void PawnInfo::clear() { -- 2.39.2