Bitboard RookPseudoAttacks[64];
Bitboard QueenPseudoAttacks[64];
+uint8_t BitCount8Bit[256];
+
////
//// Local definitions
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);
}
extern Bitboard RookPseudoAttacks[64];
extern Bitboard QueenPseudoAttacks[64];
+extern uint8_t BitCount8Bit[256];
+
////
//// Inline functions
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);
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);
- }
}
}
}
- 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.
// 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);
}
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;
+}
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;
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() {