mirror of
https://github.com/sockspls/badfish
synced 2025-05-01 01:03:09 +00:00
Introduce SimpleHash class
And use it for pawns and material infos. No functional change. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
parent
803c8e0be3
commit
0ddf84870a
5 changed files with 47 additions and 71 deletions
|
@ -132,28 +132,8 @@ template<> const SFMap& EndgameFunctions::get<SF>() const { return maps.second;
|
||||||
//// Functions
|
//// Functions
|
||||||
////
|
////
|
||||||
|
|
||||||
/// MaterialInfoTable c'tor and d'tor, called once by each thread
|
MaterialInfoTable::MaterialInfoTable() { funcs = new EndgameFunctions(); }
|
||||||
|
MaterialInfoTable::~MaterialInfoTable() { delete funcs; }
|
||||||
MaterialInfoTable::MaterialInfoTable() {
|
|
||||||
|
|
||||||
entries = new MaterialInfo[MaterialTableSize];
|
|
||||||
funcs = new EndgameFunctions();
|
|
||||||
|
|
||||||
if (!entries || !funcs)
|
|
||||||
{
|
|
||||||
cerr << "Failed to allocate " << MaterialTableSize * sizeof(MaterialInfo)
|
|
||||||
<< " bytes for material hash table." << endl;
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
memset(entries, 0, MaterialTableSize * sizeof(MaterialInfo));
|
|
||||||
}
|
|
||||||
|
|
||||||
MaterialInfoTable::~MaterialInfoTable() {
|
|
||||||
|
|
||||||
delete funcs;
|
|
||||||
delete [] entries;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// MaterialInfoTable::game_phase() calculates the phase given the current
|
/// MaterialInfoTable::game_phase() calculates the phase given the current
|
||||||
/// position. Because the phase is strictly a function of the material, it
|
/// position. Because the phase is strictly a function of the material, it
|
||||||
|
@ -181,8 +161,7 @@ Phase MaterialInfoTable::game_phase(const Position& pos) {
|
||||||
MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) {
|
MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) {
|
||||||
|
|
||||||
Key key = pos.get_material_key();
|
Key key = pos.get_material_key();
|
||||||
unsigned index = unsigned(key & (MaterialTableSize - 1));
|
MaterialInfo* mi = find(key);
|
||||||
MaterialInfo* mi = entries + index;
|
|
||||||
|
|
||||||
// If mi->key matches the position's material hash key, it means that we
|
// If mi->key matches the position's material hash key, it means that we
|
||||||
// have analysed this material configuration before, and we can simply
|
// have analysed this material configuration before, and we can simply
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
|
|
||||||
#include "endgame.h"
|
#include "endgame.h"
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
|
#include "tt.h"
|
||||||
|
|
||||||
|
|
||||||
////
|
////
|
||||||
|
@ -67,26 +68,17 @@ private:
|
||||||
Phase gamePhase;
|
Phase gamePhase;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The MaterialInfoTable class represents a pawn hash table. It is basically
|
/// The MaterialInfoTable class represents a pawn hash table. The most important
|
||||||
/// just an array of MaterialInfo objects and a few methods for accessing these
|
/// method is get_material_info, which returns a pointer to a MaterialInfo object.
|
||||||
/// objects. The most important method is get_material_info, which looks up a
|
|
||||||
/// position in the table and returns a pointer to a MaterialInfo object.
|
|
||||||
class EndgameFunctions;
|
class EndgameFunctions;
|
||||||
|
|
||||||
class MaterialInfoTable {
|
class MaterialInfoTable : public SimpleHash<MaterialInfo, MaterialTableSize> {
|
||||||
|
|
||||||
MaterialInfoTable(const MaterialInfoTable&);
|
|
||||||
MaterialInfoTable& operator=(const MaterialInfoTable&);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
MaterialInfoTable();
|
MaterialInfoTable();
|
||||||
~MaterialInfoTable();
|
~MaterialInfoTable();
|
||||||
MaterialInfo* get_material_info(const Position& pos);
|
MaterialInfo* get_material_info(const Position& pos);
|
||||||
|
|
||||||
static Phase game_phase(const Position& pos);
|
static Phase game_phase(const Position& pos);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MaterialInfo* entries;
|
|
||||||
EndgameFunctions* funcs;
|
EndgameFunctions* funcs;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -81,27 +81,6 @@ namespace {
|
||||||
//// Functions
|
//// Functions
|
||||||
////
|
////
|
||||||
|
|
||||||
/// PawnInfoTable c'tor and d'tor instantiated one each thread
|
|
||||||
|
|
||||||
PawnInfoTable::PawnInfoTable() {
|
|
||||||
|
|
||||||
entries = new PawnInfo[PawnTableSize];
|
|
||||||
|
|
||||||
if (!entries)
|
|
||||||
{
|
|
||||||
std::cerr << "Failed to allocate " << (PawnTableSize * sizeof(PawnInfo))
|
|
||||||
<< " bytes for pawn hash table." << std::endl;
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
memset(entries, 0, PawnTableSize * sizeof(PawnInfo));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
PawnInfoTable::~PawnInfoTable() {
|
|
||||||
|
|
||||||
delete [] entries;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// PawnInfoTable::get_pawn_info() takes a position object as input, computes
|
/// PawnInfoTable::get_pawn_info() takes a position object as input, computes
|
||||||
/// a PawnInfo object, and returns a pointer to it. The result is also stored
|
/// a PawnInfo object, and returns a pointer to it. The result is also stored
|
||||||
|
@ -113,8 +92,7 @@ PawnInfo* PawnInfoTable::get_pawn_info(const Position& pos) const {
|
||||||
assert(pos.is_ok());
|
assert(pos.is_ok());
|
||||||
|
|
||||||
Key key = pos.get_pawn_key();
|
Key key = pos.get_pawn_key();
|
||||||
unsigned index = unsigned(key & (PawnTableSize - 1));
|
PawnInfo* pi = find(key);
|
||||||
PawnInfo* pi = entries + index;
|
|
||||||
|
|
||||||
// If pi->key matches the position's pawn hash key, it means that we
|
// If pi->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
|
||||||
|
|
18
src/pawns.h
18
src/pawns.h
|
@ -27,8 +27,10 @@
|
||||||
|
|
||||||
#include "bitboard.h"
|
#include "bitboard.h"
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
|
#include "tt.h"
|
||||||
#include "value.h"
|
#include "value.h"
|
||||||
|
|
||||||
|
|
||||||
////
|
////
|
||||||
//// Types
|
//// Types
|
||||||
////
|
////
|
||||||
|
@ -69,29 +71,21 @@ private:
|
||||||
Score kingShelters[2];
|
Score kingShelters[2];
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The PawnInfoTable class represents a pawn hash table. It is basically
|
|
||||||
/// just an array of PawnInfo objects and a few methods for accessing these
|
|
||||||
/// objects. The most important method is get_pawn_info, which looks up a
|
|
||||||
/// position in the table and returns a pointer to a PawnInfo object.
|
|
||||||
|
|
||||||
class PawnInfoTable {
|
/// The PawnInfoTable class represents a pawn hash table. The most important
|
||||||
|
/// method is get_pawn_info, which returns a pointer to a PawnInfo object.
|
||||||
|
|
||||||
|
class PawnInfoTable : public SimpleHash<PawnInfo, PawnTableSize> {
|
||||||
|
|
||||||
enum SideType { KingSide, QueenSide };
|
enum SideType { KingSide, QueenSide };
|
||||||
|
|
||||||
PawnInfoTable(const PawnInfoTable&);
|
|
||||||
PawnInfoTable& operator=(const PawnInfoTable&);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PawnInfoTable();
|
|
||||||
~PawnInfoTable();
|
|
||||||
PawnInfo* get_pawn_info(const Position& pos) const;
|
PawnInfo* get_pawn_info(const Position& pos) const;
|
||||||
void prefetch(Key key) const;
|
void prefetch(Key key) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template<Color Us>
|
template<Color Us>
|
||||||
Score evaluate_pawns(const Position& pos, Bitboard ourPawns, Bitboard theirPawns, PawnInfo* pi) const;
|
Score evaluate_pawns(const Position& pos, Bitboard ourPawns, Bitboard theirPawns, PawnInfo* pi) const;
|
||||||
|
|
||||||
PawnInfo* entries;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
33
src/tt.h
33
src/tt.h
|
@ -34,6 +34,39 @@
|
||||||
//// Types
|
//// Types
|
||||||
////
|
////
|
||||||
|
|
||||||
|
|
||||||
|
/// A simple fixed size hash table used to store pawns and material
|
||||||
|
/// configurations. It is basically just an array of Entry objects.
|
||||||
|
/// Without cluster concept or overwrite policy.
|
||||||
|
|
||||||
|
template<class Entry, int HashSize>
|
||||||
|
class SimpleHash {
|
||||||
|
|
||||||
|
SimpleHash(const SimpleHash&);
|
||||||
|
SimpleHash& operator=(const SimpleHash&);
|
||||||
|
|
||||||
|
public:
|
||||||
|
SimpleHash() {
|
||||||
|
|
||||||
|
entries = new Entry[HashSize];
|
||||||
|
if (!entries)
|
||||||
|
{
|
||||||
|
std::cerr << "Failed to allocate " << HashSize * sizeof(Entry)
|
||||||
|
<< " bytes for material hash table." << std::endl;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
memset(entries, 0, HashSize * sizeof(Entry));
|
||||||
|
}
|
||||||
|
|
||||||
|
~SimpleHash() { delete [] entries; }
|
||||||
|
|
||||||
|
Entry* find(Key key) const { return entries + unsigned(key & (HashSize - 1)); }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Entry* entries;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/// The TTEntry class is the class of transposition table entries
|
/// The TTEntry class is the class of transposition table entries
|
||||||
///
|
///
|
||||||
/// A TTEntry needs 128 bits to be stored
|
/// A TTEntry needs 128 bits to be stored
|
||||||
|
|
Loading…
Add table
Reference in a new issue