-
Notifications
You must be signed in to change notification settings - Fork 4
/
hashtable.h
246 lines (203 loc) · 5.73 KB
/
hashtable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
MIT License
Copyright(c) 2016-2017 Judd Niemann
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef _HASHTABLE_H
#define _HASHTABLE_H 1
#include "movegen.h"
#include <iostream>
#include <atomic>
#include <string>
#include <cstring> // for std::memset
namespace juddperft {
// HTs are "shrunk" to this size when not in use:
#define MINIMAL_HASHTABLE_SIZE 1000000
#define _SQUEEZE_PERFT_COUNT_60BITS 1 // squeeze depth and count into single 64-bit integer (4:60 bits respectively) to make hash entries smaller.
typedef uint64_t HashKey;
typedef uint64_t ZobristKey;
class ZobristKeySet
{
public:
ZobristKeySet();
ZobristKey zkPieceOnSquare[16][64];
ZobristKey zkBlackToMove;
ZobristKey zkWhiteCanCastle;
ZobristKey zkWhiteCanCastleLong;
ZobristKey zkBlackCanCastle;
ZobristKey zkBlackCanCastleLong;
ZobristKey zkPerftDepth[24];
// pre-fabricated combinations of keys for castling:
ZobristKey zkDoBlackCastle;
ZobristKey zkDoBlackCastleLong;
ZobristKey zkDoWhiteCastle;
ZobristKey zkDoWhiteCastleLong;
//
bool generate();
};
// generic Hashtable template:
template<class T> class HashTable
{
public:
HashTable(const std::string& name = std::string("Hash Table"));
~HashTable();
bool setSize(uint64_t nBytes);
bool deAllocate();
T* getAddress(const HashKey& SearchHK) const;
uint64_t getSize() const; // return currently-allocated size in bytes
uint64_t getRequestedSize() const; // return what was originally requested in bytes
uint64_t getNumEntries() const;
double getLoadFactor() const;
void clear();
private:
T* m_pTable;
uint64_t m_nEntries;
uint64_t m_nIndexMask;
uint64_t m_nRequestedSize;
uint64_t m_nWrites;
uint64_t m_nCollisions;
std::string m_Name;
};
template<class T>
inline HashTable<T>::HashTable(const std::string& name) : m_Name(name)
{
m_pTable = nullptr;
m_nCollisions = 0;
m_nEntries = 0;
m_nIndexMask = 0;
m_nWrites = 0;
}
template<class T>
inline HashTable<T>::~HashTable()
{
if (m_pTable != nullptr) {
std::cout << "deallocating " << m_Name << std::endl;
delete[] m_pTable;
m_pTable = nullptr;
}
}
template<class T>
inline bool HashTable<T>::setSize(uint64_t nBytes)
{
m_nRequestedSize = nBytes;
uint64_t nNewNumEntries = 1LL;
// Make nNewSize a power of 2:
while (nNewNumEntries*sizeof(T) < nBytes) {
nNewNumEntries <<= 1;
}
nNewNumEntries >>= 1;
if (nNewNumEntries == m_nEntries) {
// No change in size
return false;
}
m_nEntries = nNewNumEntries;
// create a mask with all 1's (2^n - 1) for address calculation:
HashTable::m_nIndexMask = m_nEntries - 1;
HashTable::deAllocate();
m_pTable = new (std::nothrow) T[m_nEntries];
if (m_pTable == nullptr) {
std::cout << "Failed to allocate " << nBytes << " bytes for " << m_Name << std::endl;
return false;
}
else {
std::cout << "Allocated " << m_nEntries * sizeof(T) << " bytes for " << m_Name << " (" << m_nEntries << " entries at " << sizeof(T) << " bytes each)" << std::endl;
m_nCollisions = 0;
m_nWrites = 0;
HashTable<T>::clear();
return true;
}
}
template<class T>
inline bool HashTable<T>::deAllocate()
{
if (HashTable::m_pTable != nullptr) // to-do: do we need to do all this nullptr crap ?
{
std::cout << "deallocating " << m_Name << std::endl;
delete[] m_pTable;
m_pTable = nullptr;
return true;
}
else {
return false;
}
}
template<class T>
inline T * HashTable<T>::getAddress(const HashKey & SearchHK) const
{
return m_pTable + (SearchHK & m_nIndexMask);
}
template<class T>
inline uint64_t HashTable<T>::getSize() const
{
return m_nEntries*sizeof(T);
}
template<class T>
inline uint64_t HashTable<T>::getRequestedSize() const
{
return m_nRequestedSize;
}
template<class T>
inline uint64_t HashTable<T>::getNumEntries() const
{
return m_nEntries;
}
template<class T>
inline double HashTable<T>::getLoadFactor() const
{
return
static_cast<double>((1000 * m_nWrites) / m_nEntries) / 1000;
}
template<class T>
inline void HashTable<T>::clear()
{
if (m_pTable != nullptr)
std::memset(m_pTable, 0, sizeof(T)*m_nEntries);
}
//
struct PerftTableEntry
{
HashKey Hash;
#ifndef _SQUEEZE_PERFT_COUNT_60BITS
int depth;
unsigned long long count;
#else
// more compact (squeeze count and depth into 64 bits):
union {
struct {
unsigned long long count : 60;
unsigned long long depth : 4;
// warning: limitations are: max depth=15, max count = 2^60 = 1,152,921,504,606,846,976
// which only allows up to perft 12 from start position
};
unsigned long long data;
};
#endif
// Note: std::atomic<> version of this appears to add 8 bytes on msvc
};
struct LeafEntry
{
HashKey Hash;
unsigned char count;
};
#ifdef _USE_HASH
// Global instances:
extern ZobristKeySet zobristKeys;
extern HashTable <std::atomic<PerftTableEntry>> perftTable;
extern HashTable <std::atomic<LeafEntry>> leafTable;
#endif
} // namespace juddperft
#endif // _HASHTABLE_H