forked from hoshir/zebra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hash.c
533 lines (432 loc) · 12.5 KB
/
hash.c
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
File: hash.c
Created: June 29, 1997
Modified: November 15, 2005
Author: Gunnar Andersson ([email protected])
Toshihiko Okuhara
Contents: Routines manipulating the hash table
This piece of software is released under the GPL.
See the file COPYING for more information.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "error.h"
#include "hash.h"
#include "macros.h"
#include "myrandom.h"
#include "safemem.h"
#include "search.h"
/* Substitute an old position with draft n if the new position has
draft >= n-4 */
#define REPLACEMENT_OFFSET 4
/* An 8-bit mask to isolate the "draft" part of the hash table info. */
#define DRAFT_MASK 255
#define KEY1_MASK 0xFF000000u
#define SECONDARY_HASH( a ) ((a) ^ 1)
typedef struct {
unsigned int key2;
int eval;
unsigned int moves;
unsigned int key1_selectivity_flags_draft;
} CompactHashEntry;
/* Global variables */
int hash_size;
unsigned int hash1;
unsigned int hash2;
unsigned int hash_value1[3][128];
unsigned int hash_value2[3][128];
unsigned int hash_put_value1[3][128];
unsigned int hash_put_value2[3][128];
unsigned int hash_flip1[128];
unsigned int hash_flip2[128];
unsigned int hash_color1[3];
unsigned int hash_color2[3];
unsigned int hash_flip_color1;
unsigned int hash_flip_color2;
unsigned int hash_diff1[MAX_SEARCH_DEPTH];
unsigned int hash_diff2[MAX_SEARCH_DEPTH];
unsigned int hash_stored1[MAX_SEARCH_DEPTH];
unsigned int hash_stored2[MAX_SEARCH_DEPTH];
/* Local variables */
static int hash_bits;
static int hash_mask;
static int rehash_count;
static unsigned int hash_trans1 = 0;
static unsigned int hash_trans2 = 0;
static CompactHashEntry *hash_table;
/*
INIT_HASH
Allocate memory for the hash table.
*/
void
init_hash( int in_hash_bits ) {
hash_bits = in_hash_bits;
hash_size = 1 << hash_bits;
hash_mask = hash_size - 1;
hash_table =
(CompactHashEntry *) safe_malloc( hash_size * sizeof( CompactHashEntry ) );
rehash_count = 0;
}
/*
RESIZE_HASH
Changes the size of the hash table.
*/
void
resize_hash( int new_hash_bits ) {
free( hash_table );
init_hash( new_hash_bits );
setup_hash( TRUE );
}
/*
POPCOUNT
*/
static unsigned int
popcount( unsigned int b ) {
unsigned int n;
for ( n = 0; b != 0; n++, b &= (b - 1) )
;
return n;
}
/*
GET_CLOSENESS
Returns the closeness between the 64-bit integers (a0,a1) and (b0,b1).
A closeness of 0 means that 32 bits differ.
*/
static unsigned int
get_closeness( unsigned int a0, unsigned int a1,
unsigned int b0, unsigned int b1 ) {
return abs( popcount( a0 ^ b0 ) + popcount( a1 ^ b1 ) - 32 );
}
/*
SETUP_HASH
Determine randomized hash masks.
*/
void
setup_hash( int clear ) {
int i, j;
int pos;
int rand_index;
const unsigned int max_pair_closeness = 10;
const unsigned int max_zero_closeness = 9;
unsigned int closeness;
unsigned int random_pair[130][2];
if ( clear )
for ( i = 0; i < hash_size; i++ ) {
hash_table[i].key1_selectivity_flags_draft &= ~DRAFT_MASK;
hash_table[i].key2 = 0;
}
rand_index = 0;
while ( rand_index < 130 ) {
TRY_AGAIN:
random_pair[rand_index][0] = (my_random() << 3) + (my_random() >> 2);
random_pair[rand_index][1] = (my_random() << 3) + (my_random() >> 2);
closeness =
get_closeness( random_pair[rand_index][0], random_pair[rand_index][1],
0, 0 );
if ( closeness > max_zero_closeness )
goto TRY_AGAIN;
for ( i = 0; i < rand_index; i++ ) {
closeness =
get_closeness( random_pair[rand_index][0], random_pair[rand_index][1],
random_pair[i][0], random_pair[i][1] );
if ( closeness > max_pair_closeness )
goto TRY_AGAIN;
closeness =
get_closeness( random_pair[rand_index][0], random_pair[rand_index][1],
random_pair[i][1], random_pair[i][0] );
if ( closeness > max_pair_closeness )
goto TRY_AGAIN;
}
rand_index++;
}
rand_index = 0;
for ( i = 0; i < 128; i++ ) {
hash_value1[BLACKSQ][i] = 0;
hash_value2[BLACKSQ][i] = 0;
hash_value1[WHITESQ][i] = 0;
hash_value2[WHITESQ][i] = 0;
}
for ( i = 1; i <= 8; i++ )
for ( j = 1; j <= 8; j++ ) {
pos = 10 * i + j;
hash_value1[BLACKSQ][pos] = random_pair[rand_index][0];
hash_value2[BLACKSQ][pos] = random_pair[rand_index][1];
rand_index++;
hash_value1[WHITESQ][pos] = random_pair[rand_index][0];
hash_value2[WHITESQ][pos] = random_pair[rand_index][1];
rand_index++;
}
for ( i = 0; i < 128; i++ ) {
hash_flip1[i] = hash_value1[BLACKSQ][i] ^ hash_value1[WHITESQ][i];
hash_flip2[i] = hash_value2[BLACKSQ][i] ^ hash_value2[WHITESQ][i];
}
hash_color1[BLACKSQ] = random_pair[rand_index][0];
hash_color2[BLACKSQ] = random_pair[rand_index][1];
rand_index++;
hash_color1[WHITESQ] = random_pair[rand_index][0];
hash_color2[WHITESQ] = random_pair[rand_index][1];
rand_index++;
hash_flip_color1 = hash_color1[BLACKSQ] ^ hash_color1[WHITESQ];
hash_flip_color2 = hash_color2[BLACKSQ] ^ hash_color2[WHITESQ];
for ( j = 0; j < 128; j++ ) {
hash_put_value1[BLACKSQ][j] = hash_value1[BLACKSQ][j] ^ hash_flip_color1;
hash_put_value2[BLACKSQ][j] = hash_value2[BLACKSQ][j] ^ hash_flip_color2;
hash_put_value1[WHITESQ][j] = hash_value1[WHITESQ][j] ^ hash_flip_color1;
hash_put_value2[WHITESQ][j] = hash_value2[WHITESQ][j] ^ hash_flip_color2;
}
}
/*
CLEAR_HASH_DRAFTS
Resets the draft information for all entries in the hash table.
*/
void
clear_hash_drafts( void ) {
int i;
for ( i = 0; i < hash_size; i++ ) /* Set the draft to 0 */
hash_table[i].key1_selectivity_flags_draft &= ~0x0FF;
}
/*
FREE_HASH
Remove the hash table.
*/
void
free_hash( void ) {
free( hash_table );
}
/*
DETERMINE_HASH_VALUES
Calculates the hash codes for the given board position.
*/
void
determine_hash_values( int side_to_move,
const int *board ) {
int i, j;
hash1 = 0;
hash2 = 0;
for ( i = 1; i <= 8; i++ )
for ( j = 1; j <= 8; j++ ) {
int pos = 10 * i + j;
switch ( board[pos] ) {
case BLACKSQ:
hash1 ^= hash_value1[BLACKSQ][pos];
hash2 ^= hash_value2[BLACKSQ][pos];
break;
case WHITESQ:
hash1 ^= hash_value1[WHITESQ][pos];
hash2 ^= hash_value2[WHITESQ][pos];
break;
default:
break;
}
}
hash1 ^= hash_color1[side_to_move];
hash2 ^= hash_color2[side_to_move];
}
/*
WIDE_TO_COMPACT
Convert the easily readable representation to the more
compact one actually stored in the hash table.
*/
static INLINE void
wide_to_compact( const HashEntry *entry, CompactHashEntry *compact_entry ) {
compact_entry->key2 = entry->key2;
compact_entry->eval = entry->eval;
compact_entry->moves = entry->move[0] + (entry->move[1] << 8) +
(entry->move[2] << 16) + (entry->move[3] << 24);
compact_entry->key1_selectivity_flags_draft =
(entry->key1 & KEY1_MASK) + (entry->selectivity << 16) +
(entry->flags << 8) + entry->draft;
}
/*
COMPACT_TO_WIDE
Expand the compact internal representation of entries
in the hash table to something more usable.
*/
static INLINE void
compact_to_wide( const CompactHashEntry *compact_entry, HashEntry *entry ) {
entry->key2 = compact_entry->key2;
entry->eval = compact_entry->eval;
entry->move[0] = compact_entry->moves & 255;
entry->move[1] = (compact_entry->moves >> 8) & 255;
entry->move[2] = (compact_entry->moves >> 16) & 255;
entry->move[3] = (compact_entry->moves >> 24) & 255;
entry->key1 = compact_entry->key1_selectivity_flags_draft & KEY1_MASK;
entry->selectivity =
(compact_entry->key1_selectivity_flags_draft & 0x00ffffff) >> 16;
entry->flags =
(compact_entry->key1_selectivity_flags_draft & 0x0000ffff) >> 8;
entry->draft =
(compact_entry->key1_selectivity_flags_draft & 0x000000ff);
}
/*
SET_HASH_TRANSFORMATION
Specify the hash code transformation masks. Changing these masks
is the poor man's way to achieve the effect of clearing the hash
table.
*/
void
set_hash_transformation( unsigned int trans1, unsigned int trans2 ) {
hash_trans1 = trans1;
hash_trans2 = trans2;
}
/*
ADD_HASH
Add information to the hash table. Two adjacent positions are tried
and the most shallow search is replaced.
*/
void
add_hash( int reverse_mode,
int score,
int best,
int flags,
int draft,
int selectivity ) {
int old_draft;
int change_encouragment;
unsigned int index, index1, index2;
unsigned int code1, code2;
HashEntry entry;
assert( abs( score ) != SEARCH_ABORT );
if ( reverse_mode ) {
code1 = hash2 ^ hash_trans2;
code2 = hash1 ^ hash_trans1;
}
else {
code1 = hash1 ^ hash_trans1;
code2 = hash2 ^ hash_trans2;
}
index1 = code1 & hash_mask;
index2 = SECONDARY_HASH( index1 );
if ( hash_table[index1].key2 == code2 )
index = index1;
else {
if ( hash_table[index2].key2 == code2 )
index = index2;
else {
if ( (hash_table[index1].key1_selectivity_flags_draft & DRAFT_MASK) <=
(hash_table[index2].key1_selectivity_flags_draft & DRAFT_MASK) )
index = index1;
else
index = index2;
}
}
old_draft = hash_table[index].key1_selectivity_flags_draft & DRAFT_MASK;
if ( flags & EXACT_VALUE ) /* Exact scores are potentially more useful */
change_encouragment = 2;
else
change_encouragment = 0;
if ( hash_table[index].key2 == code2 ) {
if ( old_draft > draft + change_encouragment + 2 )
return;
}
else if ( old_draft > draft + change_encouragment + REPLACEMENT_OFFSET )
return;
entry.key1 = code1;
entry.key2 = code2;
entry.eval = score;
entry.move[0] = best;
entry.move[1] = 0;
entry.move[2] = 0;
entry.move[3] = 0;
entry.flags = (short) flags;
entry.draft = (short) draft;
entry.selectivity = selectivity;
wide_to_compact( &entry, &hash_table[index] );
}
/*
ADD_HASH_EXTENDED
Add information to the hash table. Two adjacent positions are tried
and the most shallow search is replaced.
*/
void
add_hash_extended( int reverse_mode, int score, int *best, int flags,
int draft, int selectivity ) {
int i;
int old_draft;
int change_encouragment;
unsigned int index, index1, index2;
unsigned int code1, code2;
HashEntry entry;
if ( reverse_mode ) {
code1 = hash2 ^ hash_trans2;
code2 = hash1 ^ hash_trans1;
}
else {
code1 = hash1 ^ hash_trans1;
code2 = hash2 ^ hash_trans2;
}
index1 = code1 & hash_mask;
index2 = SECONDARY_HASH( index1 );
if ( hash_table[index1].key2 == code2 )
index = index1;
else {
if ( hash_table[index2].key2 == code2 )
index = index2;
else {
if ( (hash_table[index1].key1_selectivity_flags_draft & DRAFT_MASK) <=
(hash_table[index2].key1_selectivity_flags_draft & DRAFT_MASK) )
index = index1;
else
index = index2;
}
}
old_draft = hash_table[index].key1_selectivity_flags_draft & DRAFT_MASK;
if ( flags & EXACT_VALUE ) /* Exact scores are potentially more useful */
change_encouragment = 2;
else
change_encouragment = 0;
if ( hash_table[index].key2 == code2 ) {
if ( old_draft > draft + change_encouragment + 2 )
return;
}
else if ( old_draft > draft + change_encouragment + REPLACEMENT_OFFSET )
return;
entry.key1 = code1;
entry.key2 = code2;
entry.eval = score;
for ( i = 0; i < 4; i++ )
entry.move[i] = best[i];
entry.flags = (short) flags;
entry.draft = (short) draft;
entry.selectivity = selectivity;
wide_to_compact( &entry, &hash_table[index] );
}
/*
FIND_HASH
Search the hash table for the current position. The two possible
hash table positions are probed.
*/
void REGPARM(2)
find_hash( HashEntry *entry, int reverse_mode ) {
int index1, index2;
unsigned int code1, code2;
if ( reverse_mode ) {
code1 = hash2 ^ hash_trans2;
code2 = hash1 ^ hash_trans1;
}
else {
code1 = hash1 ^ hash_trans1;
code2 = hash2 ^ hash_trans2;
}
index1 = code1 & hash_mask;
index2 = SECONDARY_HASH( index1 );
if ( hash_table[index1].key2 == code2 ) {
if ( ((hash_table[index1].key1_selectivity_flags_draft ^ code1) & KEY1_MASK) == 0 ) {
compact_to_wide( &hash_table[index1], entry );
return;
}
}
else if ( (hash_table[index2].key2 == code2) &&
(((hash_table[index2].key1_selectivity_flags_draft ^ code1) & KEY1_MASK) == 0) ) {
compact_to_wide( &hash_table[index2], entry );
return;
}
entry->draft = NO_HASH_MOVE;
entry->flags = UPPER_BOUND;
entry->eval = INFINITE_EVAL;
entry->move[0] = 44;
entry->move[1] = 0;
entry->move[2] = 0;
entry->move[3] = 0;
}