And use it for pawns and material infos.
No functional change.
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
//// 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
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
#include "endgame.h"
#include "position.h"
+#include "tt.h"
////
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<MaterialInfo, MaterialTableSize> {
public:
MaterialInfoTable();
~MaterialInfoTable();
MaterialInfo* get_material_info(const Position& pos);
-
static Phase game_phase(const Position& pos);
-
private:
- MaterialInfo* entries;
EndgameFunctions* funcs;
};
//// 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
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
#include "bitboard.h"
#include "position.h"
+#include "tt.h"
#include "value.h"
+
////
//// Types
////
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<PawnInfo, PawnTableSize> {
- 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<Color Us>
Score evaluate_pawns(const Position& pos, Bitboard ourPawns, Bitboard theirPawns, PawnInfo* pi) const;
-
- PawnInfo* entries;
};
//// 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
///
/// A TTEntry needs 128 bits to be stored