From: Marco Costalba Date: Fri, 7 Jan 2011 09:34:16 +0000 (+0100) Subject: Introduce SimpleHash class X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=0ddf84870ad9f7fb4309e992e1e5eae968577958;ds=sidebyside Introduce SimpleHash class And use it for pawns and material infos. No functional change. Signed-off-by: Marco Costalba --- diff --git a/src/material.cpp b/src/material.cpp index 0098b39c..cc90c2f9 100644 --- a/src/material.cpp +++ b/src/material.cpp @@ -132,28 +132,8 @@ template<> const SFMap& EndgameFunctions::get() const { return maps.second; //// Functions //// -/// MaterialInfoTable c'tor and d'tor, called once by each thread - -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::MaterialInfoTable() { funcs = new EndgameFunctions(); } +MaterialInfoTable::~MaterialInfoTable() { delete funcs; } /// MaterialInfoTable::game_phase() calculates the phase given the current /// 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) { Key key = pos.get_material_key(); - unsigned index = unsigned(key & (MaterialTableSize - 1)); - MaterialInfo* mi = entries + index; + MaterialInfo* mi = find(key); // If mi->key matches the position's material hash key, it means that we // have analysed this material configuration before, and we can simply diff --git a/src/material.h b/src/material.h index 535c216c..d039dbbf 100644 --- a/src/material.h +++ b/src/material.h @@ -27,6 +27,7 @@ #include "endgame.h" #include "position.h" +#include "tt.h" //// @@ -67,26 +68,17 @@ private: Phase gamePhase; }; -/// The MaterialInfoTable class represents a pawn hash table. It is basically -/// just an array of MaterialInfo objects and a few methods for accessing these -/// 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. +/// The MaterialInfoTable class represents a pawn hash table. The most important +/// method is get_material_info, which returns a pointer to a MaterialInfo object. class EndgameFunctions; -class MaterialInfoTable { - - MaterialInfoTable(const MaterialInfoTable&); - MaterialInfoTable& operator=(const MaterialInfoTable&); - +class MaterialInfoTable : public SimpleHash { public: MaterialInfoTable(); ~MaterialInfoTable(); MaterialInfo* get_material_info(const Position& pos); - static Phase game_phase(const Position& pos); - private: - MaterialInfo* entries; EndgameFunctions* funcs; }; diff --git a/src/pawns.cpp b/src/pawns.cpp index e3201081..08463442 100644 --- a/src/pawns.cpp +++ b/src/pawns.cpp @@ -81,27 +81,6 @@ namespace { //// 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 /// 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()); Key key = pos.get_pawn_key(); - unsigned index = unsigned(key & (PawnTableSize - 1)); - PawnInfo* pi = entries + index; + PawnInfo* pi = find(key); // 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 diff --git a/src/pawns.h b/src/pawns.h index 86708488..e4104409 100644 --- a/src/pawns.h +++ b/src/pawns.h @@ -27,8 +27,10 @@ #include "bitboard.h" #include "position.h" +#include "tt.h" #include "value.h" + //// //// Types //// @@ -69,29 +71,21 @@ private: 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. - enum SideType { KingSide, QueenSide }; +class PawnInfoTable : public SimpleHash { - PawnInfoTable(const PawnInfoTable&); - PawnInfoTable& operator=(const PawnInfoTable&); + enum SideType { KingSide, QueenSide }; public: - PawnInfoTable(); - ~PawnInfoTable(); PawnInfo* get_pawn_info(const Position& pos) const; void prefetch(Key key) const; private: template Score evaluate_pawns(const Position& pos, Bitboard ourPawns, Bitboard theirPawns, PawnInfo* pi) const; - - PawnInfo* entries; }; diff --git a/src/tt.h b/src/tt.h index 778e5e81..f2928fb9 100644 --- a/src/tt.h +++ b/src/tt.h @@ -34,6 +34,39 @@ //// 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 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 /// /// A TTEntry needs 128 bits to be stored