Skip to content

Commit 0ddf848

Browse files
committed
Introduce SimpleHash class
And use it for pawns and material infos. No functional change. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
1 parent 803c8e0 commit 0ddf848

5 files changed

Lines changed: 47 additions & 71 deletions

File tree

src/material.cpp

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -132,28 +132,8 @@ template<> const SFMap& EndgameFunctions::get<SF>() const { return maps.second;
132132
//// Functions
133133
////
134134

135-
/// MaterialInfoTable c'tor and d'tor, called once by each thread
136-
137-
MaterialInfoTable::MaterialInfoTable() {
138-
139-
entries = new MaterialInfo[MaterialTableSize];
140-
funcs = new EndgameFunctions();
141-
142-
if (!entries || !funcs)
143-
{
144-
cerr << "Failed to allocate " << MaterialTableSize * sizeof(MaterialInfo)
145-
<< " bytes for material hash table." << endl;
146-
exit(EXIT_FAILURE);
147-
}
148-
memset(entries, 0, MaterialTableSize * sizeof(MaterialInfo));
149-
}
150-
151-
MaterialInfoTable::~MaterialInfoTable() {
152-
153-
delete funcs;
154-
delete [] entries;
155-
}
156-
135+
MaterialInfoTable::MaterialInfoTable() { funcs = new EndgameFunctions(); }
136+
MaterialInfoTable::~MaterialInfoTable() { delete funcs; }
157137

158138
/// MaterialInfoTable::game_phase() calculates the phase given the current
159139
/// position. Because the phase is strictly a function of the material, it
@@ -181,8 +161,7 @@ Phase MaterialInfoTable::game_phase(const Position& pos) {
181161
MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) {
182162

183163
Key key = pos.get_material_key();
184-
unsigned index = unsigned(key & (MaterialTableSize - 1));
185-
MaterialInfo* mi = entries + index;
164+
MaterialInfo* mi = find(key);
186165

187166
// If mi->key matches the position's material hash key, it means that we
188167
// have analysed this material configuration before, and we can simply

src/material.h

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "endgame.h"
2929
#include "position.h"
30+
#include "tt.h"
3031

3132

3233
////
@@ -67,26 +68,17 @@ class MaterialInfo {
6768
Phase gamePhase;
6869
};
6970

70-
/// The MaterialInfoTable class represents a pawn hash table. It is basically
71-
/// just an array of MaterialInfo objects and a few methods for accessing these
72-
/// objects. The most important method is get_material_info, which looks up a
73-
/// position in the table and returns a pointer to a MaterialInfo object.
71+
/// The MaterialInfoTable class represents a pawn hash table. The most important
72+
/// method is get_material_info, which returns a pointer to a MaterialInfo object.
7473
class EndgameFunctions;
7574

76-
class MaterialInfoTable {
77-
78-
MaterialInfoTable(const MaterialInfoTable&);
79-
MaterialInfoTable& operator=(const MaterialInfoTable&);
80-
75+
class MaterialInfoTable : public SimpleHash<MaterialInfo, MaterialTableSize> {
8176
public:
8277
MaterialInfoTable();
8378
~MaterialInfoTable();
8479
MaterialInfo* get_material_info(const Position& pos);
85-
8680
static Phase game_phase(const Position& pos);
87-
8881
private:
89-
MaterialInfo* entries;
9082
EndgameFunctions* funcs;
9183
};
9284

src/pawns.cpp

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -81,27 +81,6 @@ namespace {
8181
//// Functions
8282
////
8383

84-
/// PawnInfoTable c'tor and d'tor instantiated one each thread
85-
86-
PawnInfoTable::PawnInfoTable() {
87-
88-
entries = new PawnInfo[PawnTableSize];
89-
90-
if (!entries)
91-
{
92-
std::cerr << "Failed to allocate " << (PawnTableSize * sizeof(PawnInfo))
93-
<< " bytes for pawn hash table." << std::endl;
94-
exit(EXIT_FAILURE);
95-
}
96-
memset(entries, 0, PawnTableSize * sizeof(PawnInfo));
97-
}
98-
99-
100-
PawnInfoTable::~PawnInfoTable() {
101-
102-
delete [] entries;
103-
}
104-
10584

10685
/// PawnInfoTable::get_pawn_info() takes a position object as input, computes
10786
/// 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 {
11392
assert(pos.is_ok());
11493

11594
Key key = pos.get_pawn_key();
116-
unsigned index = unsigned(key & (PawnTableSize - 1));
117-
PawnInfo* pi = entries + index;
95+
PawnInfo* pi = find(key);
11896

11997
// If pi->key matches the position's pawn hash key, it means that we
12098
// have analysed this pawn structure before, and we can simply return

src/pawns.h

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727

2828
#include "bitboard.h"
2929
#include "position.h"
30+
#include "tt.h"
3031
#include "value.h"
3132

33+
3234
////
3335
//// Types
3436
////
@@ -69,29 +71,21 @@ class PawnInfo {
6971
Score kingShelters[2];
7072
};
7173

72-
/// The PawnInfoTable class represents a pawn hash table. It is basically
73-
/// just an array of PawnInfo objects and a few methods for accessing these
74-
/// objects. The most important method is get_pawn_info, which looks up a
75-
/// position in the table and returns a pointer to a PawnInfo object.
7674

77-
class PawnInfoTable {
75+
/// The PawnInfoTable class represents a pawn hash table. The most important
76+
/// method is get_pawn_info, which returns a pointer to a PawnInfo object.
7877

79-
enum SideType { KingSide, QueenSide };
78+
class PawnInfoTable : public SimpleHash<PawnInfo, PawnTableSize> {
8079

81-
PawnInfoTable(const PawnInfoTable&);
82-
PawnInfoTable& operator=(const PawnInfoTable&);
80+
enum SideType { KingSide, QueenSide };
8381

8482
public:
85-
PawnInfoTable();
86-
~PawnInfoTable();
8783
PawnInfo* get_pawn_info(const Position& pos) const;
8884
void prefetch(Key key) const;
8985

9086
private:
9187
template<Color Us>
9288
Score evaluate_pawns(const Position& pos, Bitboard ourPawns, Bitboard theirPawns, PawnInfo* pi) const;
93-
94-
PawnInfo* entries;
9589
};
9690

9791

src/tt.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,39 @@
3434
//// Types
3535
////
3636

37+
38+
/// A simple fixed size hash table used to store pawns and material
39+
/// configurations. It is basically just an array of Entry objects.
40+
/// Without cluster concept or overwrite policy.
41+
42+
template<class Entry, int HashSize>
43+
class SimpleHash {
44+
45+
SimpleHash(const SimpleHash&);
46+
SimpleHash& operator=(const SimpleHash&);
47+
48+
public:
49+
SimpleHash() {
50+
51+
entries = new Entry[HashSize];
52+
if (!entries)
53+
{
54+
std::cerr << "Failed to allocate " << HashSize * sizeof(Entry)
55+
<< " bytes for material hash table." << std::endl;
56+
exit(EXIT_FAILURE);
57+
}
58+
memset(entries, 0, HashSize * sizeof(Entry));
59+
}
60+
61+
~SimpleHash() { delete [] entries; }
62+
63+
Entry* find(Key key) const { return entries + unsigned(key & (HashSize - 1)); }
64+
65+
protected:
66+
Entry* entries;
67+
};
68+
69+
3770
/// The TTEntry class is the class of transposition table entries
3871
///
3972
/// A TTEntry needs 128 bits to be stored

0 commit comments

Comments
 (0)