[go: up one dir, main page]

Menu

[r1120]: / src / hash.c  Maximize  Restore  History

Download this file

410 lines (327 with data), 9.8 kB

  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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
Protector -- a UCI chess engine
Copyright (C) 2009-2010 Raimund Heid (Raimund_Heid@yahoo.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include "hash.h"
#include "protector.h"
#include "io.h"
#include "keytable.h"
#define NODE_TABLE_SIZE (MAX_THREADS * MAX_DEPTH * 32)
const unsigned int NUM_DATES = 16;
const unsigned int CLUSTER_SIZE = 4;
const UINT8 DEPTH_NONE = 0;
Nodeentry nodeUsageTable[NODE_TABLE_SIZE];
long numNodeTableEntries;
INT16 getHashentryValue(const Hashentry * entry)
{
return (INT16) (entry->data & 0xFFFF);
}
INT16 getHashentryStaticValue(const Hashentry * entry)
{
return (INT16) ((entry->data >> 16) & 0xFFFF);
}
UINT8 getHashentryImportance(const Hashentry * entry)
{
return (UINT8) ((entry->data >> 32) & 0xFF);
}
UINT16 getHashentryMove(const Hashentry * entry)
{
return (UINT16) ((entry->data >> 40) & 0xFFFF);
}
UINT8 getHashentryDate(const Hashentry * entry)
{
return (UINT8) ((entry->data >> 56) & 0x0F);
}
UINT8 getHashentryFlag(const Hashentry * entry)
{
return (UINT8) ((entry->data >> 60) & 0x03);
}
UINT64 getHashentryKey(const Hashentry * entry)
{
return entry->key ^ entry->data;
}
static int getAge(const Hashtable * hashtable, const UINT8 date)
{
assert(date < NUM_DATES);
assert(hashtable->date < NUM_DATES);
return (hashtable->date + NUM_DATES - date) & (NUM_DATES - 1);
}
void incrementDate(Hashtable * hashtable)
{
assert(hashtable->date < NUM_DATES);
hashtable->date = (UINT8) ((hashtable->date + 1) % NUM_DATES);
assert(hashtable->date < NUM_DATES);
}
static void deleteTables(Hashtable * hashtable)
{
if (hashtable->table != 0)
{
free(hashtable->table);
hashtable->table = 0;
}
}
static UINT64 _getHashData(INT16 value, INT16 staticValue, UINT8 importance,
UINT16 bestMove, UINT8 date, UINT8 flag)
{
return ((UINT64) (value & 0xFFFF)) |
((UINT64) (staticValue & 0xFFFF)) << 16 |
((UINT64) importance) << 32 |
((UINT64) bestMove) << 40 |
((UINT64) date) << 56 | ((UINT64) flag) << 60;
}
Hashentry constructHashEntry(UINT64 key, INT16 value, INT16 staticValue,
UINT8 importance, UINT16 bestMove, UINT8 date,
UINT8 flag)
{
Hashentry entry;
entry.key = key;
entry.data = _getHashData(value, staticValue, importance,
bestMove, date, flag);
return entry;
}
void resetHashtable(Hashtable * hashtable)
{
UINT64 l;
Hashentry emptyEntry;
emptyEntry.key = ULONG_ZERO;
emptyEntry.data = _getHashData(-VALUE_MATED, 0, DEPTH_NONE,
(UINT16) NO_MOVE, 0, HASHVALUE_UPPER_LIMIT);
for (l = 0; l < hashtable->tableSize + CLUSTER_SIZE; l++)
{
hashtable->table[l] = emptyEntry;
}
hashtable->date = 0;
hashtable->entriesUsed = 0;
/* logDebug("hashtable reset done.\n"); */
}
void resetNodetable(void)
{
int i;
for (i = 0; i < NODE_TABLE_SIZE; i++)
{
nodeUsageTable[i].key = ULONG_ZERO;
}
}
void initializeHashtable(Hashtable * hashtable)
{
hashtable->table = 0;
hashtable->tableSize = 0;
hashtable->entriesUsed = 0;
}
bool isPrimeNumber(UINT64 n)
{
UINT64 limit, d;
if (n == 2)
{
return TRUE;
}
if (n < 2 || n % 2 == 0)
{
return FALSE;
}
limit = (UINT64) (sqrt((double) n) + 1.0);
for (d = 3; d <= limit; d += 2)
{
if (n % d == 0)
{
return FALSE;
}
}
return TRUE;
}
UINT64 getNextPrime(UINT64 n)
{
while (isPrimeNumber(n) == FALSE)
{
n++;
}
return n;
}
UINT64 getPreviousPrime(UINT64 n)
{
n--;
while (isPrimeNumber(n) == FALSE)
{
n--;
}
return n;
}
void setHashtableSize(Hashtable * hashtable, UINT64 size)
{
const UINT64 ENTRY_SIZE = sizeof(Hashentry);
deleteTables(hashtable);
hashtable->tableSize = getNextPrime(size / ENTRY_SIZE);
hashtable->table =
malloc((hashtable->tableSize + CLUSTER_SIZE) * ENTRY_SIZE);
/* logDebug("Hashtable size: %ld entries\n",
hashtable->tableSize); */
}
UINT64 getHashIndex(Hashtable * hashtable, UINT64 key)
{
return key % ((hashtable)->tableSize);
}
void setHashentry(Hashtable * hashtable, UINT64 key, INT16 value,
UINT8 importance, UINT16 bestMove, UINT8 flag,
INT16 staticValue)
{
const UINT64 index = getHashIndex(hashtable, key);
UINT64 data, i, bestEntry = 0;
int bestEntryScore = -1024;
Hashentry *entryToBeReplaced;
for (i = 0; i < CLUSTER_SIZE; i++)
{
Hashentry copy = hashtable->table[index + i];
const UINT8 copyDate = getHashentryDate(&copy);
const int copyFlag = getHashentryFlag(&copy);
if (getHashentryKey(&copy) == key || copy.key == ULONG_ZERO)
{
if (copyDate != hashtable->date || copy.key == ULONG_ZERO)
{
hashtable->entriesUsed++;
}
if (bestMove == (UINT16) NO_MOVE)
{
bestMove = getHashentryMove(&copy);
}
data = _getHashData(value, staticValue, importance, bestMove,
hashtable->date, flag);
hashtable->table[index + i].key = key ^ data;
hashtable->table[index + i].data = data;
return;
}
else
{
const int score =
getAge(hashtable, copyDate) * 8 -
getHashentryImportance(&copy) -
(copyFlag == HASHVALUE_EXACT ? 24 : 0);
if (score > bestEntryScore)
{
bestEntry = i;
bestEntryScore = score;
}
}
}
if (getHashentryDate(&hashtable->table[index + bestEntry]) !=
hashtable->date)
{
hashtable->entriesUsed++;
}
entryToBeReplaced = &hashtable->table[index + bestEntry];
data = _getHashData(value, staticValue, importance, bestMove,
hashtable->date, flag);
entryToBeReplaced->key = key ^ data;
entryToBeReplaced->data = data;
}
Hashentry *getHashentry(Hashtable * hashtable, UINT64 key)
{
const UINT64 index = getHashIndex(hashtable, key);
UINT64 i;
for (i = 0; i < CLUSTER_SIZE; i++)
{
Hashentry *tableEntry = &hashtable->table[index + i];
if (getHashentryKey(tableEntry) == key)
{
if (getHashentryDate(tableEntry) != hashtable->date)
{
#ifndef NDEBUG
const Hashentry originalEntry = *tableEntry;
#endif
const UINT64 mask = (((UINT64) 0x0F) << 56);
const UINT64 newData = (tableEntry->data & ~mask) |
(((UINT64) hashtable->date) << 56);
tableEntry->key = key ^ newData;
tableEntry->data = newData;
hashtable->entriesUsed++;
assert(getHashentryValue(&originalEntry) ==
getHashentryValue(tableEntry));
assert(getHashentryStaticValue(&originalEntry) ==
getHashentryStaticValue(tableEntry));
assert(getHashentryImportance(&originalEntry) ==
getHashentryImportance(tableEntry));
assert(getHashentryMove(&originalEntry) ==
getHashentryMove(tableEntry));
assert(getHashentryFlag(&originalEntry) ==
getHashentryFlag(tableEntry));
assert(hashtable->date == getHashentryDate(tableEntry));
}
return tableEntry;
}
}
return 0;
}
UINT64 getNodeIndex(UINT64 key)
{
return key % numNodeTableEntries;
}
bool nodeIsInUse(UINT64 key, UINT8 depth)
{
UINT64 nodeIndex = getNodeIndex(key);
return nodeUsageTable[nodeIndex].key == key &&
nodeUsageTable[nodeIndex].depth >= depth && key != ULONG_ZERO;
}
bool setNodeUsage(UINT64 key, UINT8 depth)
{
UINT64 nodeIndex = getNodeIndex(key);
if (nodeUsageTable[nodeIndex].key == ULONG_ZERO)
{
nodeUsageTable[nodeIndex].key = key;
nodeUsageTable[nodeIndex].depth = depth;
return TRUE;
}
else
{
return FALSE;
}
}
void resetNodeUsage(UINT64 key, UINT8 depth)
{
UINT64 nodeIndex = getNodeIndex(key);
nodeUsageTable[nodeIndex].key = ULONG_ZERO;
}
int initializeModuleHash(void)
{
resetNodetable();
numNodeTableEntries = getPreviousPrime(NODE_TABLE_SIZE);
return 0;
}
static int testAgeCalculation(void)
{
Hashtable hashtable;
hashtable.date = 0;
assert(getAge(&hashtable, (UINT8) (NUM_DATES - 1)) == 1);
assert(getAge(&hashtable, 0) == 0);
assert(getAge(&hashtable, 1) == (int) (NUM_DATES - 1));
hashtable.date = 2;
assert(getAge(&hashtable, 1) == 1);
assert(getAge(&hashtable, 2) == 0);
assert(getAge(&hashtable, 3) == (int) (NUM_DATES - 1));
hashtable.date = (UINT8) (NUM_DATES - 1);
assert(getAge(&hashtable, (UINT8) (NUM_DATES - 2)) == 1);
assert(getAge(&hashtable, (UINT8) (NUM_DATES - 1)) == 0);
assert(getAge(&hashtable, 0) == (int) (NUM_DATES - 1));
return (hashtable.date == (UINT8) (NUM_DATES - 1) ? 0 : 1);
}
int testModuleHash(void)
{
int result = 0;
if ((result = testAgeCalculation()) != 0)
{
return result;
}
return 0;
}