mirror of
https://github.com/sockspls/badfish
synced 2025-07-11 11:39:15 +00:00
Introduce namespace Pawns
And retire old struct PawnTable along the same lines of previous patch. No functional change.
This commit is contained in:
parent
231f62baf7
commit
158014b39d
5 changed files with 136 additions and 178 deletions
|
@ -37,7 +37,7 @@ namespace {
|
|||
|
||||
// Pointers to material and pawn hash table entries
|
||||
Material::Entry* mi;
|
||||
PawnEntry* pi;
|
||||
Pawns::Entry* pi;
|
||||
|
||||
// attackedBy[color][piece type] is a bitboard representing all squares
|
||||
// attacked by a given color and piece type, attackedBy[color][0] contains
|
||||
|
@ -405,7 +405,7 @@ Value do_evaluate(const Position& pos, Value& margin) {
|
|||
}
|
||||
|
||||
// Probe the pawn hash table
|
||||
ei.pi = pos.this_thread()->pawnTable.probe(pos);
|
||||
ei.pi = Pawns::probe(pos, th->pawnsTable);
|
||||
score += ei.pi->pawns_value();
|
||||
|
||||
// Initialize attack and king safety bitboards
|
||||
|
|
|
@ -80,49 +80,10 @@ namespace {
|
|||
|
||||
#undef S
|
||||
#undef V
|
||||
}
|
||||
|
||||
|
||||
/// PawnTable::probe() takes a position object as input, computes a PawnEntry
|
||||
/// object, and returns a pointer to it. The result is also stored in a hash
|
||||
/// table, so we don't have to recompute everything when the same pawn structure
|
||||
/// occurs again.
|
||||
|
||||
PawnEntry* PawnTable::probe(const Position& pos) {
|
||||
|
||||
Key key = pos.pawn_key();
|
||||
PawnEntry* e = entries[key];
|
||||
|
||||
// If e->key matches the position's pawn hash key, it means that we
|
||||
// have analysed this pawn structure before, and we can simply return
|
||||
// the information we found the last time instead of recomputing it.
|
||||
if (e->key == key)
|
||||
return e;
|
||||
|
||||
e->key = key;
|
||||
e->passedPawns[WHITE] = e->passedPawns[BLACK] = 0;
|
||||
e->kingSquares[WHITE] = e->kingSquares[BLACK] = SQ_NONE;
|
||||
e->halfOpenFiles[WHITE] = e->halfOpenFiles[BLACK] = 0xFF;
|
||||
|
||||
Bitboard wPawns = pos.pieces(WHITE, PAWN);
|
||||
Bitboard bPawns = pos.pieces(BLACK, PAWN);
|
||||
e->pawnAttacks[WHITE] = ((wPawns & ~FileHBB) << 9) | ((wPawns & ~FileABB) << 7);
|
||||
e->pawnAttacks[BLACK] = ((bPawns & ~FileHBB) >> 7) | ((bPawns & ~FileABB) >> 9);
|
||||
|
||||
e->value = evaluate_pawns<WHITE>(pos, wPawns, bPawns, e)
|
||||
- evaluate_pawns<BLACK>(pos, bPawns, wPawns, e);
|
||||
|
||||
e->value = apply_weight(e->value, PawnStructureWeight);
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
/// PawnTable::evaluate_pawns() evaluates each pawn of the given color
|
||||
|
||||
template<Color Us>
|
||||
Score PawnTable::evaluate_pawns(const Position& pos, Bitboard ourPawns,
|
||||
Bitboard theirPawns, PawnEntry* e) {
|
||||
Score evaluate_pawns(const Position& pos, Bitboard ourPawns,
|
||||
Bitboard theirPawns, Pawns::Entry* e) {
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
|
@ -217,13 +178,49 @@ Score PawnTable::evaluate_pawns(const Position& pos, Bitboard ourPawns,
|
|||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
namespace Pawns {
|
||||
|
||||
/// probe() takes a position object as input, computes a Entry object, and returns
|
||||
/// a pointer to it. The result is also stored in a hash table, so we don't have
|
||||
/// to recompute everything when the same pawn structure occurs again.
|
||||
|
||||
Entry* probe(const Position& pos, Table& entries) {
|
||||
|
||||
Key key = pos.pawn_key();
|
||||
Entry* e = entries[key];
|
||||
|
||||
// If e->key matches the position's pawn hash key, it means that we
|
||||
// have analysed this pawn structure before, and we can simply return
|
||||
// the information we found the last time instead of recomputing it.
|
||||
if (e->key == key)
|
||||
return e;
|
||||
|
||||
e->key = key;
|
||||
e->passedPawns[WHITE] = e->passedPawns[BLACK] = 0;
|
||||
e->kingSquares[WHITE] = e->kingSquares[BLACK] = SQ_NONE;
|
||||
e->halfOpenFiles[WHITE] = e->halfOpenFiles[BLACK] = 0xFF;
|
||||
|
||||
Bitboard wPawns = pos.pieces(WHITE, PAWN);
|
||||
Bitboard bPawns = pos.pieces(BLACK, PAWN);
|
||||
e->pawnAttacks[WHITE] = ((wPawns & ~FileHBB) << 9) | ((wPawns & ~FileABB) << 7);
|
||||
e->pawnAttacks[BLACK] = ((bPawns & ~FileHBB) >> 7) | ((bPawns & ~FileABB) >> 9);
|
||||
|
||||
e->value = evaluate_pawns<WHITE>(pos, wPawns, bPawns, e)
|
||||
- evaluate_pawns<BLACK>(pos, bPawns, wPawns, e);
|
||||
|
||||
e->value = apply_weight(e->value, PawnStructureWeight);
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
/// PawnEntry::shelter_storm() calculates shelter and storm penalties for the file
|
||||
/// Entry::shelter_storm() calculates shelter and storm penalties for the file
|
||||
/// the king is on, as well as the two adjacent files.
|
||||
|
||||
template<Color Us>
|
||||
Value PawnEntry::shelter_storm(const Position& pos, Square ksq) {
|
||||
Value Entry::shelter_storm(const Position& pos, Square ksq) {
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
|
@ -253,11 +250,11 @@ Value PawnEntry::shelter_storm(const Position& pos, Square ksq) {
|
|||
}
|
||||
|
||||
|
||||
/// PawnEntry::update_safety() calculates and caches a bonus for king safety. It is
|
||||
/// Entry::update_safety() calculates and caches a bonus for king safety. It is
|
||||
/// called only when king square changes, about 20% of total king_safety() calls.
|
||||
|
||||
template<Color Us>
|
||||
Score PawnEntry::update_safety(const Position& pos, Square ksq) {
|
||||
Score Entry::update_safety(const Position& pos, Square ksq) {
|
||||
|
||||
kingSquares[Us] = ksq;
|
||||
castleRights[Us] = pos.can_castle(Us);
|
||||
|
@ -283,5 +280,7 @@ Score PawnEntry::update_safety(const Position& pos, Square ksq) {
|
|||
}
|
||||
|
||||
// Explicit template instantiation
|
||||
template Score PawnEntry::update_safety<WHITE>(const Position& pos, Square ksq);
|
||||
template Score PawnEntry::update_safety<BLACK>(const Position& pos, Square ksq);
|
||||
template Score Entry::update_safety<WHITE>(const Position& pos, Square ksq);
|
||||
template Score Entry::update_safety<BLACK>(const Position& pos, Square ksq);
|
||||
|
||||
} // namespace Pawns
|
||||
|
|
81
src/pawns.h
81
src/pawns.h
|
@ -24,31 +24,30 @@
|
|||
#include "position.h"
|
||||
#include "types.h"
|
||||
|
||||
const int PawnTableSize = 16384;
|
||||
namespace Pawns {
|
||||
|
||||
/// PawnEntry is a class which contains various information about a pawn
|
||||
/// structure. Currently, it only includes a middle game and an end game
|
||||
/// pawn structure evaluation, and a bitboard of passed pawns. We may want
|
||||
/// to add further information in the future. A lookup to the pawn hash
|
||||
/// table (performed by calling the probe method in a PawnTable object)
|
||||
/// returns a pointer to a PawnEntry object.
|
||||
/// Pawns::Entry contains various information about a pawn structure. Currently,
|
||||
/// it only includes a middle game and end game pawn structure evaluation, and a
|
||||
/// bitboard of passed pawns. We may want to add further information in the future.
|
||||
/// A lookup to the pawn hash table (performed by calling the probe function)
|
||||
/// returns a pointer to an Entry object.
|
||||
|
||||
class PawnEntry {
|
||||
struct Entry {
|
||||
|
||||
friend struct PawnTable;
|
||||
|
||||
public:
|
||||
Score pawns_value() const;
|
||||
Bitboard pawn_attacks(Color c) const;
|
||||
Bitboard passed_pawns(Color c) const;
|
||||
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;
|
||||
Score pawns_value() const { return value; }
|
||||
Bitboard pawn_attacks(Color c) const { return pawnAttacks[c]; }
|
||||
Bitboard passed_pawns(Color c) const { return passedPawns[c]; }
|
||||
int file_is_half_open(Color c, File f) const { return halfOpenFiles[c] & (1 << int(f)); }
|
||||
int has_open_file_to_left(Color c, File f) const { return halfOpenFiles[c] & ((1 << int(f)) - 1); }
|
||||
int has_open_file_to_right(Color c, File f) const { return halfOpenFiles[c] & ~((1 << int(f+1)) - 1); }
|
||||
|
||||
template<Color Us>
|
||||
Score king_safety(const Position& pos, Square ksq);
|
||||
Score king_safety(const Position& pos, Square ksq) {
|
||||
|
||||
return kingSquares[Us] == ksq && castleRights[Us] == pos.can_castle(Us)
|
||||
? kingSafety[Us] : update_safety<Us>(pos, ksq);
|
||||
}
|
||||
|
||||
private:
|
||||
template<Color Us>
|
||||
Score update_safety(const Position& pos, Square ksq);
|
||||
|
||||
|
@ -66,50 +65,10 @@ private:
|
|||
Score kingSafety[COLOR_NB];
|
||||
};
|
||||
|
||||
typedef HashTable<Entry, 16384> Table;
|
||||
|
||||
/// The PawnTable class represents a pawn hash table. The most important
|
||||
/// method is probe, which returns a pointer to a PawnEntry object.
|
||||
Entry* probe(const Position& pos, Table& entries);
|
||||
|
||||
struct PawnTable {
|
||||
|
||||
PawnEntry* probe(const Position& pos);
|
||||
|
||||
template<Color Us>
|
||||
static Score evaluate_pawns(const Position& pos, Bitboard ourPawns,
|
||||
Bitboard theirPawns, PawnEntry* e);
|
||||
|
||||
HashTable<PawnEntry, PawnTableSize> entries;
|
||||
};
|
||||
|
||||
|
||||
inline Score PawnEntry::pawns_value() const {
|
||||
return value;
|
||||
}
|
||||
|
||||
inline Bitboard PawnEntry::pawn_attacks(Color c) const {
|
||||
return pawnAttacks[c];
|
||||
}
|
||||
|
||||
inline Bitboard PawnEntry::passed_pawns(Color c) const {
|
||||
return passedPawns[c];
|
||||
}
|
||||
|
||||
inline int PawnEntry::file_is_half_open(Color c, File f) const {
|
||||
return halfOpenFiles[c] & (1 << int(f));
|
||||
}
|
||||
|
||||
inline int PawnEntry::has_open_file_to_left(Color c, File f) const {
|
||||
return halfOpenFiles[c] & ((1 << int(f)) - 1);
|
||||
}
|
||||
|
||||
inline int PawnEntry::has_open_file_to_right(Color c, File f) const {
|
||||
return halfOpenFiles[c] & ~((1 << int(f+1)) - 1);
|
||||
}
|
||||
|
||||
template<Color Us>
|
||||
inline Score PawnEntry::king_safety(const Position& pos, Square ksq) {
|
||||
return kingSquares[Us] == ksq && castleRights[Us] == pos.can_castle(Us)
|
||||
? kingSafety[Us] : update_safety<Us>(pos, ksq);
|
||||
}
|
||||
|
||||
#endif // !defined(PAWNS_H_INCLUDED)
|
||||
|
|
|
@ -918,7 +918,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
|
|||
}
|
||||
|
||||
// Prefetch pawn and material hash tables
|
||||
prefetch((char*)thisThread->pawnTable.entries[st->pawnKey]);
|
||||
prefetch((char*)thisThread->pawnsTable[st->pawnKey]);
|
||||
prefetch((char*)thisThread->materialTable[st->materialKey]);
|
||||
|
||||
// Update incremental scores
|
||||
|
|
|
@ -112,7 +112,7 @@ public:
|
|||
Eval::Table evalTable;
|
||||
Material::Table materialTable;
|
||||
Endgames endgames;
|
||||
PawnTable pawnTable;
|
||||
Pawns::Table pawnsTable;
|
||||
size_t idx;
|
||||
int maxPly;
|
||||
Mutex mutex;
|
||||
|
|
Loading…
Add table
Reference in a new issue