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 <mcostalba@gmail.com>sf_2.3.1_base
parent
20224a5bbf
commit
080a4995a3
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -93,6 +93,8 @@ extern Bitboard BishopPseudoAttacks[64];
|
|||
extern Bitboard RookPseudoAttacks[64];
|
||||
extern Bitboard QueenPseudoAttacks[64];
|
||||
|
||||
extern uint8_t BitCount8Bit[256];
|
||||
|
||||
|
||||
////
|
||||
//// Inline functions
|
||||
|
|
|
@ -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<bool HasPopCnt>
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
13
src/pawns.h
13
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() {
|
||||
|
|
Loading…
Reference in New Issue