forked from hoshir/zebra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
end.c
2479 lines (2099 loc) · 66.7 KB
/
end.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
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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
File: end.c
Created: 1994
Modified: December 19, 2005
Authors: Gunnar Andersson ([email protected])
Contents: The fast endgame solver.
*/
#include "porting.h"
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "autoplay.h"
#include "bitbcnt.h"
#include "bitbmob.h"
#include "bitboard.h"
#include "bitbtest.h"
#include "cntflip.h"
#include "counter.h"
#include "display.h"
#include "doflip.h"
#include "end.h"
#include "epcstat.h"
#include "eval.h"
#include "getcoeff.h"
#include "globals.h"
#include "hash.h"
#include "macros.h"
#include "midgame.h"
#include "moves.h"
#include "osfbook.h"
#include "probcut.h"
#include "search.h"
#include "stable.h"
#include "texts.h"
#include "timer.h"
#include "unflip.h"
#define USE_MPC 1
#define MAX_SELECTIVITY 9
#define DISABLE_SELECTIVITY 18
#define PV_EXPANSION 16
#define DEPTH_TWO_SEARCH 15
#define DEPTH_THREE_SEARCH 20
#define DEPTH_FOUR_SEARCH 24
#define DEPTH_SIX_SEARCH 30
#define EXTRA_ROOT_SEARCH 2
#ifdef _WIN32_WCE
#define EVENT_CHECK_INTERVAL 25000.0
#else
#define EVENT_CHECK_INTERVAL 250000.0
#endif
#define LOW_LEVEL_DEPTH 8
#define FASTEST_FIRST_DEPTH 12
#define HASH_DEPTH (LOW_LEVEL_DEPTH + 1)
#define VERY_HIGH_EVAL 1000000
#define GOOD_TRANSPOSITION_EVAL 10000000
/* Parameters for the fastest-first algorithm. The performance does
not seem to depend a lot on the precise values. */
#define FAST_FIRST_FACTOR 0.45
#define MOB_FACTOR 460
/* The disc difference when special wipeout move ordering is tried.
This means more aggressive use of fastest first. */
#define WIPEOUT_THRESHOLD 60
/* Use stability pruning? */
#define USE_STABILITY TRUE
#if 0
// Profiling code
static long long int
rdtsc( void ) {
#if defined(__GNUC__)
long long a;
asm volatile("rdtsc":"=A" (a));
return a;
#else
return 0;
#endif
}
#endif
typedef enum {
NOTHING,
SELECTIVE_SCORE,
WLD_SCORE,
EXACT_SCORE
} SearchStatus;
MoveLink end_move_list[100];
/* The parities of the regions are in the region_parity bit vector. */
static unsigned int region_parity;
/* Pseudo-probabilities corresponding to the percentiles.
These are taken from the normal distribution; to the percentile
x corresponds the probability Pr(-x <= Y <= x) where Y is a N(0,1)
variable. */
static const double confidence[MAX_SELECTIVITY + 1] =
{ 1.000, 0.99, 0.98, 0.954, 0.911, 0.838, 0.729, 0.576, 0.383, 0.197 };
/* Percentiles used in the endgame MPC */
static const double end_percentile[MAX_SELECTIVITY + 1] =
{ 100.0, 4.0, 3.0, 2.0, 1.7, 1.4, 1.1, 0.8, 0.5, 0.25 };
#if USE_STABILITY
#define HIGH_STABILITY_THRESHOLD 24
static const int stability_threshold[] = { 65, 65, 65, 65, 65, 46, 38, 30, 24,
24, 24, 24, 0, 0, 0, 0, 0, 0, 0 };
#endif
static double fast_first_mean[61][64];
static double fast_first_sigma[61][64];
static int best_move, best_end_root_move;
static int true_found, true_val;
static int full_output_mode;
static int earliest_wld_solve, earliest_full_solve;
static int fast_first_threshold[61][64];
static int ff_mob_factor[61];
static BitBoard neighborhood_mask[100];
const unsigned int quadrant_mask[100] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 2, 2, 2, 2, 0,
0, 1, 1, 1, 1, 2, 2, 2, 2, 0,
0, 1, 1, 1, 1, 2, 2, 2, 2, 0,
0, 1, 1, 1, 1, 2, 2, 2, 2, 0,
0, 4, 4, 4, 4, 8, 8, 8, 8, 0,
0, 4, 4, 4, 4, 8, 8, 8, 8, 0,
0, 4, 4, 4, 4, 8, 8, 8, 8, 0,
0, 4, 4, 4, 4, 8, 8, 8, 8, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
/* Number of discs that the side to move at the root has to win with. */
static int komi_shift;
#if 1
/*
TESTFLIPS_WRAPPER
Checks if SQ is a valid move by
(1) verifying that there exists a neighboring opponent disc,
(2) verifying that the move flips some disc.
*/
INLINE static int
TestFlips_wrapper( int sq,
BitBoard my_bits,
BitBoard opp_bits ) {
int flipped;
if ( ((neighborhood_mask[sq].high & opp_bits.high) |
(neighborhood_mask[sq].low & opp_bits.low)) != 0 )
flipped = TestFlips_bitboard[sq - 11]( my_bits.high, my_bits.low, opp_bits.high, opp_bits.low );
else
flipped = 0;
return flipped;
}
#else
#define TestFlips_wrapper( sq, my_bits, opp_bits ) \
TestFlips_bitboard[sq - 11]( my_bits.high, my_bits.low, opp_bits.high, opp_bits.low )
#endif
/*
PREPARE_TO_SOLVE
Create the list of empty squares.
*/
static void
prepare_to_solve( const int *board ) {
/* fixed square ordering: */
/* jcw's order, which is the best of 4 tried (according to Warren Smith) */
static const unsigned char worst2best[64] = {
/*B2*/ 22 , 27 , 72 , 77 ,
/*B1*/ 12 , 17 , 21 , 28 , 71 , 78 , 82, 87 ,
/*C2*/ 23 , 26 , 32 , 37 , 62 , 67 , 73 , 76 ,
/*D2*/ 24 , 25 , 42 , 47 , 52 , 57 , 74 , 75 ,
/*D3*/ 34 , 35 , 43 , 46 , 53 , 56 , 64 , 65 ,
/*C1*/ 13 , 16 , 31 , 38 , 61 , 68 , 83 , 86 ,
/*D1*/ 14 , 15 , 41 , 48 , 51 , 58 , 84 , 85 ,
/*C3*/ 33 , 36 , 63 , 66 ,
/*A1*/ 11 , 18 , 81 , 88 ,
/*D4*/ 44 , 45 , 54 , 45
};
int i;
int last_sq;
region_parity = 0;
last_sq = END_MOVE_LIST_HEAD;
for ( i = 59; i >=0; i-- ) {
int sq = worst2best[i];
if ( board[sq] == EMPTY ) {
end_move_list[last_sq].succ = sq;
end_move_list[sq].pred = last_sq;
region_parity ^= quadrant_mask[sq];
last_sq = sq;
}
}
end_move_list[last_sq].succ = END_MOVE_LIST_TAIL;
}
#if 0
/*
CHECK_LIST
Performs a minimal sanity check of the move list: That it contains
the same number of moves as there are empty squares on the board.
*/
static void
check_list( int empties ) {
int links = 0;
int sq = end_move_list[END_MOVE_LIST_HEAD].succ;
while ( sq != END_MOVE_LIST_TAIL ) {
links++;
sq = end_move_list[sq].succ;
}
if ( links != empties )
printf( "%d links, %d empties\n", links, empties );
}
#endif
/*
SOLVE_TWO_EMPTY
SOLVE_THREE_EMPTY
SOLVE_FOUR_EMPTY
SOLVE_PARITY
SOLVE_PARITY_HASH
SOLVE_PARITY_HASH_HIGH
These are the core routines of the low level endgame code.
They all perform the same task: Return the score for the side to move.
Structural differences:
* SOLVE_TWO_EMPTY may only be called for *exactly* two empty
* SOLVE_THREE_EMPTY may only be called for *exactly* three empty
* SOLVE_FOUR_EMPTY may only be called for *exactly* four empty
* SOLVE_PARITY uses stability, parity and fixed move ordering
* SOLVE_PARITY_HASH uses stability, hash table and fixed move ordering
* SOLVE_PARITY_HASH_HIGH uses stability, hash table and (non-thresholded)
fastest first
*/
static int
solve_two_empty( BitBoard my_bits,
BitBoard opp_bits,
int sq1,
int sq2,
int alpha,
int beta,
int disc_diff,
int pass_legal ) {
// BitBoard new_opp_bits;
int score = -INFINITE_EVAL;
int flipped;
int ev;
INCREMENT_COUNTER( nodes );
/* Overall strategy: Lazy evaluation whenever possible, i.e., don't
update bitboards until they are used. Also look at alpha and beta
in order to perform strength reduction: Feasibility testing is
faster than counting number of flips. */
/* Try the first of the two empty squares... */
flipped = TestFlips_wrapper( sq1, my_bits, opp_bits );
if ( flipped != 0 ) { /* SQ1 feasible for me */
INCREMENT_COUNTER( nodes );
ev = disc_diff + 2 * flipped;
#if 0
FULL_ANDNOT( new_opp_bits, opp_bits, bb_flips );
if ( ev - 2 <= alpha ) { /* Fail-low if he can play SQ2 */
if ( ValidOneEmpty_bitboard[sq2]( new_opp_bits ) != 0 )
ev = alpha;
else { /* He passes, check if SQ2 is feasible for me */
if ( ev >= 0 ) { /* I'm ahead, so EV will increase by at least 2 */
ev += 2;
if ( ev < beta ) /* Only bother if not certain fail-high */
ev += 2 * CountFlips_bitboard[sq2 - 11]( bb_flips.high, bb_flips.low );
}
else {
if ( ev < beta ) { /* Only bother if not fail-high already */
flipped = CountFlips_bitboard[sq2 - 11]( bb_flips.high, bb_flips.low );
if ( flipped != 0 ) /* SQ2 feasible for me, game over */
ev += 2 * (flipped + 1);
/* ELSE: SQ2 will end up empty, game over */
}
}
}
}
else {
#endif
flipped = CountFlips_bitboard[sq2 - 11]( opp_bits.high & ~bb_flips.high, opp_bits.low & ~bb_flips.low );
if ( flipped != 0 )
ev -= 2 * flipped;
else { /* He passes, check if SQ2 is feasible for me */
if ( ev >= 0 ) { /* I'm ahead, so EV will increase by at least 2 */
ev += 2;
if ( ev < beta ) /* Only bother if not certain fail-high */
ev += 2 * CountFlips_bitboard[sq2 - 11]( bb_flips.high, bb_flips.low );
}
else {
if ( ev < beta ) { /* Only bother if not fail-high already */
flipped = CountFlips_bitboard[sq2 - 11]( bb_flips.high, bb_flips.low );
if ( flipped != 0 ) /* SQ2 feasible for me, game over */
ev += 2 * (flipped + 1);
/* ELSE: SQ2 will end up empty, game over */
}
}
}
#if 0
}
#endif
/* Being legal, the first move is the best so far */
score = ev;
if ( score > alpha ) {
if ( score >= beta )
return score;
alpha = score;
}
}
/* ...and then the second */
flipped = TestFlips_wrapper( sq2, my_bits, opp_bits );
if ( flipped != 0 ) { /* SQ2 feasible for me */
INCREMENT_COUNTER( nodes );
ev = disc_diff + 2 * flipped;
#if 0
FULL_ANDNOT( new_opp_bits, opp_bits, bb_flips );
if ( ev - 2 <= alpha ) { /* Fail-low if he can play SQ1 */
if ( ValidOneEmpty_bitboard[sq1]( new_opp_bits ) != 0 )
ev = alpha;
else { /* He passes, check if SQ1 is feasible for me */
if ( ev >= 0 ) { /* I'm ahead, so EV will increase by at least 2 */
ev += 2;
if ( ev < beta ) /* Only bother if not certain fail-high */
ev += 2 * CountFlips_bitboard[sq1 - 11]( bb_flips.high, bb_flips.low );
}
else {
if ( ev < beta ) { /* Only bother if not fail-high already */
flipped = CountFlips_bitboard[sq1 - 11]( bb_flips.high, bb_flips.low );
if ( flipped != 0 ) /* SQ1 feasible for me, game over */
ev += 2 * (flipped + 1);
/* ELSE: SQ1 will end up empty, game over */
}
}
}
}
else {
#endif
flipped = CountFlips_bitboard[sq1 - 11]( opp_bits.high & ~bb_flips.high, opp_bits.low & ~bb_flips.low );
if ( flipped != 0 ) /* SQ1 feasible for him, game over */
ev -= 2 * flipped;
else { /* He passes, check if SQ1 is feasible for me */
if ( ev >= 0 ) { /* I'm ahead, so EV will increase by at least 2 */
ev += 2;
if ( ev < beta ) /* Only bother if not certain fail-high */
ev += 2 * CountFlips_bitboard[sq1 - 11]( bb_flips.high, bb_flips.low );
}
else {
if ( ev < beta ) { /* Only bother if not fail-high already */
flipped = CountFlips_bitboard[sq1 - 11]( bb_flips.high, bb_flips.low );
if ( flipped != 0 ) /* SQ1 feasible for me, game over */
ev += 2 * (flipped + 1);
/* ELSE: SQ1 will end up empty, game over */
}
}
}
#if 0
}
#endif
/* If the second move is better than the first (if that move was legal),
its score is the score of the position */
if ( ev >= score )
return ev;
}
/* If both SQ1 and SQ2 are illegal I have to pass,
otherwise return the best score. */
if ( score == -INFINITE_EVAL ) {
if ( !pass_legal ) { /* Two empty squares */
if ( disc_diff > 0 )
return disc_diff + 2;
if ( disc_diff < 0 )
return disc_diff - 2;
return 0;
}
else
return -solve_two_empty( opp_bits, my_bits, sq1, sq2, -beta,
-alpha, -disc_diff, FALSE );
}
else
return score;
}
static int
solve_three_empty( BitBoard my_bits,
BitBoard opp_bits,
int sq1,
int sq2,
int sq3,
int alpha,
int beta,
int disc_diff,
int pass_legal ) {
BitBoard new_opp_bits;
int score = -INFINITE_EVAL;
int flipped;
int new_disc_diff;
int ev;
INCREMENT_COUNTER( nodes );
flipped = TestFlips_wrapper( sq1, my_bits, opp_bits );
if ( flipped != 0 ) {
FULL_ANDNOT( new_opp_bits, opp_bits, bb_flips );
new_disc_diff = -disc_diff - 2 * flipped - 1;
score = -solve_two_empty( new_opp_bits, bb_flips, sq2, sq3,
-beta, -alpha, new_disc_diff, TRUE );
if ( score >= beta )
return score;
else if ( score > alpha )
alpha = score;
}
flipped = TestFlips_wrapper( sq2, my_bits, opp_bits );
if ( flipped != 0 ) {
FULL_ANDNOT( new_opp_bits, opp_bits, bb_flips );
new_disc_diff = -disc_diff - 2 * flipped - 1;
ev = -solve_two_empty( new_opp_bits, bb_flips, sq1, sq3,
-beta, -alpha, new_disc_diff, TRUE );
if ( ev >= beta )
return ev;
else if ( ev > score ) {
score = ev;
if ( score > alpha )
alpha = score;
}
}
flipped = TestFlips_wrapper( sq3, my_bits, opp_bits );
if ( flipped != 0 ) {
FULL_ANDNOT( new_opp_bits, opp_bits, bb_flips );
new_disc_diff = -disc_diff - 2 * flipped - 1;
ev = -solve_two_empty( new_opp_bits, bb_flips, sq1, sq2,
-beta, -alpha, new_disc_diff, TRUE );
if ( ev >= score )
return ev;
}
if ( score == -INFINITE_EVAL ) {
if ( !pass_legal ) { /* Three empty squares */
if ( disc_diff > 0 )
return disc_diff + 3;
if ( disc_diff < 0 )
return disc_diff - 3;
return 0; /* Can't reach this code, only keep it for symmetry */
}
else
return -solve_three_empty( opp_bits, my_bits, sq1, sq2, sq3,
-beta, -alpha, -disc_diff, FALSE );
}
return score;
}
static int
solve_four_empty( BitBoard my_bits,
BitBoard opp_bits,
int sq1,
int sq2,
int sq3,
int sq4,
int alpha,
int beta,
int disc_diff,
int pass_legal ) {
BitBoard new_opp_bits;
int score = -INFINITE_EVAL;
int flipped;
int new_disc_diff;
int ev;
INCREMENT_COUNTER( nodes );
flipped = TestFlips_wrapper( sq1, my_bits, opp_bits );
if ( flipped != 0 ) {
FULL_ANDNOT( new_opp_bits, opp_bits, bb_flips );
new_disc_diff = -disc_diff - 2 * flipped - 1;
score = -solve_three_empty( new_opp_bits, bb_flips, sq2, sq3, sq4,
-beta, -alpha, new_disc_diff, TRUE );
if ( score >= beta )
return score;
else if ( score > alpha )
alpha = score;
}
flipped = TestFlips_wrapper( sq2, my_bits, opp_bits );
if ( flipped != 0 ) {
FULL_ANDNOT( new_opp_bits, opp_bits, bb_flips );
new_disc_diff = -disc_diff - 2 * flipped - 1;
ev = -solve_three_empty( new_opp_bits, bb_flips, sq1, sq3, sq4,
-beta, -alpha, new_disc_diff, TRUE );
if ( ev >= beta )
return ev;
else if ( ev > score ) {
score = ev;
if ( score > alpha )
alpha = score;
}
}
flipped = TestFlips_wrapper( sq3, my_bits, opp_bits );
if ( flipped != 0 ) {
FULL_ANDNOT( new_opp_bits, opp_bits, bb_flips );
new_disc_diff = -disc_diff - 2 * flipped - 1;
ev = -solve_three_empty( new_opp_bits, bb_flips, sq1, sq2, sq4,
-beta, -alpha, new_disc_diff, TRUE );
if ( ev >= beta )
return ev;
else if ( ev > score ) {
score = ev;
if ( score > alpha )
alpha = score;
}
}
flipped = TestFlips_wrapper( sq4, my_bits, opp_bits );
if ( flipped != 0 ) {
FULL_ANDNOT( new_opp_bits, opp_bits, bb_flips );
new_disc_diff = -disc_diff - 2 * flipped - 1;
ev = -solve_three_empty( new_opp_bits, bb_flips, sq1, sq2, sq3,
-beta, -alpha, new_disc_diff, TRUE );
if ( ev >= score )
return ev;
}
if ( score == -INFINITE_EVAL ) {
if ( !pass_legal ) { /* Four empty squares */
if ( disc_diff > 0 )
return disc_diff + 4;
if ( disc_diff < 0 )
return disc_diff - 4;
return 0;
}
else
return -solve_four_empty( opp_bits, my_bits, sq1, sq2, sq3, sq4,
-beta, -alpha, -disc_diff, FALSE );
}
return score;
}
static int
solve_parity( BitBoard my_bits,
BitBoard opp_bits,
int alpha,
int beta,
int color,
int empties,
int disc_diff,
int pass_legal ) {
BitBoard new_opp_bits;
int score = -INFINITE_EVAL;
int oppcol = OPP( color );
int ev;
int flipped;
int new_disc_diff;
int sq, old_sq, best_sq = 0;
unsigned int parity_mask;
INCREMENT_COUNTER( nodes );
/* Check for stability cutoff */
#if USE_STABILITY
if ( alpha >= stability_threshold[empties] ) {
int stability_bound;
stability_bound = 64 - 2 * count_edge_stable( oppcol, opp_bits, my_bits );
if ( stability_bound <= alpha )
return alpha;
stability_bound = 64 - 2 * count_stable( oppcol, opp_bits, my_bits );
if ( stability_bound < beta )
beta = stability_bound + 1;
if ( stability_bound <= alpha )
return alpha;
}
#endif
/* Odd parity */
parity_mask = region_parity;
if ( region_parity != 0 ) /* Is there any region with odd parity? */
for ( old_sq = END_MOVE_LIST_HEAD, sq = end_move_list[old_sq].succ;
sq != END_MOVE_LIST_TAIL;
old_sq = sq, sq = end_move_list[sq].succ ) {
unsigned int holepar = quadrant_mask[sq];
if ( holepar & parity_mask ) {
flipped = TestFlips_wrapper( sq, my_bits, opp_bits );
if ( flipped != 0 ) {
FULL_ANDNOT( new_opp_bits, opp_bits, bb_flips );
end_move_list[old_sq].succ = end_move_list[sq].succ;
new_disc_diff = -disc_diff - 2 * flipped - 1;
if ( empties == 5 ) {
int sq1 = end_move_list[END_MOVE_LIST_HEAD].succ;
int sq2 = end_move_list[sq1].succ;
int sq3 = end_move_list[sq2].succ;
int sq4 = end_move_list[sq3].succ;
ev = -solve_four_empty( new_opp_bits, bb_flips, sq1, sq2, sq3, sq4,
-beta, -alpha, new_disc_diff, TRUE );
}
else {
region_parity ^= holepar;
ev = -solve_parity( new_opp_bits, bb_flips, -beta, -alpha,
oppcol, empties - 1, new_disc_diff, TRUE );
region_parity ^= holepar;
}
end_move_list[old_sq].succ = sq;
if ( ev > score ) {
if ( ev > alpha ) {
if ( ev >= beta ) {
best_move = sq;
return ev;
}
alpha = ev;
}
score = ev;
best_sq = sq;
}
}
}
}
/* Even parity */
parity_mask = ~parity_mask;
for ( old_sq = END_MOVE_LIST_HEAD, sq = end_move_list[old_sq].succ;
sq != END_MOVE_LIST_TAIL;
old_sq = sq, sq = end_move_list[sq].succ ) {
unsigned int holepar = quadrant_mask[sq];
if ( holepar & parity_mask ) {
flipped = TestFlips_wrapper( sq, my_bits, opp_bits );
if ( flipped != 0 ) {
FULL_ANDNOT( new_opp_bits, opp_bits, bb_flips );
end_move_list[old_sq].succ = end_move_list[sq].succ;
new_disc_diff = -disc_diff - 2 * flipped - 1;
if ( empties == 5 ) {
int sq1 = end_move_list[END_MOVE_LIST_HEAD].succ;
int sq2 = end_move_list[sq1].succ;
int sq3 = end_move_list[sq2].succ;
int sq4 = end_move_list[sq3].succ;
ev = -solve_four_empty( new_opp_bits, bb_flips, sq1, sq2, sq3, sq4,
-beta, -alpha, new_disc_diff, TRUE );
}
else {
region_parity ^= holepar;
ev = -solve_parity( new_opp_bits, bb_flips, -beta, -alpha,
oppcol, empties - 1, new_disc_diff, TRUE );
region_parity ^= holepar;
}
end_move_list[old_sq].succ = sq;
if ( ev > score ) {
if ( ev > alpha ) {
if ( ev >= beta ) {
best_move = sq;
return ev;
}
alpha = ev;
}
score = ev;
best_sq = sq;
}
}
}
}
if ( score == -INFINITE_EVAL ) {
if ( !pass_legal ) {
if ( disc_diff > 0 )
return disc_diff + empties;
if ( disc_diff < 0 )
return disc_diff - empties;
return 0;
}
else
return -solve_parity( opp_bits, my_bits, -beta, -alpha, oppcol,
empties, -disc_diff, FALSE );
}
best_move = best_sq;
return score;
}
static int
solve_parity_hash( BitBoard my_bits,
BitBoard opp_bits,
int alpha,
int beta,
int color,
int empties,
int disc_diff,
int pass_legal ) {
BitBoard new_opp_bits;
int score = -INFINITE_EVAL;
int oppcol = OPP( color );
int in_alpha = alpha;
int ev;
int flipped;
int new_disc_diff;
int sq, old_sq, best_sq = 0;
unsigned int parity_mask;
HashEntry entry;
INCREMENT_COUNTER( nodes );
find_hash( &entry, ENDGAME_MODE );
if ( (entry.draft == empties) &&
(entry.selectivity == 0) &&
valid_move( entry.move[0], color ) &&
(entry.flags & ENDGAME_SCORE) &&
((entry.flags & EXACT_VALUE) ||
((entry.flags & LOWER_BOUND) && entry.eval >= beta) ||
((entry.flags & UPPER_BOUND) && entry.eval <= alpha)) ) {
best_move = entry.move[0];
return entry.eval;
}
/* Check for stability cutoff */
#if USE_STABILITY
if ( alpha >= stability_threshold[empties] ) {
int stability_bound;
stability_bound = 64 - 2 * count_edge_stable( oppcol, opp_bits, my_bits );
if ( stability_bound <= alpha )
return alpha;
stability_bound = 64 - 2 * count_stable( oppcol, opp_bits, my_bits );
if ( stability_bound < beta )
beta = stability_bound + 1;
if ( stability_bound <= alpha )
return alpha;
}
#endif
/* Odd parity. */
parity_mask = region_parity;
if ( region_parity != 0 ) /* Is there any region with odd parity? */
for ( old_sq = END_MOVE_LIST_HEAD, sq = end_move_list[old_sq].succ;
sq != END_MOVE_LIST_TAIL;
old_sq = sq, sq = end_move_list[sq].succ ) {
unsigned int holepar = quadrant_mask[sq];
if ( holepar & parity_mask ) {
flipped = TestFlips_wrapper( sq, my_bits, opp_bits );
if ( flipped != 0 ) {
FULL_ANDNOT( new_opp_bits, opp_bits, bb_flips );
region_parity ^= holepar;
end_move_list[old_sq].succ = end_move_list[sq].succ;
new_disc_diff = -disc_diff - 2 * flipped - 1;
ev = -solve_parity( new_opp_bits, bb_flips, -beta, -alpha, oppcol,
empties - 1, new_disc_diff, TRUE );
region_parity ^= holepar;
end_move_list[old_sq].succ = sq;
if ( ev > score ) {
score = ev;
if ( ev > alpha ) {
if ( ev >= beta ) {
best_move = sq;
add_hash( ENDGAME_MODE, score, best_move,
ENDGAME_SCORE | LOWER_BOUND, empties, 0 );
return score;
}
alpha = ev;
}
best_sq = sq;
}
}
}
}
/* Even parity. */
parity_mask = ~parity_mask;
for ( old_sq = END_MOVE_LIST_HEAD, sq = end_move_list[old_sq].succ;
sq != END_MOVE_LIST_TAIL;
old_sq = sq, sq = end_move_list[sq].succ ) {
unsigned int holepar = quadrant_mask[sq];
if ( holepar & parity_mask ) {
flipped = TestFlips_wrapper( sq, my_bits, opp_bits );
if ( flipped != 0 ) {
FULL_ANDNOT( new_opp_bits, opp_bits, bb_flips );
region_parity ^= holepar;
end_move_list[old_sq].succ = end_move_list[sq].succ;
new_disc_diff = -disc_diff - 2 * flipped - 1;
ev = -solve_parity( new_opp_bits, bb_flips, -beta, -alpha, oppcol,
empties - 1, new_disc_diff, TRUE );
region_parity ^= holepar;
end_move_list[old_sq].succ = sq;
if ( ev > score ) {
score = ev;
if ( ev > alpha ) {
if ( ev >= beta ) {
best_move = sq;
add_hash( ENDGAME_MODE, score, best_move,
ENDGAME_SCORE | LOWER_BOUND, empties, 0 );
return score;
}
alpha = ev;
}
best_sq = sq;
}
}
}
}
if ( score == -INFINITE_EVAL ) {
if ( !pass_legal ) {
if ( disc_diff > 0 )
return disc_diff + empties;
if ( disc_diff < 0 )
return disc_diff - empties;
return 0;
}
else {
hash1 ^= hash_flip_color1;
hash2 ^= hash_flip_color2;
score = -solve_parity_hash( opp_bits, my_bits, -beta, -alpha, oppcol,
empties, -disc_diff, FALSE );
hash1 ^= hash_flip_color1;
hash2 ^= hash_flip_color2;
}
}
else {
best_move = best_sq;
if ( score > in_alpha)
add_hash( ENDGAME_MODE, score, best_move, ENDGAME_SCORE | EXACT_VALUE,
empties, 0 );
else
add_hash( ENDGAME_MODE, score, best_move, ENDGAME_SCORE | UPPER_BOUND,
empties, 0 );
}
return score;
}
static int
solve_parity_hash_high( BitBoard my_bits,
BitBoard opp_bits,
int alpha,
int beta,
int color,
int empties,
int disc_diff,
int pass_legal ) {
/* Move bonuses without and with parity for the squares.
These are only used when sorting moves in the 9-12 empties
range and were automatically tuned by OPTIMIZE. */
static const unsigned char move_bonus[2][128] = { /* 2 * 100 used */
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 24, 1, 0, 25, 25, 0, 1, 24, 0,
0, 1, 0, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 25, 0, 0, 0, 0, 0, 0, 25, 0,
0, 25, 0, 0, 0, 0, 0, 0, 25, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 1, 0,
0, 24, 1, 0, 25, 25, 0, 1, 24, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 128, 86, 122, 125, 125, 122, 86, 128, 0,
0, 86, 117, 128, 128, 128, 128, 117, 86, 0,
0, 122, 128, 128, 128, 128, 128, 128, 122, 0,
0, 125, 128, 128, 128, 128, 128, 128, 125, 0,
0, 125, 128, 128, 128, 128, 128, 128, 125, 0,
0, 122, 128, 128, 128, 128, 128, 128, 122, 0,
0, 86, 117, 128, 128, 128, 128, 117, 86, 0,
0, 128, 86, 122, 125, 125, 122, 86, 128, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }
};
BitBoard new_opp_bits;
BitBoard best_new_my_bits, best_new_opp_bits;
int i;
int score;
int in_alpha = alpha;
int oppcol = OPP( color );
int flipped, best_flipped;
int new_disc_diff;
int ev;
int hash_move;
int moves;
int parity;
int best_value, best_index;
int pred, succ;
int sq, old_sq, best_sq = 0;
int move_order[64];
int goodness[64];
unsigned int diff1, diff2;
HashEntry entry;
INCREMENT_COUNTER( nodes );
hash_move = -1;
find_hash( &entry, ENDGAME_MODE );
if ( entry.draft == empties ) {
if ( (entry.selectivity == 0) &&
(entry.flags & ENDGAME_SCORE) &&
valid_move( entry.move[0], color ) &&
((entry.flags & EXACT_VALUE) ||
((entry.flags & LOWER_BOUND) && entry.eval >= beta) ||
((entry.flags & UPPER_BOUND) && entry.eval <= alpha)) ) {
best_move = entry.move[0];
return entry.eval;
}
}
/* Check for stability cutoff */
#if USE_STABILITY
if ( alpha >= stability_threshold[empties] ) {
int stability_bound;