-
Notifications
You must be signed in to change notification settings - Fork 1
/
shogi.h
1447 lines (1272 loc) · 53.7 KB
/
shogi.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
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
#ifndef SHOGI_H
#define SHOGI_H
#include <stdio.h>
#include "param.h"
#if defined(_WIN32)
# include <Winsock2.h>
# define CONV_CDECL __cdecl
# define SCKT_NULL INVALID_SOCKET
typedef SOCKET sckt_t;
#else
# include <pthread.h>
# include <sys/times.h>
# define CONV_CDECL
# define SCKT_NULL -1
# define SOCKET_ERROR -1
typedef int sckt_t;
#endif
/* Microsoft C/C++ */
#if defined(_MSC_VER)
# define _CRT_DISABLE_PERFCRIT_LOCKS
# define UINT64_MAX ULLONG_MAX
# define PRIu64 "I64u"
# define PRIx64 "I64x"
# define UINT64_C(u) ( u )
# define restrict __restrict
# define strtok_r strtok_s
# define read _read
# define strncpy( dst, src, len ) strncpy_s( dst, len, src, _TRUNCATE )
# define snprintf( buf, size, fmt, ... ) \
_snprintf_s( buf, size, _TRUNCATE, fmt, __VA_ARGS__ )
# define vsnprintf( buf, size, fmt, list ) \
_vsnprintf_s( buf, size, _TRUNCATE, fmt, list )
typedef unsigned __int64 uint64_t;
typedef volatile long lock_t;
/* GNU C and Intel C/C++ on x86 and x86-64 */
#elif defined(__GNUC__) && ( defined(__i386__) || defined(__x86_64__) )
# include <inttypes.h>
# define restrict __restrict
typedef volatile int lock_t;
/* other targets. */
#else
# include <inttypes.h>
typedef pthread_mutex_t lock_t;
extern unsigned char aifirst_one[512];
extern unsigned char ailast_one[512];
#endif
/*
#define BK_ULTRA_NARROW
#define BK_COM
#define BK_SMALL
#define NO_NULL_PRUNE
#define NO_STDOUT
#define DBG_EASY
*/
#if defined(CSASHOGI)
# define NO_STDOUT
# if ! defined(WIN32_PIPE)
# define WIN32_PIPE
# endif
#endif
#if defined(TLP)
# define SHARE volatile
# define SEARCH_ABORT ( root_abort || ptree->tlp_abort )
#else
# define SHARE
# define SEARCH_ABORT root_abort
#endif
#define QUIES_PLY_LIMIT 7
#define SHELL_H_LEN 7
#define MAX_ANSWER 8
#define PLY_INC 8
#define PLY_MAX 48
#define RAND_N 624
#define TC_NMOVE 37U
#define SEC_MARGIN 15U
#define SEC_KEEP_ALIVE 180U
#define TIME_RESPONSE 200U
#define RESIGN_THRESHOLD ( ( MT_CAP_DRAGON * 5 ) / 8 )
#define BNZ_VER "Feliz 0.0"
#define REP_MAX_PLY 32
#define REP_HIST_LEN 256
#define EHASH_MASK 0x3fffffU /* occupies 32MB */
#define MNJ_MASK 0xfffU
#define HIST_SIZE 0x4000U
#define HIST_INVALID 0xffffU
#define HIST_MAX 0x1000U
#define REJEC_MASK 0x0ffffU
#define REJEC_MIN_DEPTH ( ( PLY_INC * 5 ) )
#define EXT_RECAP1 ( ( PLY_INC * 1 ) / 4 )
#define EXT_RECAP2 ( ( PLY_INC * 2 ) / 4 )
#define EXT_ONEREP ( ( PLY_INC * 2 ) / 4 )
#define EXT_CHECK ( ( PLY_INC * 4 ) / 4 )
#define EFUTIL_MG1 ( ( MT_CAP_DRAGON * 2 ) / 8 )
#define EFUTIL_MG2 ( ( MT_CAP_DRAGON * 2 ) / 8 )
#define FMG_MG ( ( MT_CAP_DRAGON * 2 ) / 16 )
#define FMG_MG_KING ( ( MT_CAP_DRAGON * 3 ) / 16 )
#define FMG_MG_MT ( ( MT_CAP_DRAGON * 8 ) / 16 )
#define FMG_MISC ( ( MT_CAP_DRAGON * 2 ) / 8 )
#define FMG_CAP ( ( MT_CAP_DRAGON * 2 ) / 8 )
#define FMG_DROP ( ( MT_CAP_DRAGON * 2 ) / 8 )
#define FMG_MT ( ( MT_CAP_DRAGON * 2 ) / 8 )
#define FMG_MISC_KING ( ( MT_CAP_DRAGON * 2 ) / 8 )
#define FMG_CAP_KING ( ( MT_CAP_DRAGON * 2 ) / 8 )
#define HASH_REG_HIST_LEN 256
#define HASH_REG_MINDIFF ( ( MT_CAP_DRAGON * 1 ) / 8 )
#define HASH_REG_THRESHOLD ( ( MT_CAP_DRAGON * 8 ) / 8 )
#define FV_WINDOW 256
#define FV_SCALE 32
#define FV_PENALTY ( 0.2 / (double)FV_SCALE )
#define MPV_MAX_PV 16
#define TLP_MAX_THREADS 12
#define TLP_NUM_WORK ( TLP_MAX_THREADS * 8 )
#define TIME_CHECK_MIN_NODE 10000U
#define TIME_CHECK_MAX_NODE 100000U
#define SIZE_FILENAME 256
#define SIZE_PLAYERNAME 256
#define SIZE_MESSAGE 512
#define SIZE_CMDLINE 512
#define SIZE_CSALINE 512
#define SIZE_CMDBUFFER 512
#define IsMove(move) ( (move) & 0xffffffU )
#define MOVE_NA 0x00000000U
#define MOVE_PASS 0x01000000U
#define MOVE_PONDER_FAILED 0xfe000000U
#define MOVE_RESIGN 0xff000000U
#define MOVE_CHK_SET 0x80000000U
#define MOVE_CHK_CLEAR 0x40000000U
#define MAX_LEGAL_MOVES 700
#define MAX_LEGAL_EVASION 256
#define MOVE_LIST_LEN 16384
#define MAX_SIZE_SECTION 0xffff
#define NUM_SECTION 0x4000
#define MATERIAL (ptree->posi.material)
#define HAND_B (ptree->posi.hand_black)
#define HAND_W (ptree->posi.hand_white)
#define BB_BOCCUPY (ptree->posi.b_occupied)
#define BB_BTGOLD (ptree->posi.b_tgold)
#define BB_B_HDK (ptree->posi.b_hdk)
#define BB_B_BH (ptree->posi.b_bh)
#define BB_B_RD (ptree->posi.b_rd)
#define BB_BPAWN_ATK (ptree->posi.b_pawn_attacks)
#define BB_BPAWN (ptree->posi.b_pawn)
#define BB_BLANCE (ptree->posi.b_lance)
#define BB_BKNIGHT (ptree->posi.b_knight)
#define BB_BSILVER (ptree->posi.b_silver)
#define BB_BGOLD (ptree->posi.b_gold)
#define BB_BBISHOP (ptree->posi.b_bishop)
#define BB_BROOK (ptree->posi.b_rook)
#define BB_BKING (abb_mask[SQ_BKING])
#define BB_BPRO_PAWN (ptree->posi.b_pro_pawn)
#define BB_BPRO_LANCE (ptree->posi.b_pro_lance)
#define BB_BPRO_KNIGHT (ptree->posi.b_pro_knight)
#define BB_BPRO_SILVER (ptree->posi.b_pro_silver)
#define BB_BHORSE (ptree->posi.b_horse)
#define BB_BDRAGON (ptree->posi.b_dragon)
#define BB_WOCCUPY (ptree->posi.w_occupied)
#define BB_WTGOLD (ptree->posi.w_tgold)
#define BB_W_HDK (ptree->posi.w_hdk)
#define BB_W_BH (ptree->posi.w_bh)
#define BB_W_RD (ptree->posi.w_rd)
#define BB_WPAWN_ATK (ptree->posi.w_pawn_attacks)
#define BB_WPAWN (ptree->posi.w_pawn)
#define BB_WLANCE (ptree->posi.w_lance)
#define BB_WKNIGHT (ptree->posi.w_knight)
#define BB_WSILVER (ptree->posi.w_silver)
#define BB_WGOLD (ptree->posi.w_gold)
#define BB_WBISHOP (ptree->posi.w_bishop)
#define BB_WROOK (ptree->posi.w_rook)
#define BB_WKING (abb_mask[SQ_WKING])
#define BB_WPRO_PAWN (ptree->posi.w_pro_pawn)
#define BB_WPRO_LANCE (ptree->posi.w_pro_lance)
#define BB_WPRO_KNIGHT (ptree->posi.w_pro_knight)
#define BB_WPRO_SILVER (ptree->posi.w_pro_silver)
#define BB_WHORSE (ptree->posi.w_horse)
#define BB_WDRAGON (ptree->posi.w_dragon)
#define OCCUPIED_FILE (ptree->posi.occupied_rl90)
#define OCCUPIED_DIAG1 (ptree->posi.occupied_rr45)
#define OCCUPIED_DIAG2 (ptree->posi.occupied_rl45)
#define BOARD (ptree->posi.asquare)
#define SQ_BKING (ptree->posi.isquare_b_king)
#define SQ_WKING (ptree->posi.isquare_w_king)
#define HASH_KEY (ptree->posi.hash_key)
#define HASH_VALUE (ptree->sort_value[0])
#define MOVE_CURR (ptree->current_move[ply])
#define MOVE_LAST (ptree->current_move[ply-1])
#define NullDepth(d) ( (d) < PLY_INC*26/4 ? (d)-PLY_INC*12/4 : \
( (d) <= PLY_INC*30/4 ? PLY_INC*14/4 \
: (d)-PLY_INC*16/4) )
#define LimitExtension(e,ply) if ( (e) && (ply) > 2 * iteration_depth ) { \
if ( (ply) < 4 * iteration_depth ) { \
e *= 4 * iteration_depth - (ply); \
e /= 2 * iteration_depth; \
} else { e = 0; } }
#define Flip(turn) ((turn)^1)
#define Inv(sq) (nsquare-1-sq)
#define PcOnSq(k,i) pc_on_sq[k][(i)*((i)+3)/2]
#define PcPcOnSq(k,i,j) pc_on_sq[k][(i)*((i)+1)/2+(j)]
/*
xxxxxxxx xxxxxxxx xxx11111 pawn
xxxxxxxx xxxxxxxx 111xxxxx lance
xxxxxxxx xxxxx111 xxxxxxxx knight
xxxxxxxx xx111xxx xxxxxxxx silver
xxxxxxx1 11xxxxxx xxxxxxxx gold
xxxxx11x xxxxxxxx xxxxxxxx bishop
xxx11xxx xxxxxxxx xxxxxxxx rook
*/
#define I2HandPawn(hand) (((hand) >> 0) & 0x1f)
#define I2HandLance(hand) (((hand) >> 5) & 0x07)
#define I2HandKnight(hand) (((hand) >> 8) & 0x07)
#define I2HandSilver(hand) (((hand) >> 11) & 0x07)
#define I2HandGold(hand) (((hand) >> 14) & 0x07)
#define I2HandBishop(hand) (((hand) >> 17) & 0x03)
#define I2HandRook(hand) ((hand) >> 19)
#define IsHandPawn(hand) ((hand) & 0x000001f)
#define IsHandLance(hand) ((hand) & 0x00000e0)
#define IsHandKnight(hand) ((hand) & 0x0000700)
#define IsHandSilver(hand) ((hand) & 0x0003800)
#define IsHandGold(hand) ((hand) & 0x001c000)
#define IsHandBishop(hand) ((hand) & 0x0060000)
#define IsHandRook(hand) ((hand) & 0x0180000)
/*
xxxxxxxx xxxxxxxx x1111111 destination
xxxxxxxx xx111111 1xxxxxxx starting square or drop piece+nsquare-1
xxxxxxxx x1xxxxxx xxxxxxxx flag for promotion
xxxxx111 1xxxxxxx xxxxxxxx piece to move
x1111xxx xxxxxxxx xxxxxxxx captured piece
*/
#define To2Move(to) ((unsigned int)(to) << 0)
#define From2Move(from) ((unsigned int)(from) << 7)
#define Drop2Move(piece) ((nsquare-1+(piece)) << 7)
#define Drop2From(piece) (nsquare-1+(piece))
#define FLAG_PROMO (1U << 14)
#define Piece2Move(piece) ((piece) << 15)
#define Cap2Move(piece) ((piece) << 19)
#define I2To(move) (((move) >> 0) & 0x007fU)
#define I2From(move) (((move) >> 7) & 0x007fU)
#define I2FromTo(move) (((move) >> 0) & 0x3fffU)
#define I2IsPromote(move) ((move) & FLAG_PROMO)
#define I2PieceMove(move) (((move) >> 15) & 0x000fU)
#define UToFromToPromo(u) ( (u) & 0x7ffffU )
#define UToCap(u) (((u) >> 19) & 0x000fU)
#define From2Drop(from) ((from)-nsquare+1)
#define BBIni(bb) (bb).p[0] = (bb).p[1] = (bb).p[2] = 0
#define BBToU(bb) ((bb).p[0] | (bb).p[1] | (bb).p[2])
#define BBToUShift(bb) ((bb).p[0]<<2 | (bb).p[1]<<1 | (bb).p[2])
#define PopuCount(bb) popu_count012( bb.p[0], bb.p[1], bb.p[2] )
#define FirstOne(bb) first_one012( bb.p[0], bb.p[1], bb.p[2] )
#define LastOne(bb) last_one210( bb.p[2], bb.p[1], bb.p[0] )
#define BBCmp(bb1,bb2) ( (bb1).p[0] != (bb2).p[0] \
|| (bb1).p[1] != (bb2).p[1] \
|| (bb1).p[2] != (bb2).p[2] )
#define BBNot(bb,bb1) (bb).p[0] = ~(bb1).p[0], \
(bb).p[1] = ~(bb1).p[1], \
(bb).p[2] = ~(bb1).p[2]
#define BBOr(bb,bb1,bb2) (bb).p[0] = (bb1).p[0] | (bb2).p[0], \
(bb).p[1] = (bb1).p[1] | (bb2).p[1], \
(bb).p[2] = (bb1).p[2] | (bb2).p[2]
#define BBAnd(bb,bb1,bb2) (bb).p[0] = (bb1).p[0] & (bb2).p[0], \
(bb).p[1] = (bb1).p[1] & (bb2).p[1], \
(bb).p[2] = (bb1).p[2] & (bb2).p[2]
#define BBXor(bb,b1,b2) (bb).p[0] = (b1).p[0] ^ (b2).p[0], \
(bb).p[1] = (b1).p[1] ^ (b2).p[1], \
(bb).p[2] = (b1).p[2] ^ (b2).p[2]
#define BBAndOr(bb,bb1,bb2) (bb).p[0] |= (bb1).p[0] & (bb2).p[0], \
(bb).p[1] |= (bb1).p[1] & (bb2).p[1], \
(bb).p[2] |= (bb1).p[2] & (bb2).p[2]
#define BBNotAnd(bb,bb1) bb.p[0] &= ~bb1.p[0]; \
bb.p[1] &= ~bb1.p[1]; \
bb.p[2] &= ~bb1.p[2]
#define BBContractShift(bb1,bb2) ( ( (bb1).p[0] & (bb2).p[0] ) << 2 \
| ( (bb1).p[1] & (bb2).p[1] ) << 1 \
| ( (bb1).p[2] & (bb2).p[2] ) )
#define BBContract(bb1,bb2) ( ( (bb1).p[0] & (bb2).p[0] ) \
| ( (bb1).p[1] & (bb2).p[1] ) \
| ( (bb1).p[2] & (bb2).p[2] ) )
#define Xor(i,bb) (bb).p[0] ^= abb_mask[i].p[0], \
(bb).p[1] ^= abb_mask[i].p[1], \
(bb).p[2] ^= abb_mask[i].p[2]
#define XorFile(i,bb) (bb).p[0] ^= abb_mask_rl90[i].p[0], \
(bb).p[1] ^= abb_mask_rl90[i].p[1], \
(bb).p[2] ^= abb_mask_rl90[i].p[2]
#define XorDiag1(i,bb) (bb).p[0] ^= abb_mask_rr45[i].p[0], \
(bb).p[1] ^= abb_mask_rr45[i].p[1], \
(bb).p[2] ^= abb_mask_rr45[i].p[2]
#define XorDiag2(i,bb) (bb).p[0] ^= abb_mask_rl45[i].p[0], \
(bb).p[1] ^= abb_mask_rl45[i].p[1], \
(bb).p[2] ^= abb_mask_rl45[i].p[2]
#define SetClear(bb) (bb).p[0] ^= (bb_set_clear.p[0]), \
(bb).p[1] ^= (bb_set_clear.p[1]), \
(bb).p[2] ^= (bb_set_clear.p[2])
#define SetClearFile(i1,i2,bb) \
(bb).p[0] ^= ((abb_mask_rl90[i1].p[0])|(abb_mask_rl90[i2].p[0])), \
(bb).p[1] ^= ((abb_mask_rl90[i1].p[1])|(abb_mask_rl90[i2].p[1])), \
(bb).p[2] ^= ((abb_mask_rl90[i1].p[2])|(abb_mask_rl90[i2].p[2]))
#define SetClearDiag1(i1,i2,bb) \
(bb).p[0] ^= ((abb_mask_rr45[i1].p[0])|(abb_mask_rr45[i2].p[0])), \
(bb).p[1] ^= ((abb_mask_rr45[i1].p[1])|(abb_mask_rr45[i2].p[1])), \
(bb).p[2] ^= ((abb_mask_rr45[i1].p[2])|(abb_mask_rr45[i2].p[2]))
#define SetClearDiag2(i1,i2,bb) \
(bb).p[0] ^= ((abb_mask_rl45[i1].p[0])|(abb_mask_rl45[i2].p[0])), \
(bb).p[1] ^= ((abb_mask_rl45[i1].p[1])|(abb_mask_rl45[i2].p[1])), \
(bb).p[2] ^= ((abb_mask_rl45[i1].p[2])|(abb_mask_rl45[i2].p[2]))
#define AttackFile(i) (abb_file_attacks[i] \
[((ptree->posi.occupied_rl90.p[aslide[i].irl90]) \
>> aslide[i].srl90) & 0x7f])
#define AttackRank(i) (ai_rook_attacks_r0[i] \
[((ptree->posi.b_occupied.p[aslide[i].ir0] \
|ptree->posi.w_occupied.p[aslide[i].ir0]) \
>> aslide[i].sr0) & 0x7f ])
#define AttackDiag1(i) \
(abb_bishop_attacks_rr45[i] \
[((ptree->posi.occupied_rr45.p[aslide[i].irr45]) \
>> aslide[i].srr45) & 0x7f])
#define AttackDiag2(i) \
(abb_bishop_attacks_rl45[i] \
[((ptree->posi.occupied_rl45.p[aslide[i].irl45]) \
>> aslide[i].srl45) & 0x7f])
#define BishopAttack0(i) ( AttackDiag1(i).p[0] | AttackDiag2(i).p[0] )
#define BishopAttack1(i) ( AttackDiag1(i).p[1] | AttackDiag2(i).p[1] )
#define BishopAttack2(i) ( AttackDiag1(i).p[2] | AttackDiag2(i).p[2] )
#define AttackBLance(bb,i) BBAnd( bb, abb_minus_rays[i], AttackFile(i) )
#define AttackWLance(bb,i) BBAnd( bb, abb_plus_rays[i], AttackFile(i) )
#define AttackHorse(bb,i) AttackBishop(bb,i); BBOr(bb,bb,abb_king_attacks[i])
#define AttackDragon(bb,i) AttackRook(bb,i); BBOr(bb,bb,abb_king_attacks[i])
#define InCheck(turn) \
( (turn) ? is_white_attacked( ptree, SQ_WKING ) \
: is_black_attacked( ptree, SQ_BKING ) )
#define MakeMove(turn,move,ply) \
( (turn) ? make_move_w( ptree, move, ply ) \
: make_move_b( ptree, move, ply ) )
#define UnMakeMove(turn,move,ply) \
( (turn) ? unmake_move_w( ptree, move, ply ) \
: unmake_move_b( ptree, move, ply ) )
#define IsMoveCheck( ptree, turn, move ) \
( (turn) ? is_move_check_w( ptree, move ) \
: is_move_check_b( ptree, move ) )
#define GenCaptures(turn,pmove) ( (turn) ? w_gen_captures( ptree, pmove ) \
: b_gen_captures( ptree, pmove ) )
#define GenNoCaptures(turn,pmove) \
( (turn) ? w_gen_nocaptures( ptree, pmove ) \
: b_gen_nocaptures( ptree, pmove ) )
#define GenDrop(turn,pmove) ( (turn) ? w_gen_drop( ptree, pmove ) \
: b_gen_drop( ptree, pmove ) )
#define GenCapNoProEx2(turn,pmove) \
( (turn) ? w_gen_cap_nopro_ex2( ptree, pmove ) \
: b_gen_cap_nopro_ex2( ptree, pmove ) )
#define GenNoCapNoProEx2(turn,pmove) \
( (turn) ? w_gen_nocap_nopro_ex2( ptree, pmove ) \
: b_gen_nocap_nopro_ex2( ptree, pmove ) )
#define GenEvasion(turn,pmove) \
( (turn) ? w_gen_evasion( ptree, pmove ) \
: b_gen_evasion( ptree, pmove ) )
#define GenCheck(turn,pmove) \
( (turn) ? w_gen_checks( ptree, pmove ) \
: b_gen_checks( ptree, pmove ) )
#define IsMateIn1Ply(turn) \
( (turn) ? is_w_mate_in_1ply( ptree ) \
: is_b_mate_in_1ply( ptree ) )
#define IsDiscoverBK(from,to) \
idirec = (int)adirec[SQ_BKING][from], \
( idirec && ( idirec!=(int)adirec[SQ_BKING][to] ) \
&& is_pinned_on_black_king( ptree, from, idirec ) )
#define IsDiscoverWK(from,to) \
idirec = (int)adirec[SQ_WKING][from], \
( idirec && ( idirec!=(int)adirec[SQ_WKING][to] ) \
&& is_pinned_on_white_king( ptree, from, idirec ) )
#define IsMateWPawnDrop(ptree,to) ( BOARD[(to)+9] == king \
&& is_mate_w_pawn_drop( ptree, to ) )
#define IsMateBPawnDrop(ptree,to) ( BOARD[(to)-9] == -king \
&& is_mate_b_pawn_drop( ptree, to ) )
enum { b0000, b0001, b0010, b0011, b0100, b0101, b0110, b0111,
b1000, b1001, b1010, b1011, b1100, b1101, b1110, b1111 };
enum { A9 = 0, B9, C9, D9, E9, F9, G9, H9, I9,
A8, B8, C8, D8, E8, F8, G8, H8, I8,
A7, B7, C7, D7, E7, F7, G7, H7, I7,
A6, B6, C6, D6, E6, F6, G6, H6, I6,
A5, B5, C5, D5, E5, F5, G5, H5, I5,
A4, B4, C4, D4, E4, F4, G4, H4, I4,
A3, B3, C3, D3, E3, F3, G3, H3, I3,
A2, B2, C2, D2, E2, F2, G2, H2, I2,
A1, B1, C1, D1, E1, F1, G1, H1, I1 };
enum { promote = 8, empty = 0,
pawn, lance, knight, silver, gold, bishop, rook, king, pro_pawn,
pro_lance, pro_knight, pro_silver, piece_null, horse, dragon };
enum { npawn_max = 18, nlance_max = 4, nknight_max = 4, nsilver_max = 4,
ngold_max = 4, nbishop_max = 2, nrook_max = 2, nking_max = 2 };
enum { rank1 = 0, rank2, rank3, rank4, rank5, rank6, rank7, rank8, rank9 };
enum { file1 = 0, file2, file3, file4, file5, file6, file7, file8, file9 };
enum { nhand = 7, nfile = 9, nrank = 9, nsquare = 81 };
enum { mask_file1 = (( 1U << 18 | 1U << 9 | 1U ) << 8) };
enum { flag_diag1 = b0001, flag_plus = b0010 };
enum { score_draw = 1,
score_max_eval = 30000,
score_mate1ply = 32598,
score_inferior = 32599,
score_bound = 32600,
score_foul = 32600 };
enum { phase_hash = b0001,
phase_killer1 = b0001 << 1,
phase_killer2 = b0010 << 1,
phase_killer = b0011 << 1,
phase_cap1 = b0001 << 3,
phase_cap_misc = b0010 << 3,
phase_cap = b0011 << 3,
phase_history1 = b0001 << 5,
phase_history2 = b0010 << 5,
phase_history = b0011 << 5,
phase_misc = b0100 << 5 };
enum { next_move_hash = 0, next_move_capture, next_move_history2,
next_move_misc };
/* next_evasion_hash should be the same as next_move_hash */
enum { next_evasion_hash = 0, next_evasion_genall, next_evasion_misc };
enum { next_quies_gencap, next_quies_captures, next_quies_misc };
enum { no_rep = 0, four_fold_rep, perpetual_check, perpetual_check2,
black_superi_rep, white_superi_rep, hash_hit, prev_solution, book_hit,
pv_fail_high, mate_search };
enum { record_misc, record_eof, record_next, record_resign, record_drawn,
record_error };
enum { black = 0, white = 1 };
enum { direc_misc = b0000,
direc_file = b0010, /* | */
direc_rank = b0011, /* - */
direc_diag1 = b0100, /* / */
direc_diag2 = b0101, /* \ */
flag_cross = b0010,
flag_diag = b0100 };
enum { value_null = b0000,
value_upper = b0001,
value_lower = b0010,
value_exact = b0011,
flag_value_up_exact = b0001,
flag_value_low_exact = b0010,
node_do_null = b0100,
node_do_recap = b1000,
node_do_mate = b0001 << 4,
node_mate_threat = b0010 << 4, /* <- don't change it */
node_do_futile = b0100 << 4,
state_node_end };
/* note: maximum bits are 8. tlp_state_node uses type unsigned char. */
enum { flag_from_ponder = b0001,
flag_refer_rest = b0010 };
enum { flag_time = b0001,
flag_history = b0010,
flag_rep = b0100,
flag_detect_hang = b1000,
flag_rejections = b0001 << 4,
flag_nomake_move = b0010 << 4,
flag_nofmargin = b0100 << 4 };
/* flags represent status of root move */
enum { flag_searched = b0001,
flag_failhigh = b0010,
flag_faillow = b0100,
flag_first = b1000 };
enum { flag_mated = b0001,
flag_resigned = b0010,
flag_drawn = b0100,
flag_suspend = b1000,
mask_game_end = b1111,
flag_quit = b0001 << 4,
flag_puzzling = b0010 << 4,
flag_pondering = b0100 << 4,
flag_thinking = b1000 << 4,
flag_problem = b0001 << 8,
flag_move_now = b0010 << 8,
flag_quit_ponder = b0100 << 8,
flag_search_error = b0001 << 12,
flag_quiet = b0010 << 12,
flag_reverse = b0100 << 12,
flag_narrow_book = b1000 << 12,
flag_time_extendable = b0001 << 16,
flag_learning = b0010 << 16,
flag_nobeep = b0100 << 16,
flag_nostress = b1000 << 16,
flag_nopeek = b0001 << 20,
flag_noponder = b0010 << 20,
flag_noprompt = b0100 << 20 };
enum { flag_hand_pawn = 1 << 0,
flag_hand_lance = 1 << 5,
flag_hand_knight = 1 << 8,
flag_hand_silver = 1 << 11,
flag_hand_gold = 1 << 14,
flag_hand_bishop = 1 << 17,
flag_hand_rook = 1 << 19 };
enum { f_hand_pawn = 0,
e_hand_pawn = 19,
f_hand_lance = 38,
e_hand_lance = 43,
f_hand_knight = 48,
e_hand_knight = 53,
f_hand_silver = 58,
e_hand_silver = 63,
f_hand_gold = 68,
e_hand_gold = 73,
f_hand_bishop = 78,
e_hand_bishop = 81,
f_hand_rook = 84,
e_hand_rook = 87,
fe_hand_end = 90,
f_pawn = 81,
e_pawn = 162,
f_lance = 225,
e_lance = 306,
f_knight = 360,
e_knight = 441,
f_silver = 504,
e_silver = 585,
f_gold = 666,
e_gold = 747,
f_bishop = 828,
e_bishop = 909,
f_horse = 990,
e_horse = 1071,
f_rook = 1152,
e_rook = 1233,
f_dragon = 1314,
e_dragon = 1395,
fe_end = 1476,
kkp_hand_pawn = 0,
kkp_hand_lance = 19,
kkp_hand_knight = 24,
kkp_hand_silver = 29,
kkp_hand_gold = 34,
kkp_hand_bishop = 39,
kkp_hand_rook = 42,
kkp_hand_end = 45,
kkp_pawn = 36,
kkp_lance = 108,
kkp_knight = 171,
kkp_silver = 252,
kkp_gold = 333,
kkp_bishop = 414,
kkp_horse = 495,
kkp_rook = 576,
kkp_dragon = 657,
kkp_end = 738 };
enum { pos_n = fe_end * ( fe_end + 1 ) / 2 };
typedef struct { unsigned int p[3]; } bitboard_t;
typedef struct { bitboard_t gold, silver, knight, lance; } check_table_t;
#if ! defined(MINIMUM)
typedef struct { fpos_t fpos; unsigned int games, moves, lines; } rpos_t;
typedef struct {
double pawn, lance, knight, silver, gold, bishop, rook;
double pro_pawn, pro_lance, pro_knight, pro_silver, horse, dragon;
float pc_on_sq[nsquare][fe_end*(fe_end+1)/2];
float kkp[nsquare][nsquare][kkp_end];
} param_t;
#endif
typedef enum { mode_write, mode_read_write, mode_read } record_mode_t;
typedef struct { uint64_t word1, word2; } trans_entry_t;
typedef struct { trans_entry_t prefer, always[2]; } trans_table_t;
typedef struct { int count; unsigned int cnst[2], vec[RAND_N]; }rand_work_t;
typedef struct {
uint64_t root;
SHARE uint64_t sibling;
} rejections_t;
typedef struct {
int no1_value, no2_value;
unsigned int no1, no2;
} move_killer_t;
typedef struct { unsigned int no1, no2; } killer_t;
typedef struct {
union { char str_move[ MAX_ANSWER ][ 8 ]; } info;
char str_name1[ SIZE_PLAYERNAME ];
char str_name2[ SIZE_PLAYERNAME ];
FILE *pf;
unsigned int games, moves, lines;
} record_t;
typedef struct {
unsigned int a[PLY_MAX];
unsigned char type;
unsigned char length;
unsigned char depth;
} pv_t;
typedef struct {
unsigned char ir0, sr0;
unsigned char irl90, srl90;
unsigned char irl45, srl45;
unsigned char irr45, srr45;
} slide_tbl_t;
typedef struct {
uint64_t hash_key;
bitboard_t b_occupied, w_occupied;
bitboard_t occupied_rl90, occupied_rl45, occupied_rr45;
bitboard_t b_hdk, w_hdk;
bitboard_t b_tgold, w_tgold;
bitboard_t b_bh, w_bh;
bitboard_t b_rd, w_rd;
bitboard_t b_pawn_attacks, w_pawn_attacks;
bitboard_t b_lance, w_lance;
bitboard_t b_knight, w_knight;
bitboard_t b_silver, w_silver;
bitboard_t b_bishop, w_bishop;
bitboard_t b_rook, w_rook;
bitboard_t b_horse, w_horse;
bitboard_t b_dragon, w_dragon;
bitboard_t b_pawn, w_pawn;
bitboard_t b_gold, w_gold;
bitboard_t b_pro_pawn, w_pro_pawn;
bitboard_t b_pro_lance, w_pro_lance;
bitboard_t b_pro_knight, w_pro_knight;
bitboard_t b_pro_silver, w_pro_silver;
unsigned int hand_black, hand_white;
int material;
signed char asquare[nsquare];
unsigned char isquare_b_king, isquare_w_king;
} posi_t;
typedef struct {
unsigned int hand_black, hand_white;
char turn_to_move;
signed char asquare[nsquare];
} min_posi_t;
typedef struct {
uint64_t nodes;
unsigned int move, status;
} root_move_t;
typedef struct {
unsigned int *move_last;
unsigned int move_cap1;
unsigned int move_cap2;
int phase_done, next_phase, remaining, value_cap1, value_cap2;
} next_move_t;
/* data: 31 1bit flag_learned */
/* 30 1bit is_flip */
/* 15 16bit value */
typedef struct {
uint64_t key_book;
unsigned int key_responsible, key_probed, key_played;
unsigned int hand_responsible, hand_probed, hand_played;
unsigned int move_played, move_responsible, move_probed, data;
} history_book_learn_t;
typedef struct tree tree_t;
struct tree {
posi_t posi;
uint64_t rep_board_list[ REP_HIST_LEN ];
uint64_t node_searched;
unsigned int *move_last[ PLY_MAX ];
next_move_t anext_move[ PLY_MAX ];
pv_t pv[ PLY_MAX ];
move_killer_t amove_killer[ PLY_MAX ];
unsigned int null_pruning_done;
unsigned int null_pruning_tried;
unsigned int check_extension_done;
unsigned int recap_extension_done;
unsigned int onerp_extension_done;
unsigned int neval_called;
unsigned int nquies_called;
unsigned int nfour_fold_rep;
unsigned int nperpetual_check;
unsigned int nsuperior_rep;
unsigned int nrep_tried;
unsigned int nreject_tried;
unsigned int nreject_done;
unsigned int ntrans_always_hit;
unsigned int ntrans_prefer_hit;
unsigned int ntrans_probe;
unsigned int ntrans_exact;
unsigned int ntrans_lower;
unsigned int ntrans_upper;
unsigned int ntrans_superior_hit;
unsigned int ntrans_inferior_hit;
unsigned int fail_high;
unsigned int fail_high_first;
unsigned int rep_hand_list[ REP_HIST_LEN ];
unsigned int amove_hash[ PLY_MAX ];
unsigned int amove[ MOVE_LIST_LEN ];
unsigned int current_move[ PLY_MAX ];
killer_t killers[ PLY_MAX ];
unsigned int hist_nmove[ PLY_MAX ];
unsigned int hist_move[ PLY_MAX ][ MAX_LEGAL_MOVES ];
int sort_value[ MAX_LEGAL_MOVES ];
unsigned short hist_tried[ HIST_SIZE ];
unsigned short hist_good[ HIST_SIZE ];
short save_material[ PLY_MAX ];
short stand_pat[ PLY_MAX+1 ];
unsigned char nsuc_check[ PLY_MAX+1 ];
#if defined(TLP)
struct tree *tlp_ptrees_sibling[ TLP_MAX_THREADS ];
struct tree *tlp_ptree_parent;
lock_t tlp_lock;
volatile int tlp_abort;
volatile int tlp_used;
unsigned short tlp_slot;
short tlp_beta;
short tlp_best;
volatile unsigned char tlp_nsibling;
unsigned char tlp_depth;
unsigned char tlp_state_node;
unsigned char tlp_id;
char tlp_turn;
char tlp_ply;
#endif
};
extern SHARE unsigned int game_status;
extern history_book_learn_t history_book_learn[ HASH_REG_HIST_LEN ];
extern int npawn_box;
extern int nlance_box;
extern int nknight_box;
extern int nsilver_box;
extern int ngold_box;
extern int nbishop_box;
extern int nrook_box;
extern unsigned int ponder_move_list[ MAX_LEGAL_MOVES ];
extern unsigned int ponder_move;
extern int ponder_nmove;
extern root_move_t root_move_list[ MAX_LEGAL_MOVES ];
extern SHARE int root_abort;
extern int root_nrep;
extern int root_nmove;
extern int root_value;
extern int root_alpha;
extern int root_beta;
extern int root_turn;
extern int root_move_cap;
extern int root_nfail_high;
extern int root_nfail_low;
extern int resign_threshold;
extern int n_nobook_move;
extern uint64_t node_limit;
extern unsigned int node_per_second;
extern unsigned int node_next_signal;
extern unsigned int node_last_check;
extern unsigned int hash_mask;
extern int trans_table_age;
extern pv_t last_pv;
extern pv_t last_pv_save;
extern int last_root_value;
extern int last_root_value_save;
extern SHARE trans_table_t *ptrans_table;
extern trans_table_t *ptrans_table_orig;
extern int log2_ntrans_table;
extern int depth_limit;
extern unsigned int time_last_result;
extern unsigned int time_last_eff_search;
extern unsigned int time_last_search;
extern unsigned int time_last_check;
extern unsigned int time_turn_start;
extern unsigned int time_start;
extern unsigned int time_max_limit;
extern unsigned int time_limit;
extern unsigned int time_response;
extern unsigned int sec_limit;
extern unsigned int sec_limit_up;
extern unsigned int sec_limit_depth;
extern unsigned int sec_elapsed;
extern unsigned int sec_b_total;
extern unsigned int sec_w_total;
extern record_t record_problems;
extern record_t record_game;
extern FILE *pf_book;
extern FILE *pf_hash;
extern int irecord_game;
extern short p_value[31];
extern short pc_on_sq[nsquare][fe_end*(fe_end+1)/2];
extern short kkp[nsquare][nsquare][kkp_end];
extern uint64_t ehash_tbl[ EHASH_MASK + 1 ];
extern unsigned char hash_rejections_parent[ REJEC_MASK+1 ];
extern rejections_t hash_rejections[ REJEC_MASK+1 ];
extern rand_work_t rand_work;
extern slide_tbl_t aslide[ nsquare ];
extern bitboard_t abb_b_knight_attacks[ nsquare ];
extern bitboard_t abb_b_silver_attacks[ nsquare ];
extern bitboard_t abb_b_gold_attacks[ nsquare ];
extern bitboard_t abb_w_knight_attacks[ nsquare ];
extern bitboard_t abb_w_silver_attacks[ nsquare ];
extern bitboard_t abb_w_gold_attacks[ nsquare ];
extern bitboard_t abb_king_attacks[ nsquare ];
extern bitboard_t abb_obstacle[ nsquare ][ nsquare ];
extern bitboard_t abb_bishop_attacks_rl45[ nsquare ][ 128 ];
extern bitboard_t abb_bishop_attacks_rr45[ nsquare ][ 128 ];
extern bitboard_t abb_file_attacks[ nsquare ][ 128 ];
extern bitboard_t abb_mask[ nsquare ];
extern bitboard_t abb_mask_rl90[ nsquare ];
extern bitboard_t abb_mask_rl45[ nsquare ];
extern bitboard_t abb_mask_rr45[ nsquare ];
extern bitboard_t abb_plus_rays[ nsquare ];
extern bitboard_t abb_minus_rays[ nsquare ];
extern uint64_t b_pawn_rand[ nsquare ];
extern uint64_t b_lance_rand[ nsquare ];
extern uint64_t b_knight_rand[ nsquare ];
extern uint64_t b_silver_rand[ nsquare ];
extern uint64_t b_gold_rand[ nsquare ];
extern uint64_t b_bishop_rand[ nsquare ];
extern uint64_t b_rook_rand[ nsquare ];
extern uint64_t b_king_rand[ nsquare ];
extern uint64_t b_pro_pawn_rand[ nsquare ];
extern uint64_t b_pro_lance_rand[ nsquare ];
extern uint64_t b_pro_knight_rand[ nsquare ];
extern uint64_t b_pro_silver_rand[ nsquare ];
extern uint64_t b_horse_rand[ nsquare ];
extern uint64_t b_dragon_rand[ nsquare ];
extern uint64_t b_hand_pawn_rand[ npawn_max ];
extern uint64_t b_hand_lance_rand[ nlance_max ];
extern uint64_t b_hand_knight_rand[ nknight_max ];
extern uint64_t b_hand_silver_rand[ nsilver_max ];
extern uint64_t b_hand_gold_rand[ ngold_max ];
extern uint64_t b_hand_bishop_rand[ nbishop_max ];
extern uint64_t b_hand_rook_rand[ nrook_max ];
extern uint64_t w_pawn_rand[ nsquare ];
extern uint64_t w_lance_rand[ nsquare ];
extern uint64_t w_knight_rand[ nsquare ];
extern uint64_t w_silver_rand[ nsquare ];
extern uint64_t w_gold_rand[ nsquare ];
extern uint64_t w_bishop_rand[ nsquare ];
extern uint64_t w_rook_rand[ nsquare ];
extern uint64_t w_king_rand[ nsquare ];
extern uint64_t w_pro_pawn_rand[ nsquare ];
extern uint64_t w_pro_lance_rand[ nsquare ];
extern uint64_t w_pro_knight_rand[ nsquare ];
extern uint64_t w_pro_silver_rand[ nsquare ];
extern uint64_t w_horse_rand[ nsquare ];
extern uint64_t w_dragon_rand[ nsquare ];
extern uint64_t w_hand_pawn_rand[ npawn_max ];
extern uint64_t w_hand_lance_rand[ nlance_max ];
extern uint64_t w_hand_knight_rand[ nknight_max ];
extern uint64_t w_hand_silver_rand[ nsilver_max ];
extern uint64_t w_hand_gold_rand[ ngold_max ];
extern uint64_t w_hand_bishop_rand[ nbishop_max ];
extern uint64_t w_hand_rook_rand[ nrook_max ];
extern unsigned int ai_rook_attacks_r0[ nsquare ][ 128 ];
extern unsigned int move_evasion_pchk;
extern int p_value_ex[31];
extern int benefit2promo[15];
extern int easy_abs;
extern int easy_min;
extern int easy_max;
extern int easy_value;
extern SHARE int fmg_misc;
extern SHARE int fmg_cap;
extern SHARE int fmg_drop;
extern SHARE int fmg_mt;
extern SHARE int fmg_misc_king;
extern SHARE int fmg_cap_king;
extern int iteration_depth;
extern unsigned char book_section[ MAX_SIZE_SECTION+1 ];
extern unsigned char adirec[nsquare][nsquare];
extern unsigned char is_same[16][16];
extern char str_message[ SIZE_MESSAGE ];
extern char str_cmdline[ SIZE_CMDLINE ];
extern char str_buffer_cmdline[ SIZE_CMDBUFFER ];
extern const char *str_error;
extern const char *astr_table_piece[ 16 ];
extern const char *str_resign;
extern const char *str_repetition;
extern const char *str_jishogi;
extern const char *str_record_error;
extern const char *str_unexpect_eof;
extern const char *str_ovrflw_line;
extern const char *str_warning;
extern const char *str_on;
extern const char *str_off;
extern const char *str_book;
extern const char *str_hash;
extern const char *str_fv;
extern const char *str_book_error;
extern const char *str_perpet_check;