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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
|
/**
Copyright (c) Scott Gasch
Module Name:
hash.c
Abstract:
The main transposition table code. The table is a large range of
memory organized into lines (64 bytes). Each line contains four
entries that are 16 bytes each (index 0..3).
After a search, data about the position's score and the depth we
searched to determine this score can be stored in the hash table.
When information is stored a hash replacement scheme is used to
overwrite one of the four entries on a line.
Later, before embarking on a deep recursive search, we can check
the hash table to see if information relevant to this position
has been stored already (and can be reused). If so, we can avoid
repeating the same search and save time.
A hash entry is 16 bytes long and looks like this:
typedef struct _HASH_ENTRY
{
MOVE mv; // 0 1 2 3
UCHAR uDepth; // 4
UCHAR bvFlags; // 5 ==> d i r t | thr up low exact
signed short iValue; // 6 7
UINT64 u64Sig; // 8 9 A B C D E F == 16 bytes
} HASH_ENTRY;
The mv is the best move in a position. Even if we do not have a
deep enough uDepth recorded to cut off, mv is searched first.
uDepth is the depth searched to determine iValue. iValue is
either the exact score or a bound score -- depending on the
value of bvFlag's low order 4 bits. The high order 4 bits
in bvFlags holds "dirty" information to aide the hash replacement
scheme. Finally the u64Sig value is used to absolutely identify
the position to which the hash entry's information refers.
When screwing around with the hash code, turn on PERF_COUNTERS and
watch the hash hit rate (and usable hash hit rate). Also here is
Fine position #70 which is a great sanity check for hash code:
8/k7/3p4/p2P1p2/P2P1P2/8/8/K7/ w - - 0 0 bm 1. Ka1-b1 Ka7-b7 2. Kb1-c1
Author:
Scott Gasch ([email protected]) 12 Jun 2004
Revision History:
$Id: hash.c 345 2007-12-02 22:56:42Z scott $
**/
#include "chess.h"
//
// Hash entries are 16 bytes each and hash lines are 4 entries = 64
// bytes. This is done on purpose so that a hash line fits in a cpu
// cache line. My Athlons have 64 byte cache lines. To adjust this
// for other processors, change the constant below.
//
#define CONVERT_SEARCH_DEPTH_TO_HASH_DEPTH(x) \
ASSERT(IS_VALID_DEPTH(x)); \
(x) >>= 4; \
ASSERT(((x) & 0xffffff00) == 0);
#define HASH_ENTRY_IS_DIRTY(x) \
(((x).bvFlags & 0xF0) != g_cDirty)
#undef ADJUST_MATE_IN_N
#ifdef MP
//
// On MP builds we lock ranges of the hash table in order to mitigate
// against two searcher threads colliding on the same entry at once.
// I did not have a free bit to use in the hash entry so I lock every
// 256th hash table entry at once.
//
#define NUM_HASH_LOCKS 256
volatile static ULONG g_uHashLocks[NUM_HASH_LOCKS];
#define HASH_IS_LOCKED(x) ((g_uHashLocks[(x)]) != 0)
#define LOCK_HASH(x) \
AcquireSpinLock(&(g_uHashLocks[(x)])); \
ASSERT(HASH_IS_LOCKED(x));
#define UNLOCK_HASH(x) \
ASSERT(HASH_IS_LOCKED(x)); \
ReleaseSpinLock(&(g_uHashLocks[(x)]));
#else // no MP
#define HASH_IS_LOCKED(x)
#define LOCK_HASH(x)
#define UNLOCK_HASH(x)
#endif
ULONG g_uHashTableSizeEntries = 0;
ULONG g_uHashTableSizeBytes = 0;
HASH_ENTRY *g_pHashTable = NULL;
UCHAR g_cDirty = 0;
void
DirtyHashTable(void)
/**
Routine description:
The dirty pattern is bitwise ored onto the bvFlags part of a
hash table entry. Since the low nibble is used to hold flags
like upper bound, lower bound, exact score, and threat the
dirty indicator is limited to operating in the upper nibble
of bvFlags (giving it a range of 16 values)
Parameters:
void
Return value:
void
**/
{
ASSERT((g_cDirty & 0x0F) == 0);
g_cDirty += 0x10;
ASSERT((g_cDirty & 0x0F) == 0);
#ifdef TEST
if (g_Options.fShouldPost)
{
Trace("Hash table dirty char: 0x%02x\n", g_cDirty);
}
#endif
}
void
CleanupHashSystem(void)
/**
Routine description:
Cleanup the hash table by freeing the memory and zeroing some
variables. This is called before building an opening book in
order to free some virtual address space up.
Parameters:
void
Return value:
void
**/
{
if (NULL != g_pHashTable)
{
ASSERT(g_uHashTableSizeBytes > 0);
SystemFreeMemory(g_pHashTable);
g_pHashTable = NULL;
g_uHashTableSizeBytes = 0;
g_uHashTableSizeEntries = 0;
}
}
FLAG
InitializeHashSystem(void)
/**
Routine description:
Initialize the hash system... allocate the table and get everything
ready to go.
Parameters:
void
Return value:
FLAG
**/
{
ULONG uBytesToAlloc;
if (!IS_A_POWER_OF_2(g_Options.uNumHashTableEntries))
{
return(FALSE);
}
g_uHashTableSizeEntries = g_Options.uNumHashTableEntries;
if (g_uHashTableSizeEntries != 0)
{
ASSERT(g_pHashTable == NULL);
uBytesToAlloc = (sizeof(HASH_ENTRY) * g_uHashTableSizeEntries);
g_pHashTable = SystemAllocateMemory(uBytesToAlloc);
g_uHashTableSizeBytes = uBytesToAlloc;
ClearHashTable();
#ifdef MP
memset((BYTE *)g_uHashLocks, 0, sizeof(g_uHashLocks));
#endif
}
return(TRUE);
}
void
ClearHashTable (void)
/**
Routine description:
Zero out the entire hash table. This function is called when a new
position is loaded into the root board.
Parameters:
void
Return value:
void
**/
{
if (NULL != g_pHashTable)
{
g_cDirty = 0;
ASSERT(0 != g_uHashTableSizeBytes);
ASSERT(0 != g_uHashTableSizeEntries);
memset(g_pHashTable, 0, g_uHashTableSizeBytes);
}
}
//
// (g_uHashTableSizeEntries) => 1000 0000 ... 0000
// NUM_HASH_ENTRIES_PER_LINE => - 0100
// ------------------------------------------------
// 1111 1111 ... 1100 => Start of a hash line
//
#define KEY_TO_HASH_LINE_START(x) \
((x) & (g_uHashTableSizeEntries - NUM_HASH_ENTRIES_PER_LINE))
static HASH_ENTRY *
_SelectHashLine(UINT64 u64Key)
/**
Routine description:
On an MP system, this routine locks part of the hash table, the
caller should do its thing and then unlock the locked portion of
the hash table by calling _DeselectHashLine(u64Key).
Parameters:
UINT64 u64Key
Return value:
static HASH_ENTRY
**/
{
ULONG uLow = (ULONG)(u64Key >> 32);
ULONG uLine = KEY_TO_HASH_LINE_START(uLow);
#ifdef MP
ULONG uLock = (uLine >> 2) & (NUM_HASH_LOCKS - 1);
LOCK_HASH(uLock);
ASSERT(IS_A_POWER_OF_2(NUM_HASH_LOCKS));
#endif
ASSERT(g_uHashTableSizeBytes != 0);
ASSERT(g_uHashTableSizeEntries != 0);
ASSERT(g_pHashTable != NULL);
ASSERT(IS_A_POWER_OF_2(g_uHashTableSizeEntries));
ASSERT(((g_uHashTableSizeEntries - NUM_HASH_ENTRIES_PER_LINE) &
(NUM_HASH_ENTRIES_PER_LINE - 1)) == 0);
return(&(g_pHashTable[uLine]));
}
#ifdef MP
static void
_DeselectHashLine(UINT64 u64Key)
/**
Routine description:
This releases the lock taken by _SelectHashLine
Parameters:
UINT64 u64Key
Return value:
static void
**/
{
ULONG uLow = (ULONG)(u64Key >> 32);
ULONG uLine = KEY_TO_HASH_LINE_START(uLow);
ULONG uLock = (uLine >> 2) & (NUM_HASH_LOCKS - 1);
ASSERT(IS_A_POWER_OF_2(NUM_HASH_LOCKS));
ASSERT(MP);
UNLOCK_HASH(uLock);
}
#endif
static HASH_ENTRY *
_SelectHashEntry(UINT64 u64Key,
ULONG uDepth,
ULONG uType,
SCORE iValue)
/**
Routine description:
This routine takes a lock. The caller should do its thing and then
release the lock by calling _DeselectHashEntry.
Parameters:
UINT64 u64Key,
ULONG uDepth
Return value:
static HASH_ENTRY
**/
{
HASH_ENTRY *pEntry;
HASH_ENTRY *pStore;
ASSERT(g_uHashTableSizeBytes != 0);
ASSERT(g_uHashTableSizeEntries != 0);
ASSERT(g_pHashTable != NULL);
ASSERT(IS_A_POWER_OF_2(g_uHashTableSizeEntries));
//
// _SelectHashLine takes a lock on MP builds.
//
pEntry = _SelectHashLine(u64Key);
ASSERT(pEntry != NULL);
#ifdef DEBUG
pStore = NULL;
#endif
//
// Note: Hash lines consist of four entries currently:
//
// 1. The first is a depth-priority hash entry (store here if you
// can beat the depth of what's already in there).
//
// 2. The second is an always-store hash entry (store here if you
// can't store in the depth-priority entry).
//
// 3. and 4. The last two entries are where we bump the data from
// the depth- priority table and always-store table when they are
// going to be overwritten by newer data.
//
//
// Replace the 0th entry on the line if:
//
// ...the new entry has greater depth
// ...the current entry is stale (from an old search)
//
if ((pEntry[0].uDepth <= uDepth) ||
HASH_ENTRY_IS_DIRTY(pEntry[0]))
{
//
// Do we need to back up the entry we will overwrite?
//
if ((pEntry[0].u64Sig != u64Key) ||
((pEntry[0].bvFlags & HASH_FLAG_VALID_BOUNDS) != uType))
{
pEntry[2] = pEntry[0];
}
pStore = &(pEntry[0]);
goto end;
}
//
// We could not replace the depth priority 0th entry; instead,
// replace the "always replace" 1st entry on the line unless it
// contains mate information and is not stale.
//
if (((abs(pEntry[1].iValue >= +NMATE)) &&
(abs(iValue) < +NMATE)) &&
(!HASH_ENTRY_IS_DIRTY(pEntry[1])))
{
pStore = &(pEntry[3]);
goto end;
}
if ((pEntry[1].u64Sig != u64Key) ||
((pEntry[1].bvFlags & HASH_FLAG_VALID_BOUNDS) != uType))
{
pEntry[3] = pEntry[1];
}
pStore = &(pEntry[1]);
end:
ASSERT(pStore != NULL);
return(pStore);
}
#ifdef MP
static void
_DeselectHashEntry(UINT64 u64Key)
/**
Routine description:
Releases the lock taken by _SelectHashEntry.
Parameters:
UINT64 u64Key
Return value:
static void
**/
{
_DeselectHashLine(u64Key); // this releases a lock
}
#endif
void
StoreUpperBound(//MOVE mvBest,
POSITION *pos,
SCORE iValue,
ULONG uDepth,
FLAG fThreat)
/**
Routine description:
Store an upper bound in the hash table.
Parameters:
MOVE mvBest : singular move from the node, if any
POSITION *pos,
SCORE iValue,
ULONG uDepth,
FLAG fThreat
Return value:
void
**/
{
UINT64 u64Key;
HASH_ENTRY *pHash;
ASSERT(IS_VALID_SCORE(iValue));
ASSERT(IS_VALID_DEPTH(uDepth));
//
// It does not make sense to store hash positions that are worth
// at best ~INFINITY. Every position is and this is a waste of
// hash space. Ignore these.
//
if (iValue >= +NMATE) return;
if (NULL == g_pHashTable) return;
CONVERT_SEARCH_DEPTH_TO_HASH_DEPTH(uDepth);
u64Key = pos->u64NonPawnSig ^ pos->u64PawnSig;
//
// _SelectHashEntry takes a lock on MP builds
//
pHash = _SelectHashEntry(u64Key, uDepth, HASH_FLAG_UPPER, iValue);
ASSERT(pHash != NULL);
pHash->uDepth = (UCHAR)uDepth;
if (pHash->u64Sig != u64Key)
{
//
// Note: don't overwrite a good hash move with "none" if we
// are replacing a hash entry for the same position. Also
// if we are klobbering data about the same position save
// a memory write to update the (same) sig.
//
pHash->mv.uMove = 0;
pHash->u64Sig = u64Key;
}
ASSERT((g_cDirty & HASH_FLAG_UPPER) == 0);
pHash->bvFlags = g_cDirty | HASH_FLAG_UPPER;
ASSERT((pHash->bvFlags & 0xF0) == g_cDirty);
if (TRUE == fThreat)
{
ASSERT((pHash->bvFlags & HASH_FLAG_THREAT) == 0);
pHash->bvFlags |= HASH_FLAG_THREAT;
}
if (iValue <= -NMATE)
{
//
// If this is an upper bound near -INFINITY
// (i.e. ~-INFINITY at best) convert it into -NMATE at
// best (at best mate in N).
//
pHash->iValue = -NMATE;
}
else
{
//
// Otherwise hash the exact value.
//
ASSERT(iValue > -NMATE);
ASSERT(iValue < +NMATE);
pHash->iValue = (signed short)iValue;
}
#ifdef MP
//
// Release the lock on MP builds
//
_DeselectHashEntry(u64Key);
#endif
}
void
StoreExactScore(MOVE mvBestMove,
POSITION *pos,
SCORE iValue,
ULONG uDepth,
FLAG fThreat,
ULONG uPly)
/**
Routine description:
Store a best move and its exact score in the hash table.
Parameters:
MOVE mvBestMove,
POSITION *pos,
SCORE iValue,
ULONG uDepth,
FLAG fThreat,
ULONG uPly
Return value:
void
**/
{
HASH_ENTRY *pHash;
UINT64 u64Key;
ASSERT(IS_VALID_SCORE(iValue));
ASSERT(IS_VALID_DEPTH(uDepth));
if (NULL == g_pHashTable) return;
CONVERT_SEARCH_DEPTH_TO_HASH_DEPTH(uDepth);
u64Key = pos->u64NonPawnSig ^ pos->u64PawnSig;
//
// _SelectHashEntry takes a lock on MP builds.
//
pHash = _SelectHashEntry(u64Key, uDepth, HASH_FLAG_EXACT, iValue);
ASSERT(pHash != NULL);
//
// Populate the hash entry
//
pHash->uDepth = (UCHAR)uDepth;
ASSERT(mvBestMove.uMove);
pHash->mv.uMove = mvBestMove.uMove;
ASSERT((g_cDirty & HASH_FLAG_EXACT) == 0);
pHash->bvFlags = g_cDirty | HASH_FLAG_EXACT;
ASSERT((pHash->bvFlags & 0xF0) == g_cDirty);
if (TRUE == fThreat)
{
ASSERT((pHash->bvFlags & HASH_FLAG_THREAT) == 0);
pHash->bvFlags |= HASH_FLAG_THREAT;
}
pHash->u64Sig = u64Key;
if (iValue >= +NMATE)
{
#ifdef ADJUST_MATE_IN_N
ASSERT((iValue + (int)uPly) < +INFINITY);
ASSERT((iValue + (int)uPly) > -INFINITY);
pHash->iValue = (signed short)(iValue + (int)uPly);
#else
pHash->bvFlags = g_cDirty | HASH_FLAG_LOWER;
ASSERT((pHash->bvFlags & 0xF0) == g_cDirty);
pHash->iValue = +NMATE;
#endif
}
else if (iValue <= -NMATE)
{
#ifdef ADJUST_MATE_IN_N
ASSERT((iValue + (int)uPly) < +INFINITY);
ASSERT((iValue - (int)uPly) > -INFINITY);
pHash->iValue = (signed short)(iValue - (int)uPly);
#else
pHash->bvFlags = g_cDirty | HASH_FLAG_UPPER;
ASSERT((pHash->bvFlags & 0xF0) == g_cDirty);
pHash->iValue = +NMATE;
#endif
}
else
{
ASSERT(iValue > -NMATE);
ASSERT(iValue < +NMATE);
pHash->iValue = (short signed)iValue;
}
#ifdef MP
//
// Release the lock on MP builds
//
_DeselectHashEntry(u64Key); // Release the lock
#endif
}
void
StoreLowerBound(MOVE mvBestMove,
POSITION *pos,
SCORE iValue,
ULONG uDepth,
FLAG fThreat)
/**
Routine description:
Store a lower bound score and move in the hash table.
Parameters:
MOVE mvBestMove,
POSITION *pos,
SCORE iValue,
ULONG uDepth,
FLAG fThreat
Return value:
void
**/
{
HASH_ENTRY *pHash;
UINT64 u64Key;
ASSERT(IS_VALID_SCORE(iValue));
ASSERT(IS_VALID_DEPTH(uDepth));
//
// Do not bother to hash this node if it is telling us that the
// value of this position is worth at least ~-INFINITY. This is
// useless information and a waste of hash space.
//
if (iValue <= -NMATE) return;
if (NULL == g_pHashTable) return;
CONVERT_SEARCH_DEPTH_TO_HASH_DEPTH(uDepth);
u64Key = pos->u64NonPawnSig ^ pos->u64PawnSig;
//
// _SelectHashEntry takes a lock on MP builds
//
pHash = _SelectHashEntry(u64Key, uDepth, HASH_FLAG_LOWER, iValue);
ASSERT(pHash != NULL);
//
// Populate the entry
//
pHash->uDepth = (UCHAR)uDepth;
if ((mvBestMove.uMove) || (pHash->u64Sig != u64Key))
{
//
// Note: only overwrite the existing hash move if either we
// have a move (i.e. not null move) or the entry we are
// overwriting is for a different position.
//
pHash->mv.uMove = mvBestMove.uMove;
}
pHash->u64Sig = u64Key;
ASSERT((g_cDirty & HASH_FLAG_LOWER) == 0);
pHash->bvFlags = g_cDirty | HASH_FLAG_LOWER;
ASSERT((pHash->bvFlags & 0xF0) == g_cDirty);
pHash->bvFlags |= (HASH_FLAG_THREAT * (fThreat == TRUE));
if (iValue < +NMATE)
{
ASSERT(iValue < +NMATE);
ASSERT(iValue > -NMATE);
pHash->iValue = (signed short)iValue;
}
else
{
//
// If the value is greater than mate in N don't store how many
// moves N is because this depends on where in the search we
// have found this position. Just say that this position is
// worth at least mate in N.
//
pHash->iValue = +NMATE;
}
#ifdef MP
//
// Release the lock on MP builds
//
_DeselectHashEntry(u64Key);
#endif
}
MOVE
GetPonderMove(POSITION *pos)
/**
Routine description:
Get a move to ponder.
Parameters:
POSITION *pos
Return value:
MOVE
**/
{
HASH_ENTRY *pHash;
ULONG x;
UINT64 u64Key = pos->u64NonPawnSig ^ pos->u64PawnSig;
MOVE mv;
mv.uMove = 0;
if (NULL == g_pHashTable) return(mv);
//
// _SelectHashLine takes a lock on MP builds
//
pHash = _SelectHashLine(u64Key);
for (x = 0;
x < NUM_HASH_ENTRIES_PER_LINE;
x++)
{
if (u64Key == pHash->u64Sig)
{
mv = pHash->mv;
if ((pHash->bvFlags & HASH_FLAG_VALID_BOUNDS) ==
HASH_FLAG_EXACT)
{
goto end;
}
}
}
end:
#ifdef MP
//
// Release the lock.
//
_DeselectHashLine(u64Key);
#endif
return(mv);
}
HASH_ENTRY *
HashLookup(SEARCHER_THREAD_CONTEXT *ctx,
ULONG uDepth,
ULONG uNextDepth,
SCORE iAlpha,
SCORE iBeta,
FLAG *pfThreat,
FLAG *pfAvoidNull,
MOVE *pHashMove,
SCORE *piScore)
/**
Routine description:
Lookup any information we have about the current position in the
hash table. Cutoff if we can.
Parameters:
SEARCHER_THREAD_CONTEXT *ctx,
ULONG uDepth,
ULONG uNextDepth,
SCORE iAlpha,
SCORE iBeta,
FLAG *pfThreat,
FLAG *pfAvoidNull,
MOVE *pHashMove,
SCORE *piScore
Return value:
HASH_ENTRY
**/
{
HASH_ENTRY *pHash;
ULONG x;
POSITION *pos = &(ctx->sPosition);
UINT64 u64Key = pos->u64NonPawnSig ^ pos->u64PawnSig;
ULONG uMoveScore = 0;
ULONG uThisScore;
#define COMPUTE_MOVE_SCORE(mv, depth) \
((depth) * 8 + PIECE_VALUE_OVER_100((mv).pCaptured) + 1)
ASSERT(IS_VALID_DEPTH(uDepth));
ASSERT(IS_VALID_DEPTH(uNextDepth));
ASSERT(uNextDepth <= uDepth);
ASSERT(IS_VALID_SCORE(iAlpha));
ASSERT(IS_VALID_SCORE(iBeta));
ASSERT(iAlpha < iBeta);
ASSERT(pHashMove->uMove == 0);
ASSERT(NULL != g_pHashTable);
INC(ctx->sCounters.hash.u64Probes);
//
// These are out parameters for the caller, initialize them
// saying we don't know anything.
//
*pfAvoidNull = FALSE;
*pfThreat = FALSE;
CONVERT_SEARCH_DEPTH_TO_HASH_DEPTH(uDepth);
CONVERT_SEARCH_DEPTH_TO_HASH_DEPTH(uNextDepth);
//
// _SelectHashLine takes a lock on MP builds.
//
pHash = _SelectHashLine(u64Key);
for (x = 0;
x < NUM_HASH_ENTRIES_PER_LINE;
x++)
{
if (u64Key == pHash->u64Sig)
{
//
// This entry hit, make it "clean" so it is harder to replace
// later on.
//
pHash->bvFlags &= 0x0F;
ASSERT((g_cDirty & 0x0F) == 0);
pHash->bvFlags |= g_cDirty;
ASSERT((pHash->bvFlags & 0xF0) == g_cDirty);
//
// Parse the matching entry
//
*pfThreat |= ((pHash->bvFlags & HASH_FLAG_THREAT) > 0);
//
// If we have an exact match and sufficient depth to
// satisfy the depth to which we will later search after a
// nullmove, see if the known value of this node is less
// than beta. If so, doing nothing in this position will
// probably not be better than doing something therefore
// its not wise to perform the nullmove search. It's just
// a waste of nodes and time.
//
if ((pHash->uDepth >= uNextDepth) &&
((pHash->bvFlags & HASH_FLAG_UPPER) ||
(pHash->bvFlags & HASH_FLAG_EXACT)) &&
(pHash->iValue < iBeta))
{
*pfAvoidNull = TRUE;
}
// Because we can encounter several moves while looking for
// a hash cutoff, have a semantic about which one we return
// to the search.
INC(ctx->sCounters.hash.u64OverallHits);
if (pHash->mv.uMove) {
uThisScore = COMPUTE_MOVE_SCORE(pHash->mv, uDepth);
ASSERT(uThisScore != 0);
if (uThisScore > uMoveScore) {
uMoveScore = uThisScore;
*pHashMove = pHash->mv;
ASSERT(SanityCheckMove(pos, pHash->mv));
}
}
//
// We have a hit, but is it useful?
//
if (pHash->uDepth >= uDepth)
{
switch (pHash->bvFlags & HASH_FLAG_VALID_BOUNDS)
{
case HASH_FLAG_EXACT:
*piScore = pHash->iValue;
INC(ctx->sCounters.hash.u64UsefulHits);
INC(ctx->sCounters.hash.u64ExactScoreHits);
#ifdef ADJUST_MATE_IN_N
//
// Readjust mate in N scores.
//
if (pHash->iValue >= +NMATE)
{
*piScore -= (int)uPly;
}
else if (pHash->iValue <= -NMATE)
{
*piScore += (int)uPly;
}
#endif
ASSERT(IS_VALID_SCORE(*piScore));
goto end;
break;
case HASH_FLAG_LOWER:
if (pHash->iValue >= iBeta)
{
INC(ctx->sCounters.hash.u64UsefulHits);
INC(ctx->sCounters.hash.u64LowerBoundHits);
*piScore = pHash->iValue;
ASSERT(IS_VALID_SCORE(*piScore));
goto end;
}
break;
case HASH_FLAG_UPPER:
if (pHash->iValue <= iAlpha)
{
INC(ctx->sCounters.hash.u64UsefulHits);
INC(ctx->sCounters.hash.u64UpperBoundHits);
*piScore = pHash->iValue;
ASSERT(IS_VALID_SCORE(*piScore));
goto end;
}
break;
#ifdef DEBUG
default:
ASSERT(FALSE);
break;
#endif
}
}
}
pHash++;
}
pHash = NULL;
end:
#ifdef MP
//
// Release the lock.
//
_DeselectHashLine(u64Key);
#endif
return(pHash);
}
|