1
0
Fork 0
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:
Marco Costalba 2012-12-22 11:21:06 +01:00
parent 231f62baf7
commit 158014b39d
5 changed files with 136 additions and 178 deletions

View file

@ -37,7 +37,7 @@ namespace {
// Pointers to material and pawn hash table entries // Pointers to material and pawn hash table entries
Material::Entry* mi; Material::Entry* mi;
PawnEntry* pi; Pawns::Entry* pi;
// attackedBy[color][piece type] is a bitboard representing all squares // attackedBy[color][piece type] is a bitboard representing all squares
// attacked by a given color and piece type, attackedBy[color][0] contains // 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 // 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(); score += ei.pi->pawns_value();
// Initialize attack and king safety bitboards // Initialize attack and king safety bitboards

View file

@ -80,18 +80,116 @@ namespace {
#undef S #undef S
#undef V #undef V
template<Color Us>
Score evaluate_pawns(const Position& pos, Bitboard ourPawns,
Bitboard theirPawns, Pawns::Entry* e) {
const Color Them = (Us == WHITE ? BLACK : WHITE);
Bitboard b;
Square s;
File f;
Rank r;
bool passed, isolated, doubled, opposed, chain, backward, candidate;
Score value = SCORE_ZERO;
const Square* pl = pos.piece_list(Us, PAWN);
// Loop through all pawns of the current color and score each pawn
while ((s = *pl++) != SQ_NONE)
{
assert(pos.piece_on(s) == make_piece(Us, PAWN));
f = file_of(s);
r = rank_of(s);
// This file cannot be half open
e->halfOpenFiles[Us] &= ~(1 << f);
// Our rank plus previous one. Used for chain detection
b = rank_bb(r) | rank_bb(Us == WHITE ? r - Rank(1) : r + Rank(1));
// Flag the pawn as passed, isolated, doubled or member of a pawn
// chain (but not the backward one).
chain = ourPawns & adjacent_files_bb(f) & b;
isolated = !(ourPawns & adjacent_files_bb(f));
doubled = ourPawns & forward_bb(Us, s);
opposed = theirPawns & forward_bb(Us, s);
passed = !(theirPawns & passed_pawn_mask(Us, s));
// Test for backward pawn
backward = false;
// If the pawn is passed, isolated, or member of a pawn chain it cannot
// be backward. If there are friendly pawns behind on adjacent files
// or if can capture an enemy pawn it cannot be backward either.
if ( !(passed | isolated | chain)
&& !(ourPawns & attack_span_mask(Them, s))
&& !(pos.attacks_from<PAWN>(s, Us) & theirPawns))
{
// We now know that there are no friendly pawns beside or behind this
// pawn on adjacent files. We now check whether the pawn is
// backward by looking in the forward direction on the adjacent
// files, and seeing whether we meet a friendly or an enemy pawn first.
b = pos.attacks_from<PAWN>(s, Us);
// Note that we are sure to find something because pawn is not passed
// nor isolated, so loop is potentially infinite, but it isn't.
while (!(b & (ourPawns | theirPawns)))
Us == WHITE ? b <<= 8 : b >>= 8;
// The friendly pawn needs to be at least two ranks closer than the
// enemy pawn in order to help the potentially backward pawn advance.
backward = (b | (Us == WHITE ? b << 8 : b >> 8)) & theirPawns;
}
assert(opposed | passed | (attack_span_mask(Us, s) & theirPawns));
// A not passed pawn is a candidate to become passed if it is free to
// advance and if the number of friendly pawns beside or behind this
// pawn on adjacent files is higher or equal than the number of
// enemy pawns in the forward direction on the adjacent files.
candidate = !(opposed | passed | backward | isolated)
&& (b = attack_span_mask(Them, s + pawn_push(Us)) & ourPawns) != 0
&& popcount<Max15>(b) >= popcount<Max15>(attack_span_mask(Us, s) & theirPawns);
// Passed pawns will be properly scored in evaluation because we need
// full attack info to evaluate passed pawns. Only the frontmost passed
// pawn on each file is considered a true passed pawn.
if (passed && !doubled)
e->passedPawns[Us] |= s;
// Score this pawn
if (isolated)
value -= IsolatedPawnPenalty[opposed][f];
if (doubled)
value -= DoubledPawnPenalty[opposed][f];
if (backward)
value -= BackwardPawnPenalty[opposed][f];
if (chain)
value += ChainBonus[f];
if (candidate)
value += CandidateBonus[relative_rank(Us, s)];
}
return value;
}
} }
namespace Pawns {
/// PawnTable::probe() takes a position object as input, computes a PawnEntry /// probe() takes a position object as input, computes a Entry object, and returns
/// object, and returns a pointer to it. The result is also stored in a hash /// a pointer to it. The result is also stored in a hash table, so we don't have
/// table, so we don't have to recompute everything when the same pawn structure /// to recompute everything when the same pawn structure occurs again.
/// occurs again.
PawnEntry* PawnTable::probe(const Position& pos) { Entry* probe(const Position& pos, Table& entries) {
Key key = pos.pawn_key(); Key key = pos.pawn_key();
PawnEntry* e = entries[key]; Entry* e = entries[key];
// If e->key matches the position's pawn hash key, it means that we // 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 // have analysed this pawn structure before, and we can simply return
@ -118,112 +216,11 @@ PawnEntry* PawnTable::probe(const Position& pos) {
} }
/// PawnTable::evaluate_pawns() evaluates each pawn of the given color /// Entry::shelter_storm() calculates shelter and storm penalties for the file
template<Color Us>
Score PawnTable::evaluate_pawns(const Position& pos, Bitboard ourPawns,
Bitboard theirPawns, PawnEntry* e) {
const Color Them = (Us == WHITE ? BLACK : WHITE);
Bitboard b;
Square s;
File f;
Rank r;
bool passed, isolated, doubled, opposed, chain, backward, candidate;
Score value = SCORE_ZERO;
const Square* pl = pos.piece_list(Us, PAWN);
// Loop through all pawns of the current color and score each pawn
while ((s = *pl++) != SQ_NONE)
{
assert(pos.piece_on(s) == make_piece(Us, PAWN));
f = file_of(s);
r = rank_of(s);
// This file cannot be half open
e->halfOpenFiles[Us] &= ~(1 << f);
// Our rank plus previous one. Used for chain detection
b = rank_bb(r) | rank_bb(Us == WHITE ? r - Rank(1) : r + Rank(1));
// Flag the pawn as passed, isolated, doubled or member of a pawn
// chain (but not the backward one).
chain = ourPawns & adjacent_files_bb(f) & b;
isolated = !(ourPawns & adjacent_files_bb(f));
doubled = ourPawns & forward_bb(Us, s);
opposed = theirPawns & forward_bb(Us, s);
passed = !(theirPawns & passed_pawn_mask(Us, s));
// Test for backward pawn
backward = false;
// If the pawn is passed, isolated, or member of a pawn chain it cannot
// be backward. If there are friendly pawns behind on adjacent files
// or if can capture an enemy pawn it cannot be backward either.
if ( !(passed | isolated | chain)
&& !(ourPawns & attack_span_mask(Them, s))
&& !(pos.attacks_from<PAWN>(s, Us) & theirPawns))
{
// We now know that there are no friendly pawns beside or behind this
// pawn on adjacent files. We now check whether the pawn is
// backward by looking in the forward direction on the adjacent
// files, and seeing whether we meet a friendly or an enemy pawn first.
b = pos.attacks_from<PAWN>(s, Us);
// Note that we are sure to find something because pawn is not passed
// nor isolated, so loop is potentially infinite, but it isn't.
while (!(b & (ourPawns | theirPawns)))
Us == WHITE ? b <<= 8 : b >>= 8;
// The friendly pawn needs to be at least two ranks closer than the
// enemy pawn in order to help the potentially backward pawn advance.
backward = (b | (Us == WHITE ? b << 8 : b >> 8)) & theirPawns;
}
assert(opposed | passed | (attack_span_mask(Us, s) & theirPawns));
// A not passed pawn is a candidate to become passed if it is free to
// advance and if the number of friendly pawns beside or behind this
// pawn on adjacent files is higher or equal than the number of
// enemy pawns in the forward direction on the adjacent files.
candidate = !(opposed | passed | backward | isolated)
&& (b = attack_span_mask(Them, s + pawn_push(Us)) & ourPawns) != 0
&& popcount<Max15>(b) >= popcount<Max15>(attack_span_mask(Us, s) & theirPawns);
// Passed pawns will be properly scored in evaluation because we need
// full attack info to evaluate passed pawns. Only the frontmost passed
// pawn on each file is considered a true passed pawn.
if (passed && !doubled)
e->passedPawns[Us] |= s;
// Score this pawn
if (isolated)
value -= IsolatedPawnPenalty[opposed][f];
if (doubled)
value -= DoubledPawnPenalty[opposed][f];
if (backward)
value -= BackwardPawnPenalty[opposed][f];
if (chain)
value += ChainBonus[f];
if (candidate)
value += CandidateBonus[relative_rank(Us, s)];
}
return value;
}
/// PawnEntry::shelter_storm() calculates shelter and storm penalties for the file
/// the king is on, as well as the two adjacent files. /// the king is on, as well as the two adjacent files.
template<Color Us> 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); 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. /// called only when king square changes, about 20% of total king_safety() calls.
template<Color Us> template<Color Us>
Score PawnEntry::update_safety(const Position& pos, Square ksq) { Score Entry::update_safety(const Position& pos, Square ksq) {
kingSquares[Us] = ksq; kingSquares[Us] = ksq;
castleRights[Us] = pos.can_castle(Us); castleRights[Us] = pos.can_castle(Us);
@ -283,5 +280,7 @@ Score PawnEntry::update_safety(const Position& pos, Square ksq) {
} }
// Explicit template instantiation // Explicit template instantiation
template Score PawnEntry::update_safety<WHITE>(const Position& pos, Square ksq); template Score Entry::update_safety<WHITE>(const Position& pos, Square ksq);
template Score PawnEntry::update_safety<BLACK>(const Position& pos, Square ksq); template Score Entry::update_safety<BLACK>(const Position& pos, Square ksq);
} // namespace Pawns

View file

@ -24,31 +24,30 @@
#include "position.h" #include "position.h"
#include "types.h" #include "types.h"
const int PawnTableSize = 16384; namespace Pawns {
/// PawnEntry is a class which contains various information about a pawn /// Pawns::Entry contains various information about a pawn structure. Currently,
/// structure. Currently, it only includes a middle game and an end game /// it only includes a middle game and end game pawn structure evaluation, and a
/// pawn structure evaluation, and a bitboard of passed pawns. We may want /// bitboard of passed pawns. We may want to add further information in the future.
/// to add further information in the future. A lookup to the pawn hash /// A lookup to the pawn hash table (performed by calling the probe function)
/// table (performed by calling the probe method in a PawnTable object) /// returns a pointer to an Entry object.
/// returns a pointer to a PawnEntry object.
class PawnEntry { struct Entry {
friend struct PawnTable; Score pawns_value() const { return value; }
Bitboard pawn_attacks(Color c) const { return pawnAttacks[c]; }
public: Bitboard passed_pawns(Color c) const { return passedPawns[c]; }
Score pawns_value() const; int file_is_half_open(Color c, File f) const { return halfOpenFiles[c] & (1 << int(f)); }
Bitboard pawn_attacks(Color c) const; int has_open_file_to_left(Color c, File f) const { return halfOpenFiles[c] & ((1 << int(f)) - 1); }
Bitboard passed_pawns(Color c) const; int has_open_file_to_right(Color c, File f) const { return halfOpenFiles[c] & ~((1 << int(f+1)) - 1); }
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;
template<Color Us> 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> template<Color Us>
Score update_safety(const Position& pos, Square ksq); Score update_safety(const Position& pos, Square ksq);
@ -66,50 +65,10 @@ private:
Score kingSafety[COLOR_NB]; Score kingSafety[COLOR_NB];
}; };
typedef HashTable<Entry, 16384> Table;
/// The PawnTable class represents a pawn hash table. The most important Entry* probe(const Position& pos, Table& entries);
/// method is probe, which returns a pointer to a PawnEntry object.
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) #endif // !defined(PAWNS_H_INCLUDED)

View file

@ -918,7 +918,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
} }
// Prefetch pawn and material hash tables // 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]); prefetch((char*)thisThread->materialTable[st->materialKey]);
// Update incremental scores // Update incremental scores

View file

@ -112,7 +112,7 @@ public:
Eval::Table evalTable; Eval::Table evalTable;
Material::Table materialTable; Material::Table materialTable;
Endgames endgames; Endgames endgames;
PawnTable pawnTable; Pawns::Table pawnsTable;
size_t idx; size_t idx;
int maxPly; int maxPly;
Mutex mutex; Mutex mutex;