-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.c
More file actions
4132 lines (3267 loc) · 124 KB
/
Copy pathengine.c
File metadata and controls
4132 lines (3267 loc) · 124 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
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
/*****************************************************\
====================================================
================= headers ================
====================================================
\*****************************************************/
// system headers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef _WIN32
#include <windows.h>
#else
# include <sys/time.h>
#endif
// include NNUE wrapper header
#include "nnue_eval.h"
// define version
#define version " -1.0 + SF NNUE"
// define bitboard data type
#define U64 unsigned long long
// FEN dedug positions
#define empty_board "8/8/8/8/8/8/8/8 b - - "
#define start_position "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 "
#define tricky_position "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1 "
#define killer_position "rnbqkb1r/pp1p1pPp/8/2p1pP2/1P1P4/3P3P/P1P1P3/RNBQKBNR w KQkq e6 0 1"
#define cmk_position "r2q1rk1/ppp2ppp/2n1bn2/2b1p3/3pP3/3P1NPP/PPP1NPB1/R1BQ1RK1 b - - 0 9 "
#define repetitions "2r3k1/R7/8/1R6/8/8/P4KPP/8 w - - 0 40 "
// board squares
enum {
a8, b8, c8, d8, e8, f8, g8, h8,
a7, b7, c7, d7, e7, f7, g7, h7,
a6, b6, c6, d6, e6, f6, g6, h6,
a5, b5, c5, d5, e5, f5, g5, h5,
a4, b4, c4, d4, e4, f4, g4, h4,
a3, b3, c3, d3, e3, f3, g3, h3,
a2, b2, c2, d2, e2, f2, g2, h2,
a1, b1, c1, d1, e1, f1, g1, h1,
no_sq
};
// encode pieces
enum { P, N, B, R, Q, K, p, n, b, r, q, k };
// sides to move (colors)
enum { white, black, both };
// bishop and rook
enum { rook, bishop };
// castling rights binary encoding
/*
bin dec
0001 1 white king can castle to the king side
0010 2 white king can castle to the queen side
0100 4 black king can castle to the king side
1000 8 black king can castle to the queen side
examples
1111 both sides an castle both directions
1001 black king => queen side
white king => king side
*/
enum { wk = 1, wq = 2, bk = 4, bq = 8 };
// convert squares to coordinates
const char *square_to_coordinates[] = {
"a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8",
"a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7",
"a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6",
"a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5",
"a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4",
"a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3",
"a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2",
"a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1",
};
// ASCII pieces
char ascii_pieces[12] = "PNBRQKpnbrqk";
// unicode pieces
char *unicode_pieces[12] = {"♙", "♘", "♗", "♖", "♕", "♔", "♟︎", "♞", "♝", "♜", "♛", "♚"};
// convert ASCII character pieces to encoded constants
int char_pieces[] = {
['P'] = P,
['N'] = N,
['B'] = B,
['R'] = R,
['Q'] = Q,
['K'] = K,
['p'] = p,
['n'] = n,
['b'] = b,
['r'] = r,
['q'] = q,
['k'] = k
};
// promoted pieces
char promoted_pieces[] = {
[Q] = 'q',
[R] = 'r',
[B] = 'b',
[N] = 'n',
[q] = 'q',
[r] = 'r',
[b] = 'b',
[n] = 'n'
};
/**********************************\
==================================
Chess board
==================================
\**********************************/
/*
WHITE PIECES
Pawns Knights Bishops
8 0 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0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♜
7 ♟︎ ♟︎ ♟︎ ♟︎ ♟︎ ♟︎ ♟︎ ♟︎
6 . . . . . . . .
5 . . . . . . . .
4 . . . . . . . .
3 . . . . . . . .
2 ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
1 ♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
a b c d e f g h
*/
// piece bitboards
U64 bitboards[12];
// occupancy bitboards
U64 occupancies[3];
// side to move
int side;
// enpassant square
int enpassant = no_sq;
// castling rights
int castle;
// "almost" unique position identifier aka hash key or position key
U64 hash_key;
// positions repetition table
U64 repetition_table[1000]; // 1000 is a number of plies (500 moves) in the entire game
// repetition index
int repetition_index;
// half move counter
int ply;
// fifty move rule counter
int fifty;
/**********************************\
==================================
Time controls variables
==================================
\**********************************/
// exit from engine flag
int quit = 0;
// UCI "movestogo" command moves counter
int movestogo = 30;
// UCI "movetime" command time counter
int movetime = -1;
// UCI "time" command holder (ms)
int ttime = -1;
// UCI "inc" command's time increment holder
int inc = 0;
// UCI "starttime" command time holder
int starttime = 0;
// UCI "stoptime" command time holder
int stoptime = 0;
// variable to flag time control availability
int timeset = 0;
// variable to flag when the time is up
int stopped = 0;
/**********************************\
==================================
Miscellaneous functions
==================================
\**********************************/
// get time in milliseconds
int get_time_ms()
{
#ifdef _WIN32
return GetTickCount();
#else
struct timeval time_value;
gettimeofday(&time_value, NULL);
return time_value.tv_sec * 1000 + time_value.tv_usec / 1000;
#endif
}
/*
Function to "listen" to GUI's input during search.
It's waiting for the user input from STDIN.
OS dependent.
First Richard Allbert aka BluefeverSoftware grabbed it from somewhere...
*/
int input_waiting()
{
#ifndef _WIN32
fd_set readfds;
struct timeval tv;
FD_ZERO (&readfds);
FD_SET (fileno(stdin), &readfds);
tv.tv_sec=0; tv.tv_usec=0;
select(16, &readfds, 0, 0, &tv);
return (FD_ISSET(fileno(stdin), &readfds));
#else
static int init = 0, pipe;
static HANDLE inh;
DWORD dw;
if (!init)
{
init = 1;
inh = GetStdHandle(STD_INPUT_HANDLE);
pipe = !GetConsoleMode(inh, &dw);
if (!pipe)
{
SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT|ENABLE_WINDOW_INPUT));
FlushConsoleInputBuffer(inh);
}
}
if (pipe)
{
if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL)) return 1;
return dw;
}
else
{
GetNumberOfConsoleInputEvents(inh, &dw);
return dw <= 1 ? 0 : dw;
}
#endif
}
// read GUI/user input
void read_input()
{
// bytes to read holder
int bytes;
// GUI/user input
char input[256] = "", *endc;
// "listen" to STDIN
if (input_waiting())
{
// tell engine to stop calculating
stopped = 1;
// loop to read bytes from STDIN
do
{
// read bytes from STDIN
bytes=read(fileno(stdin), input, 256);
}
// until bytes available
while (bytes < 0);
// searches for the first occurrence of '\n'
endc = strchr(input,'\n');
// if found new line set value at pointer to 0
if (endc) *endc=0;
// if input is available
if (strlen(input) > 0)
{
// match UCI "quit" command
if (!strncmp(input, "quit", 4))
// tell engine to terminate exacution
quit = 1;
// // match UCI "stop" command
else if (!strncmp(input, "stop", 4))
// tell engine to terminate exacution
quit = 1;
}
}
}
// a bridge function to interact between search and GUI input
static void communicate() {
// if time is up break here
if(timeset == 1 && get_time_ms() > stoptime) {
// tell engine to stop calculating
stopped = 1;
}
// read GUI input
read_input();
}
/**********************************\
==================================
Random numbers
==================================
\**********************************/
// pseudo random number state
unsigned int random_state = 1804289383;
// generate 32-bit pseudo legal numbers
unsigned int get_random_U32_number()
{
// get current state
unsigned int number = random_state;
// XOR shift algorithm
number ^= number << 13;
number ^= number >> 17;
number ^= number << 5;
// update random number state
random_state = number;
// return random number
return number;
}
// generate 64-bit pseudo legal numbers
U64 get_random_U64_number()
{
// define 4 random numbers
U64 n1, n2, n3, n4;
// init random numbers slicing 16 bits from MS1B side
n1 = (U64)(get_random_U32_number()) & 0xFFFF;
n2 = (U64)(get_random_U32_number()) & 0xFFFF;
n3 = (U64)(get_random_U32_number()) & 0xFFFF;
n4 = (U64)(get_random_U32_number()) & 0xFFFF;
// return random number
return n1 | (n2 << 16) | (n3 << 32) | (n4 << 48);
}
// generate magic number candidate
U64 generate_magic_number()
{
return get_random_U64_number() & get_random_U64_number() & get_random_U64_number();
}
/**********************************\
==================================
Bit manipulations
==================================
\**********************************/
// set/get/pop bit macros
#define set_bit(bitboard, square) ((bitboard) |= (1ULL << (square)))
#define get_bit(bitboard, square) ((bitboard) & (1ULL << (square)))
#define pop_bit(bitboard, square) ((bitboard) &= ~(1ULL << (square)))
// count bits within a bitboard (Brian Kernighan's way)
static inline int count_bits(U64 bitboard)
{
// bit counter
int count = 0;
// consecutively reset least significant 1st bit
while (bitboard)
{
// increment count
count++;
// reset least significant 1st bit
bitboard &= bitboard - 1;
}
// return bit count
return count;
}
// get least significant 1st bit index
static inline int get_ls1b_index(U64 bitboard)
{
// make sure bitboard is not 0
if (bitboard)
{
// count trailing bits before LS1B
return count_bits((bitboard & -bitboard) - 1);
}
//otherwise
else
// return illegal index
return -1;
}
/**********************************\
==================================
Zobrist keys
==================================
\**********************************/
// random piece keys [piece][square]
U64 piece_keys[12][64];
// random enpassant keys [square]
U64 enpassant_keys[64];
// random castling keys
U64 castle_keys[16];
// random side key
U64 side_key;
// init random hash keys
void init_random_keys()
{
// update pseudo random number state
random_state = 1804289383;
// loop over piece codes
for (int piece = P; piece <= k; piece++)
{
// loop over board squares
for (int square = 0; square < 64; square++)
// init random piece keys
piece_keys[piece][square] = get_random_U64_number();
}
// loop over board squares
for (int square = 0; square < 64; square++)
// init random enpassant keys
enpassant_keys[square] = get_random_U64_number();
// loop over castling keys
for (int index = 0; index < 16; index++)
// init castling keys
castle_keys[index] = get_random_U64_number();
// init random side key
side_key = get_random_U64_number();
}
// generate "almost" unique position ID aka hash key from scratch
U64 generate_hash_key()
{
// final hash key
U64 final_key = 0ULL;
// temp piece bitboard copy
U64 bitboard;
// loop over piece bitboards
for (int piece = P; piece <= k; piece++)
{
// init piece bitboard copy
bitboard = bitboards[piece];
// loop over the pieces within a bitboard
while (bitboard)
{
// init square occupied by the piece
int square = get_ls1b_index(bitboard);
// hash piece
final_key ^= piece_keys[piece][square];
// pop LS1B
pop_bit(bitboard, square);
}
}
// if enpassant square is on board
if (enpassant != no_sq)
// hash enpassant
final_key ^= enpassant_keys[enpassant];
// hash castling rights
final_key ^= castle_keys[castle];
// hash the side only if black is to move
if (side == black) final_key ^= side_key;
// return generated hash key
return final_key;
}
/**********************************\
==================================
Input & Output
==================================
\**********************************/
// print bitboard
void print_bitboard(U64 bitboard)
{
// print offset
printf("\n");
// loop over board ranks
for (int rank = 0; rank < 8; rank++)
{
// loop over board files
for (int file = 0; file < 8; file++)
{
// convert file & rank into square index
int square = rank * 8 + file;
// print ranks
if (!file)
printf(" %d ", 8 - rank);
// print bit state (either 1 or 0)
printf(" %d", get_bit(bitboard, square) ? 1 : 0);
}
// print new line every rank
printf("\n");
}
// print board files
printf("\n a b c d e f g h\n\n");
// print bitboard as unsigned decimal number
printf(" Bitboard: %llud\n\n", bitboard);
}
// print board
void print_board()
{
// print offset
printf("\n");
// loop over board ranks
for (int rank = 0; rank < 8; rank++)
{
// loop ober board files
for (int file = 0; file < 8; file++)
{
// init square
int square = rank * 8 + file;
// print ranks
if (!file)
printf(" %d ", 8 - rank);
// define piece variable
int piece = -1;
// loop over all piece bitboards
for (int bb_piece = P; bb_piece <= k; bb_piece++)
{
// if there is a piece on current square
if (get_bit(bitboards[bb_piece], square))
// get piece code
piece = bb_piece;
}
// print different piece set depending on OS
#ifdef WIN64
printf(" %c", (piece == -1) ? '.' : ascii_pieces[piece]);
#else
printf(" %s", (piece == -1) ? "." : unicode_pieces[piece]);
#endif
}
// print new line every rank
printf("\n");
}
// print board files
printf("\n a b c d e f g h\n\n");
// print side to move
printf(" Side: %s\n", !side ? "white" : "black");
// print enpassant square
printf(" Enpassant: %s\n", (enpassant != no_sq) ? square_to_coordinates[enpassant] : "no");
// print castling rights
printf(" Castling: %c%c%c%c\n\n", (castle & wk) ? 'K' : '-',
(castle & wq) ? 'Q' : '-',
(castle & bk) ? 'k' : '-',
(castle & bq) ? 'q' : '-');
// print hash key
printf(" Hash key: %llx\n", hash_key);
// fifty move rule counter
printf(" Fifty move: %d\n\n", fifty);
}
// reset board variables
void reset_board()
{
// reset board position (bitboards)
memset(bitboards, 0ULL, sizeof(bitboards));
// reset occupancies (bitboards)
memset(occupancies, 0ULL, sizeof(occupancies));
// reset game state variables
side = 0;
enpassant = no_sq;
castle = 0;
// reset repetition index
repetition_index = 0;
// reset fifty move rule counter
fifty = 0;
// reset repetition table
memset(repetition_table, 0ULL, sizeof(repetition_table));
}
// parse FEN string
void parse_fen(char *fen)
{
// prepare for new game
reset_board();
// loop over board ranks
for (int rank = 0; rank < 8; rank++)
{
// loop over board files
for (int file = 0; file < 8; file++)
{
// init current square
int square = rank * 8 + file;
// match ascii pieces within FEN string
if ((*fen >= 'a' && *fen <= 'z') || (*fen >= 'A' && *fen <= 'Z'))
{
// init piece type
int piece = char_pieces[*fen];
// set piece on corresponding bitboard
set_bit(bitboards[piece], square);
// increment pointer to FEN string
fen++;
}
// match empty square numbers within FEN string
if (*fen >= '0' && *fen <= '9')
{
// init offset (convert char 0 to int 0)
int offset = *fen - '0';
// define piece variable
int piece = -1;
// loop over all piece bitboards
for (int bb_piece = P; bb_piece <= k; bb_piece++)
{
// if there is a piece on current square
if (get_bit(bitboards[bb_piece], square))
// get piece code
piece = bb_piece;
}
// on empty current square
if (piece == -1)
// decrement file
file--;
// adjust file counter
file += offset;
// increment pointer to FEN string
fen++;
}
// match rank separator
if (*fen == '/')
// increment pointer to FEN string
fen++;
}
}
// got to parsing side to move (increment pointer to FEN string)
fen++;
// parse side to move
(*fen == 'w') ? (side = white) : (side = black);
// go to parsing castling rights
fen += 2;
// parse castling rights
while (*fen != ' ')
{
switch (*fen)
{
case 'K': castle |= wk; break;
case 'Q': castle |= wq; break;
case 'k': castle |= bk; break;
case 'q': castle |= bq; break;
case '-': break;
}
// increment pointer to FEN string
fen++;
}
// got to parsing enpassant square (increment pointer to FEN string)
fen++;
// parse enpassant square
if (*fen != '-')
{
// parse enpassant file & rank
int file = fen[0] - 'a';
int rank = 8 - (fen[1] - '0');
// init enpassant square
enpassant = rank * 8 + file;
}
// no enpassant square
else
enpassant = no_sq;
// loop over white pieces bitboards
for (int piece = P; piece <= K; piece++)
// populate white occupancy bitboard
occupancies[white] |= bitboards[piece];
// loop over black pieces bitboards
for (int piece = p; piece <= k; piece++)
// populate white occupancy bitboard
occupancies[black] |= bitboards[piece];
// init all occupancies
occupancies[both] |= occupancies[white];
occupancies[both] |= occupancies[black];
// init hash key
hash_key = generate_hash_key();
}
/**********************************\
==================================
Attacks
==================================
\**********************************/
/*
not A file not H file not HG files not AB files
bitboard bitboard bitboard bitboard
8 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1
7 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1
6 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1
5 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1
4 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1
3 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1
2 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1
1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1
a b c d e f g h a b c d e f g h a b c d e f g h a b c d e f g h
*/
// not A file constant
const U64 not_a_file = 18374403900871474942ULL;
// not H file constant
const U64 not_h_file = 9187201950435737471ULL;
// not HG file constant
const U64 not_hg_file = 4557430888798830399ULL;
// not AB file constant
const U64 not_ab_file = 18229723555195321596ULL;
// bishop relevant occupancy bit count for every square on board
const int bishop_relevant_bits[64] = {
6, 5, 5, 5, 5, 5, 5, 6,
5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 7, 7, 7, 7, 5, 5,
5, 5, 7, 9, 9, 7, 5, 5,
5, 5, 7, 9, 9, 7, 5, 5,
5, 5, 7, 7, 7, 7, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5,
6, 5, 5, 5, 5, 5, 5, 6
};
// rook relevant occupancy bit count for every square on board
const int rook_relevant_bits[64] = {
12, 11, 11, 11, 11, 11, 11, 12,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
12, 11, 11, 11, 11, 11, 11, 12
};
// rook magic numbers
U64 rook_magic_numbers[64] = {
0x8a80104000800020ULL,
0x140002000100040ULL,
0x2801880a0017001ULL,
0x100081001000420ULL,
0x200020010080420ULL,
0x3001c0002010008ULL,
0x8480008002000100ULL,
0x2080088004402900ULL,
0x800098204000ULL,
0x2024401000200040ULL,
0x100802000801000ULL,
0x120800800801000ULL,
0x208808088000400ULL,
0x2802200800400ULL,
0x2200800100020080ULL,
0x801000060821100ULL,
0x80044006422000ULL,
0x100808020004000ULL,
0x12108a0010204200ULL,
0x140848010000802ULL,
0x481828014002800ULL,
0x8094004002004100ULL,
0x4010040010010802ULL,
0x20008806104ULL,
0x100400080208000ULL,
0x2040002120081000ULL,
0x21200680100081ULL,
0x20100080080080ULL,
0x2000a00200410ULL,
0x20080800400ULL,
0x80088400100102ULL,
0x80004600042881ULL,
0x4040008040800020ULL,
0x440003000200801ULL,
0x4200011004500ULL,
0x188020010100100ULL,
0x14800401802800ULL,
0x2080040080800200ULL,
0x124080204001001ULL,
0x200046502000484ULL,
0x480400080088020ULL,
0x1000422010034000ULL,
0x30200100110040ULL,
0x100021010009ULL,
0x2002080100110004ULL,
0x202008004008002ULL,
0x20020004010100ULL,
0x2048440040820001ULL,
0x101002200408200ULL,
0x40802000401080ULL,