-
Notifications
You must be signed in to change notification settings - Fork 4
/
movegen.h
1274 lines (1115 loc) · 50.4 KB
/
movegen.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
/*
MIT License
Copyright(c) 2016-2017 Judd Niemann
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//////////////////////////////////////////////
// movegen.h //
// Defines: //
// BitBoard, class Move,
// class ChessPosition, //
// Move Generation Functions //
// Bitboard fill functions //
// Piece codes and other constants //
//////////////////////////////////////////////
#ifndef _MOVEGEN
#define _MOVEGEN 1
#define _CRT_SECURE_NO_WARNINGS 1
#ifdef _MSC_VER
#include <intrin.h>
#else
#include <x86intrin.h>
#endif
#include <cstdint>
namespace juddperft {
// Build Options:
#define _USE_HASH 1 // if undefined, entire hash table system will be excluded from build
//
#define _USE_BITSCAN_INSTRUCTIONS 1 // if defined, use x86-64 BSR and BSF instructions (Only available on x86-64)
#define _USE_POPCNT_INSTRUCTION 1 // if defined, use popcnt instruction (Intel: Nehalem or Higher, AMD: Barcelona or Higher)
// #define _USE_BITTEST_INSTRUCTION 1 // if defined, use the BT instruction (all Intels)
// #define _FLAG_CHECKS_IN_MOVE_GENERATION 1 // Move generator will set "Check" flag in moves which put enemy in check (not needed for perft)
#ifndef NULL
#define NULL 0
#endif
#ifndef SMALL_BUFFER_SIZE
#define SMALL_BUFFER_SIZE 64
#endif
#define MOVELIST_SIZE 128
#define CHECKMATE 9999
#define STALEMATE -1
#define WPAWN 1
#define WBISHOP 2
#define WENPASSANT 3
#define WROOK 4
#define WKNIGHT 5
#define WQUEEN 6
#define WKING 7
#define BPAWN 9
#define BBISHOP 10
#define BENPASSANT 11
#define BROOK 12
#define BKNIGHT 13
#define BQUEEN 14
#define BKING 15
typedef uint64_t BitBoard;
typedef uint64_t HashKey;
// class Move: This is the Long-Format, in which the From and To squares are represented by Bitboards.
// This is the format that is used throughout the full chess engine. The downside is that for recursive functions like perft,
// an array of these uses a lot of stack space, and slows things down. Hence, the more compact version below is used.
class Move
{
public:
BitBoard From;
BitBoard To;
union{
struct{
uint32_t BlackToMove : 1;
uint32_t Check : 1;
uint32_t Capture : 1;
uint32_t EnPassantCapture : 1;
uint32_t DoublePawnMove : 1;
uint32_t Castle : 1;
uint32_t CastleLong : 1;
uint32_t PromoteKnight : 1;
uint32_t PromoteBishop : 1;
uint32_t PromoteRook : 1;
uint32_t PromoteQueen : 1;
uint32_t Unused : 7;
uint32_t IllegalMove : 1;
uint32_t NoMoreMoves : 1;
uint32_t Piece : 4;
uint32_t MoveCount : 8;
};
uint32_t Flags;
};
public:
Move(BitBoard From=0,BitBoard To=0,uint32_t Flags=0);
bool operator==(const Move & B) const;
// Move & operator=(const Move & M);
Move& format(
BitBoard From,
BitBoard To,
uint32_t BlackToMove=0,
uint32_t Piece=0,
uint32_t Flags=0
);
void ClearFlags() {
Move::Flags = 0;
}
};
// ChessMove{} - Compact move format packed into 64 bits
// From and To squares represented by unsigned char square index
// to-do: try to pack it into 32 bits ?
struct ChessMove {
union {
struct {
uint32_t BlackToMove : 1;
uint32_t Check : 1;
uint32_t Capture : 1;
uint32_t EnPassantCapture : 1;
uint32_t DoublePawnMove : 1;
uint32_t Castle : 1;
uint32_t CastleLong : 1;
uint32_t PromoteKnight : 1;
uint32_t PromoteBishop : 1;
uint32_t PromoteRook : 1;
uint32_t PromoteQueen : 1;
uint32_t Unused : 7;
uint32_t IllegalMove : 1;
uint32_t NoMoreMoves : 1;
uint32_t Piece : 4;
uint32_t MoveCount : 8;
};
uint32_t Flags;
};
unsigned char FromSquare;
unsigned char ToSquare;
};
class ChessPosition
{
/* --------------------------------------------------------------
Explanation of Bit-Representation used in positions:
D (msb): Colour (1=Black)
C 1=Straight-Moving Piece (Q or R)
B 1=Diagonal-moving piece (Q or B)
A (lsb): 1=Pawn
D C B A
(0) 0 0 0 0 Empty Square
(1) 0 0 0 1 White Pawn
(2) 0 0 1 0 White Bishop
(3) 0 0 1 1 White En passant Square
(4) 0 1 0 0 White Rook
(5) 0 1 0 1 White Knight
(6) 0 1 1 0 White Queen
(7) 0 1 1 1 White King
(8) 1 0 0 0 Reserved-Don't use (Black empty square)
(9) 1 0 0 1 Black Pawn
(10) 1 0 1 0 Black Bishop
(11) 1 0 1 1 Black En passant Square
(12) 1 1 0 0 Black Rook
(13) 1 1 0 1 Black Knight
(14) 1 1 1 0 Black Queen
(15) 1 1 1 1 Black King
---------------------------------------------------------------*/
public:
BitBoard A;
BitBoard B;
BitBoard C;
BitBoard D;
union{
struct{
uint32_t BlackToMove : 1;
uint32_t WhiteCanCastle : 1;
uint32_t WhiteCanCastleLong : 1;
uint32_t BlackCanCastle : 1;
uint32_t BlackCanCastleLong : 1;
uint32_t WhiteForfeitedCastle : 1;
uint32_t WhiteForfeitedCastleLong : 1;
uint32_t BlackForfeitedCastle : 1;
uint32_t BlackForfeitedCastleLong : 1;
uint32_t WhiteDidCastle : 1;
uint32_t WhiteDidCastleLong : 1;
uint32_t BlackDidCastle : 1;
uint32_t BlackDidCastleLong : 1;
uint32_t CheckmateWhite;
uint32_t CheckmateBlack;
};
uint32_t Flags;
};
uint16_t MoveNumber;
uint16_t HalfMoves;
int material;
bool operator==(const ChessPosition& Q) {
return((ChessPosition::A == Q.A) && (ChessPosition::B == Q.B) && (ChessPosition::C == Q.C) && (ChessPosition::D == Q.D) && (ChessPosition::Flags == Q.Flags));
}
#ifdef _USE_HASH
HashKey HK;
#endif
public:
ChessPosition();
ChessPosition& setupStartPosition();
#ifdef _USE_HASH
ChessPosition& calculateHash();
#endif
ChessPosition & setPieceAtSquare(const unsigned int & piece, BitBoard square);
uint32_t getPieceAtSquare(const BitBoard & square) const;
ChessPosition& calculateMaterial();
ChessPosition& performMove(ChessMove M);
void switchSides();
void clear(void);
// ChessPosition& operator=(const ChessPosition& P);
private:
};
/////////////////////////////////////////////
// Move notation styles:
// ---------------------
// LongAlgebraic: e7xf8(q)
// StandardAlgebraic: exf(q)
// CoOrdinate: e7f8q
/////////////////////////////////////////////
enum MoveNotationStyle{
LongAlgebraic,
StandardAlgebraic,
CoOrdinate,
Diagnostic,
LongAlgebraicNoNewline
};
// Move-Generation (and verification)
void generateMoves(const ChessPosition & P, ChessMove * pM);
bool isInCheck(const ChessPosition& P, bool bIsBlack);
// White Move-Generation Functions:
void genWhiteMoves(const ChessPosition& P, ChessMove*);
inline BitBoard genBlackAttacks(const ChessPosition& Z);
BitBoard isWhiteInCheck(const ChessPosition & Z);
void addWhiteMoveToListIfLegal(const ChessPosition & P, ChessMove *& pM, unsigned char fromsquare, BitBoard to, int32_t piece, int32_t flags=0);
void addWhitePromotionsToListIfLegal(const ChessPosition & P, ChessMove *& pM, unsigned char fromsquare, BitBoard to, int32_t piece, int32_t flags=0);
// Black Move-Generation Functions:
void genBlackMoves(const ChessPosition& P, ChessMove*);
inline BitBoard genWhiteAttacks(const ChessPosition& Z);
BitBoard isBlackInCheck(const ChessPosition & Z);
void addBlackMoveToListIfLegal(const ChessPosition & P, ChessMove *& pM, unsigned char fromsquare, BitBoard to, int32_t piece, int32_t flags=0);
void addBlackPromotionsToListIfLegal(const ChessPosition & P, ChessMove *& pM, unsigned char fromsquare, BitBoard to, int32_t piece, int32_t flags=0);
// Dump I/O functions:
void dumpBitBoard(BitBoard b);
void dumpChessPosition(ChessPosition p);
void dumpMove(ChessMove M, MoveNotationStyle style = LongAlgebraic, char* pBuffer = NULL);
void dumpMoveList(ChessMove * pMoveList, MoveNotationStyle style=LongAlgebraic, char * pBuffer=NULL);
// Compound Bitboard Fill operations:
BitBoard fillStraightAttacksOccluded(BitBoard g, BitBoard p);
BitBoard fillDiagonalAttacksOccluded(BitBoard g, BitBoard p);
BitBoard fillKingAttacksOccluded(BitBoard g, BitBoard p);
BitBoard fillKingAttacks(BitBoard g);
BitBoard fillKnightAttacksOccluded(BitBoard g, BitBoard p);
// Fill and Move Bitboard Operations:
BitBoard fillRightOccluded(BitBoard g, BitBoard p);
BitBoard fillLeftOccluded(BitBoard g, BitBoard p);
BitBoard fillUpOccluded(BitBoard g, BitBoard p);
BitBoard fillDownOccluded(BitBoard g, BitBoard p);
BitBoard fillUpRightOccluded(BitBoard g, BitBoard p);
BitBoard fillDownRightOccluded(BitBoard g, BitBoard p);
BitBoard fillDownLeftOccluded(BitBoard g, BitBoard p);
BitBoard fillUpLeftOccluded(BitBoard g, BitBoard p);
BitBoard moveUpSingleOccluded(BitBoard g, BitBoard p);
BitBoard moveUpRightSingleOccluded(BitBoard g, BitBoard p);
BitBoard moveRightSingleOccluded(BitBoard g, BitBoard p);
BitBoard moveDownRightSingleOccluded(BitBoard g, BitBoard p);
BitBoard moveDownSingleOccluded(BitBoard g, BitBoard p);
BitBoard moveDownLeftSingleOccluded(BitBoard g, BitBoard p);
BitBoard moveLeftSingleOccluded(BitBoard g, BitBoard p);
BitBoard moveUpLeftSingleOccluded(BitBoard g, BitBoard p);
BitBoard moveKnight1Occluded(BitBoard g, BitBoard p);
BitBoard moveKnight2Occluded(BitBoard g, BitBoard p);
BitBoard moveKnight3Occluded(BitBoard g, BitBoard p);
BitBoard moveKnight4Occluded(BitBoard g, BitBoard p);
BitBoard moveKnight5Occluded(BitBoard g, BitBoard p);
BitBoard moveKnight6Occluded(BitBoard g, BitBoard p);
BitBoard moveKnight7Occluded(BitBoard g, BitBoard p);
BitBoard moveKnight8Occluded(BitBoard g, BitBoard p);
/* --------------------------------------------------------------
Deafault material values of Pieces:
---------------------------------------------------------------*/
const int pieceMaterialValue[16] =
{
0, // Empty Square
100, // White pawn
300, // White Bishop
0, // White En passant Square
500, // White Rook
300, // White Knight
900, // White Queen
0, // White King
0, // Unused
-100, // Black Pawn
-300, // Black Bishop
0, // Black En passant Square
-500, // Black Rook
-300, // Black Knight
-900 // Black Queen
,0 // Black King
};
// Various Bitboard configurations:
#define RANK1 0x00000000000000ff
#define RANK2 0x000000000000ff00
#define RANK3 0x0000000000ff0000
#define RANK4 0x00000000ff000000
#define RANK5 0x000000ff00000000
#define RANK6 0x0000ff0000000000
#define RANK7 0x00ff000000000000
#define RANK8 0xff00000000000000
#define FILEA 0x8080808080808080
#define FILEB 0x4040404040404040
#define FILEC 0x2020202020202020
#define FILED 0x1010101010101010
#define FILEE 0x0808080808080808
#define FILEF 0x0404040404040404
#define FILEG 0x0202020202020202
#define FILEH 0x0101010101010101
#define A1 0x0000000000000080
#define B1 0x0000000000000040
#define C1 0x0000000000000020
#define D1 0x0000000000000010
#define E1 0x0000000000000008
#define F1 0x0000000000000004
#define G1 0x0000000000000002
#define H1 0x0000000000000001
#define D4 0x0000000010000000
#define E4 0x0000000008000000
#define D5 0x0000000100000000
#define E5 0x0000000080000000
#define A8 0x8000000000000000
#define B8 0x4000000000000000
#define C8 0x2000000000000000
#define D8 0x1000000000000000
#define E8 0x0800000000000000
#define F8 0x0400000000000000
#define G8 0x0200000000000000
#define H8 0x0100000000000000
#define RIM 0xff818181818181ff
#define UPPER_CENTRE 0x0000001800000000
#define LOWER_CENTRE 0x0000000018000000
#define WHITECASTLEZONE 0x0000000000000006
#define BLACKCASTLEZONE 0x0600000000000000
#define WHITECASTLELONGZONE 0x0000000000000070
#define BLACKCASTLELONGZONE 0x7000000000000000
#define WHITEOUTPOSTZONE 0x0000ffffff000000
#define BLACKOUTPOSTZONE 0x000000ffffff0000
#define WHITEQRPOS 0x0000000000000080
#define WHITEQNPOS 0x0000000000000040
#define WHITEQBPOS 0x0000000000000020
#define WHITEQUEENPOS 0x0000000000000010
#define WHITEKINGPOS 0x0000000000000008
#define WHITEKBPOS 0x0000000000000004
#define WHITEKNPOS 0x0000000000000002
#define WHITEKRPOS 0x0000000000000001
#define BLACKQRPOS 0x8000000000000000
#define BLACKQNPOS 0x4000000000000000
#define BLACKQBPOS 0x2000000000000000
#define BLACKQUEENPOS 0x1000000000000000
#define BLACKKINGPOS 0x0800000000000000
#define BLACKKBPOS 0x0400000000000000
#define BLACKKNPOS 0x0200000000000000
#define BLACKKRPOS 0x0100000000000000
const BitBoard LEFTMASK =0xfefefefefefefefe;
const BitBoard RIGHTMASK=0x7f7f7f7f7f7f7f7f;
// Pre-calculated move tables: BitBoards
const BitBoard MoveUp[64] = {
0x0000000000000100, 0x0000000000000200, 0x0000000000000400, 0x0000000000000800, 0x00000000001000, 0x0000000000002000, 0x0000000000004000, 0x0000000000008000,
0x0000000000010000, 0x0000000000020000, 0x0000000000040000, 0x0000000000080000, 0x00000000100000, 0x0000000000200000, 0x0000000000400000, 0x0000000000800000,
0x0000000001000000, 0x0000000002000000, 0x0000000004000000, 0x0000000008000000, 0x00000010000000, 0x0000000020000000, 0x0000000040000000, 0x0000000080000000,
0x0000000100000000, 0x0000000200000000, 0x0000000400000000, 0x0000000800000000, 0x00001000000000, 0x0000002000000000, 0x0000004000000000, 0x0000008000000000,
0x0000010000000000, 0x0000020000000000, 0x0000040000000000, 0x0000080000000000, 0x00100000000000, 0x0000200000000000, 0x0000400000000000, 0x0000800000000000,
0x0001000000000000, 0x0002000000000000, 0x0004000000000000, 0x0008000000000000, 0x10000000000000, 0x0020000000000000, 0x0040000000000000, 0x0080000000000000,
0x0100000000000000, 0x0200000000000000, 0x0400000000000000, 0x0800000000000000, 0x1000000000000000, 0x2000000000000000, 0x4000000000000000, 0x8000000000000000,
000000000000000000, 000000000000000000, 000000000000000000, 000000000000000000, 0000000000000000, 000000000000000000, 000000000000000000, 000000000000000000
};
const BitBoard MoveUpRight[64] = {
000000000000000000, 0x0000000000000100, 0x0000000000000200, 0x0000000000000400, 0x00000000000800, 0x0000000000001000, 0x0000000000002000, 0x0000000000004000,
000000000000000000, 0x0000000000010000, 0x0000000000020000, 0x0000000000040000, 0x00000000080000, 0x0000000000100000, 0x0000000000200000, 0x0000000000400000,
000000000000000000, 0x0000000001000000, 0x0000000002000000, 0x0000000004000000, 0x00000008000000, 0x0000000010000000, 0x0000000020000000, 0x0000000040000000,
000000000000000000, 0x0000000100000000, 0x0000000200000000, 0x0000000400000000, 0x00000800000000, 0x0000001000000000, 0x0000002000000000, 0x0000004000000000,
000000000000000000, 0x0000010000000000, 0x0000020000000000, 0x0000040000000000, 0x00080000000000, 0x0000100000000000, 0x0000200000000000, 0x0000400000000000,
000000000000000000, 0x0001000000000000, 0x0002000000000000, 0x0004000000000000, 0x08000000000000, 0x0010000000000000, 0x0020000000000000, 0x0040000000000000,
000000000000000000, 0x0100000000000000, 0x0200000000000000, 0x0400000000000000, 0x800000000000000, 0x1000000000000000, 0x2000000000000000, 0x4000000000000000,
000000000000000000, 000000000000000000, 000000000000000000, 000000000000000000, 0000000000000000, 000000000000000000, 000000000000000000, 000000000000000000
};
const BitBoard MoveRight[64] = {
000000000000000000, 0x0000000000000001, 0x0000000000000002, 0x0000000000000004, 0x00000000000008, 0x0000000000000010, 0x0000000000000020, 0x0000000000000040,
000000000000000000, 0x0000000000000100, 0x0000000000000200, 0x0000000000000400, 0x00000000000800, 0x0000000000001000, 0x0000000000002000, 0x0000000000004000,
000000000000000000, 0x0000000000010000, 0x0000000000020000, 0x0000000000040000, 0x00000000080000, 0x0000000000100000, 0x0000000000200000, 0x0000000000400000,
000000000000000000, 0x0000000001000000, 0x0000000002000000, 0x0000000004000000, 0x00000008000000, 0x0000000010000000, 0x0000000020000000, 0x0000000040000000,
000000000000000000, 0x0000000100000000, 0x0000000200000000, 0x0000000400000000, 0x00000800000000, 0x0000001000000000, 0x0000002000000000, 0x0000004000000000,
000000000000000000, 0x0000010000000000, 0x0000020000000000, 0x0000040000000000, 0x00080000000000, 0x0000100000000000, 0x0000200000000000, 0x0000400000000000,
000000000000000000, 0x0001000000000000, 0x0002000000000000, 0x0004000000000000, 0x08000000000000, 0x0010000000000000, 0x0020000000000000, 0x0040000000000000,
000000000000000000, 0x0100000000000000, 0x0200000000000000, 0x0400000000000000, 0x800000000000000, 0x1000000000000000, 0x2000000000000000, 0x4000000000000000
};
const BitBoard MoveDownRight[64] = {
000000000000000000, 000000000000000000, 000000000000000000, 000000000000000000, 0000000000000000, 000000000000000000, 000000000000000000, 000000000000000000,
000000000000000000, 0x0000000000000001, 0x0000000000000002, 0x0000000000000004, 0x00000000000008, 0x0000000000000010, 0x0000000000000020, 0x0000000000000040,
000000000000000000, 0x0000000000000100, 0x0000000000000200, 0x0000000000000400, 0x00000000000800, 0x0000000000001000, 0x0000000000002000, 0x0000000000004000,
000000000000000000, 0x0000000000010000, 0x0000000000020000, 0x0000000000040000, 0x00000000080000, 0x0000000000100000, 0x0000000000200000, 0x0000000000400000,
000000000000000000, 0x0000000001000000, 0x0000000002000000, 0x0000000004000000, 0x00000008000000, 0x0000000010000000, 0x0000000020000000, 0x0000000040000000,
000000000000000000, 0x0000000100000000, 0x0000000200000000, 0x0000000400000000, 0x00000800000000, 0x0000001000000000, 0x0000002000000000, 0x0000004000000000,
000000000000000000, 0x0000010000000000, 0x0000020000000000, 0x0000040000000000, 0x00080000000000, 0x0000100000000000, 0x0000200000000000, 0x0000400000000000,
000000000000000000, 0x0001000000000000, 0x0002000000000000, 0x0004000000000000, 0x08000000000000, 0x0010000000000000, 0x0020000000000000, 0x0040000000000000
};
const BitBoard MoveDown[64] = {
000000000000000000, 000000000000000000, 000000000000000000, 000000000000000000, 0000000000000000, 000000000000000000, 000000000000000000, 000000000000000000,
0x0000000000000001, 0x0000000000000002, 0x0000000000000004, 0x0000000000000008, 0x00000000000010, 0x0000000000000020, 0x0000000000000040, 0x0000000000000080,
0x0000000000000100, 0x0000000000000200, 0x0000000000000400, 0x0000000000000800, 0x00000000001000, 0x0000000000002000, 0x0000000000004000, 0x0000000000008000,
0x0000000000010000, 0x0000000000020000, 0x0000000000040000, 0x0000000000080000, 0x00000000100000, 0x0000000000200000, 0x0000000000400000, 0x0000000000800000,
0x0000000001000000, 0x0000000002000000, 0x0000000004000000, 0x0000000008000000, 0x00000010000000, 0x0000000020000000, 0x0000000040000000, 0x0000000080000000,
0x0000000100000000, 0x0000000200000000, 0x0000000400000000, 0x0000000800000000, 0x00001000000000, 0x0000002000000000, 0x0000004000000000, 0x0000008000000000,
0x0000010000000000, 0x0000020000000000, 0x0000040000000000, 0x0000080000000000, 0x00100000000000, 0x0000200000000000, 0x0000400000000000, 0x0000800000000000,
0x0001000000000000, 0x0002000000000000, 0x0004000000000000, 0x0008000000000000, 0x10000000000000, 0x0020000000000000, 0x0040000000000000, 0x0080000000000000
};
const BitBoard MoveDownLeft[64] = {
000000000000000000, 000000000000000000, 000000000000000000, 000000000000000000, 0000000000000000, 000000000000000000, 000000000000000000, 000000000000000000,
0x0000000000000002, 0x0000000000000004, 0x0000000000000008, 0x0000000000000010, 0x00000000000020, 0x0000000000000040, 0x0000000000000080, 000000000000000000,
0x0000000000000200, 0x0000000000000400, 0x0000000000000800, 0x0000000000001000, 0x00000000002000, 0x0000000000004000, 0x0000000000008000, 000000000000000000,
0x0000000000020000, 0x0000000000040000, 0x0000000000080000, 0x0000000000100000, 0x00000000200000, 0x0000000000400000, 0x0000000000800000, 000000000000000000,
0x0000000002000000, 0x0000000004000000, 0x0000000008000000, 0x0000000010000000, 0x00000020000000, 0x0000000040000000, 0x0000000080000000, 000000000000000000,
0x0000000200000000, 0x0000000400000000, 0x0000000800000000, 0x0000001000000000, 0x00002000000000, 0x0000004000000000, 0x0000008000000000, 000000000000000000,
0x0000020000000000, 0x0000040000000000, 0x0000080000000000, 0x0000100000000000, 0x00200000000000, 0x0000400000000000, 0x0000800000000000, 000000000000000000,
0x0002000000000000, 0x0004000000000000, 0x0008000000000000, 0x0010000000000000, 0x20000000000000, 0x0040000000000000, 0x0080000000000000, 000000000000000000
};
const BitBoard MoveLeft[64] = {
0x0000000000000002, 0x0000000000000004, 0x0000000000000008, 0x0000000000000010, 0x00000000000020, 0x0000000000000040, 0x0000000000000080, 000000000000000000,
0x0000000000000200, 0x0000000000000400, 0x0000000000000800, 0x0000000000001000, 0x00000000002000, 0x0000000000004000, 0x0000000000008000, 000000000000000000,
0x0000000000020000, 0x0000000000040000, 0x0000000000080000, 0x0000000000100000, 0x00000000200000, 0x0000000000400000, 0x0000000000800000, 000000000000000000,
0x0000000002000000, 0x0000000004000000, 0x0000000008000000, 0x0000000010000000, 0x00000020000000, 0x0000000040000000, 0x0000000080000000, 000000000000000000,
0x0000000200000000, 0x0000000400000000, 0x0000000800000000, 0x0000001000000000, 0x00002000000000, 0x0000004000000000, 0x0000008000000000, 000000000000000000,
0x0000020000000000, 0x0000040000000000, 0x0000080000000000, 0x0000100000000000, 0x00200000000000, 0x0000400000000000, 0x0000800000000000, 000000000000000000,
0x0002000000000000, 0x0004000000000000, 0x0008000000000000, 0x0010000000000000, 0x20000000000000, 0x0040000000000000, 0x0080000000000000, 000000000000000000,
0x0200000000000000, 0x0400000000000000, 0x0800000000000000, 0x1000000000000000, 0x2000000000000000, 0x4000000000000000, 0x8000000000000000, 000000000000000000
};
const BitBoard MoveUpLeft[64] = {
0x0000000000000200, 0x0000000000000400, 0x0000000000000800, 0x0000000000001000, 0x00000000002000, 0x0000000000004000, 0x0000000000008000, 000000000000000000,
0x0000000000020000, 0x0000000000040000, 0x0000000000080000, 0x0000000000100000, 0x00000000200000, 0x0000000000400000, 0x0000000000800000, 000000000000000000,
0x0000000002000000, 0x0000000004000000, 0x0000000008000000, 0x0000000010000000, 0x00000020000000, 0x0000000040000000, 0x0000000080000000, 000000000000000000,
0x0000000200000000, 0x0000000400000000, 0x0000000800000000, 0x0000001000000000, 0x00002000000000, 0x0000004000000000, 0x0000008000000000, 000000000000000000,
0x0000020000000000, 0x0000040000000000, 0x0000080000000000, 0x0000100000000000, 0x00200000000000, 0x0000400000000000, 0x0000800000000000, 000000000000000000,
0x0002000000000000, 0x0004000000000000, 0x0008000000000000, 0x0010000000000000, 0x20000000000000, 0x0040000000000000, 0x0080000000000000, 000000000000000000,
0x0200000000000000, 0x0400000000000000, 0x0800000000000000, 0x1000000000000000, 0x2000000000000000, 0x4000000000000000, 0x8000000000000000, 000000000000000000,
000000000000000000, 000000000000000000, 000000000000000000, 000000000000000000, 0000000000000000, 000000000000000000, 000000000000000000, 000000000000000000
};
const BitBoard MoveKnight1[64] = {
000000000000000000, 0x0000000000010000, 0x0000000000020000, 0x0000000000040000, 0x00000000080000, 0x0000000000100000, 0x0000000000200000, 0x0000000000400000,
000000000000000000, 0x0000000001000000, 0x0000000002000000, 0x0000000004000000, 0x00000008000000, 0x0000000010000000, 0x0000000020000000, 0x0000000040000000,
000000000000000000, 0x0000000100000000, 0x0000000200000000, 0x0000000400000000, 0x00000800000000, 0x0000001000000000, 0x0000002000000000, 0x0000004000000000,
000000000000000000, 0x0000010000000000, 0x0000020000000000, 0x0000040000000000, 0x00080000000000, 0x0000100000000000, 0x0000200000000000, 0x0000400000000000,
000000000000000000, 0x0001000000000000, 0x0002000000000000, 0x0004000000000000, 0x08000000000000, 0x0010000000000000, 0x0020000000000000, 0x0040000000000000,
000000000000000000, 0x0100000000000000, 0x0200000000000000, 0x0400000000000000, 0x800000000000000, 0x1000000000000000, 0x2000000000000000, 0x4000000000000000,
000000000000000000, 000000000000000000, 000000000000000000, 000000000000000000, 0000000000000000, 000000000000000000, 000000000000000000, 000000000000000000,
000000000000000000, 000000000000000000, 000000000000000000, 000000000000000000, 0000000000000000, 000000000000000000, 000000000000000000, 000000000000000000
};
const BitBoard MoveKnight2[64] = {
000000000000000000, 000000000000000000, 0x0000000000000100, 0x0000000000000200, 0x00000000000400, 0x0000000000000800, 0x0000000000001000, 0x0000000000002000,
000000000000000000, 000000000000000000, 0x0000000000010000, 0x0000000000020000, 0x00000000040000, 0x0000000000080000, 0x0000000000100000, 0x0000000000200000,
000000000000000000, 000000000000000000, 0x0000000001000000, 0x0000000002000000, 0x00000004000000, 0x0000000008000000, 0x0000000010000000, 0x0000000020000000,
000000000000000000, 000000000000000000, 0x0000000100000000, 0x0000000200000000, 0x00000400000000, 0x0000000800000000, 0x0000001000000000, 0x0000002000000000,
000000000000000000, 000000000000000000, 0x0000010000000000, 0x0000020000000000, 0x00040000000000, 0x0000080000000000, 0x0000100000000000, 0x0000200000000000,
000000000000000000, 000000000000000000, 0x0001000000000000, 0x0002000000000000, 0x04000000000000, 0x0008000000000000, 0x0010000000000000, 0x0020000000000000,
000000000000000000, 000000000000000000, 0x0100000000000000, 0x0200000000000000, 0x400000000000000, 0x0800000000000000, 0x1000000000000000, 0x2000000000000000,
000000000000000000, 000000000000000000, 000000000000000000, 000000000000000000, 0000000000000000, 000000000000000000, 000000000000000000, 000000000000000000
};
const BitBoard MoveKnight3[64] = {
000000000000000000, 000000000000000000, 000000000000000000, 000000000000000000, 0000000000000000, 000000000000000000, 000000000000000000, 000000000000000000,
000000000000000000, 000000000000000000, 0x0000000000000001, 0x0000000000000002, 0x00000000000004, 0x0000000000000008, 0x0000000000000010, 0x0000000000000020,
000000000000000000, 000000000000000000, 0x0000000000000100, 0x0000000000000200, 0x00000000000400, 0x0000000000000800, 0x0000000000001000, 0x0000000000002000,
000000000000000000, 000000000000000000, 0x0000000000010000, 0x0000000000020000, 0x00000000040000, 0x0000000000080000, 0x0000000000100000, 0x0000000000200000,
000000000000000000, 000000000000000000, 0x0000000001000000, 0x0000000002000000, 0x00000004000000, 0x0000000008000000, 0x0000000010000000, 0x0000000020000000,
000000000000000000, 000000000000000000, 0x0000000100000000, 0x0000000200000000, 0x00000400000000, 0x0000000800000000, 0x0000001000000000, 0x0000002000000000,
000000000000000000, 000000000000000000, 0x0000010000000000, 0x0000020000000000, 0x00040000000000, 0x0000080000000000, 0x0000100000000000, 0x0000200000000000,
000000000000000000, 000000000000000000, 0x0001000000000000, 0x0002000000000000, 0x04000000000000, 0x0008000000000000, 0x0010000000000000, 0x0020000000000000
};
const BitBoard MoveKnight4[64] = {
000000000000000000, 000000000000000000, 000000000000000000, 000000000000000000, 0000000000000000, 000000000000000000, 000000000000000000, 000000000000000000,
000000000000000000, 000000000000000000, 000000000000000000, 000000000000000000, 0000000000000000, 000000000000000000, 000000000000000000, 000000000000000000,
000000000000000000, 0x0000000000000001, 0x0000000000000002, 0x0000000000000004, 0x00000000000008, 0x0000000000000010, 0x0000000000000020, 0x0000000000000040,
000000000000000000, 0x0000000000000100, 0x0000000000000200, 0x0000000000000400, 0x00000000000800, 0x0000000000001000, 0x0000000000002000, 0x0000000000004000,
000000000000000000, 0x0000000000010000, 0x0000000000020000, 0x0000000000040000, 0x00000000080000, 0x0000000000100000, 0x0000000000200000, 0x0000000000400000,
000000000000000000, 0x0000000001000000, 0x0000000002000000, 0x0000000004000000, 0x00000008000000, 0x0000000010000000, 0x0000000020000000, 0x0000000040000000,
000000000000000000, 0x0000000100000000, 0x0000000200000000, 0x0000000400000000, 0x00000800000000, 0x0000001000000000, 0x0000002000000000, 0x0000004000000000,
000000000000000000, 0x0000010000000000, 0x0000020000000000, 0x0000040000000000, 0x00080000000000, 0x0000100000000000, 0x0000200000000000, 0x0000400000000000
};
const BitBoard MoveKnight5[64] = {
000000000000000000, 000000000000000000, 000000000000000000, 000000000000000000, 0000000000000000, 000000000000000000, 000000000000000000, 000000000000000000,
000000000000000000, 000000000000000000, 000000000000000000, 000000000000000000, 0000000000000000, 000000000000000000, 000000000000000000, 000000000000000000,
0x0000000000000002, 0x0000000000000004, 0x0000000000000008, 0x0000000000000010, 0x00000000000020, 0x0000000000000040, 0x0000000000000080, 000000000000000000,
0x0000000000000200, 0x0000000000000400, 0x0000000000000800, 0x0000000000001000, 0x00000000002000, 0x0000000000004000, 0x0000000000008000, 000000000000000000,
0x0000000000020000, 0x0000000000040000, 0x0000000000080000, 0x0000000000100000, 0x00000000200000, 0x0000000000400000, 0x0000000000800000, 000000000000000000,
0x0000000002000000, 0x0000000004000000, 0x0000000008000000, 0x0000000010000000, 0x00000020000000, 0x0000000040000000, 0x0000000080000000, 000000000000000000,
0x0000000200000000, 0x0000000400000000, 0x0000000800000000, 0x0000001000000000, 0x00002000000000, 0x0000004000000000, 0x0000008000000000, 000000000000000000,
0x0000020000000000, 0x0000040000000000, 0x0000080000000000, 0x0000100000000000, 0x00200000000000, 0x0000400000000000, 0x0000800000000000, 000000000000000000
};
const BitBoard MoveKnight6[64] = {
000000000000000000, 000000000000000000, 000000000000000000, 000000000000000000, 0000000000000000, 000000000000000000, 000000000000000000, 000000000000000000,
0x0000000000000004, 0x0000000000000008, 0x0000000000000010, 0x0000000000000020, 0x00000000000040, 0x0000000000000080, 000000000000000000, 000000000000000000,
0x0000000000000400, 0x0000000000000800, 0x0000000000001000, 0x0000000000002000, 0x00000000004000, 0x0000000000008000, 000000000000000000, 000000000000000000,
0x0000000000040000, 0x0000000000080000, 0x0000000000100000, 0x0000000000200000, 0x00000000400000, 0x0000000000800000, 000000000000000000, 000000000000000000,
0x0000000004000000, 0x0000000008000000, 0x0000000010000000, 0x0000000020000000, 0x00000040000000, 0x0000000080000000, 000000000000000000, 000000000000000000,
0x0000000400000000, 0x0000000800000000, 0x0000001000000000, 0x0000002000000000, 0x00004000000000, 0x0000008000000000, 000000000000000000, 000000000000000000,
0x0000040000000000, 0x0000080000000000, 0x0000100000000000, 0x0000200000000000, 0x00400000000000, 0x0000800000000000, 000000000000000000, 000000000000000000,
0x0004000000000000, 0x0008000000000000, 0x0010000000000000, 0x0020000000000000, 0x40000000000000, 0x0080000000000000, 000000000000000000, 000000000000000000
};
const BitBoard MoveKnight7[64] = {
0x0000000000000400, 0x0000000000000800, 0x0000000000001000, 0x0000000000002000, 0x00000000004000, 0x0000000000008000, 000000000000000000, 000000000000000000,
0x0000000000040000, 0x0000000000080000, 0x0000000000100000, 0x0000000000200000, 0x00000000400000, 0x0000000000800000, 000000000000000000, 000000000000000000,
0x0000000004000000, 0x0000000008000000, 0x0000000010000000, 0x0000000020000000, 0x00000040000000, 0x0000000080000000, 000000000000000000, 000000000000000000,
0x0000000400000000, 0x0000000800000000, 0x0000001000000000, 0x0000002000000000, 0x00004000000000, 0x0000008000000000, 000000000000000000, 000000000000000000,
0x0000040000000000, 0x0000080000000000, 0x0000100000000000, 0x0000200000000000, 0x00400000000000, 0x0000800000000000, 000000000000000000, 000000000000000000,
0x0004000000000000, 0x0008000000000000, 0x0010000000000000, 0x0020000000000000, 0x40000000000000, 0x0080000000000000, 000000000000000000, 000000000000000000,
0x0400000000000000, 0x0800000000000000, 0x1000000000000000, 0x2000000000000000, 0x4000000000000000, 0x8000000000000000, 000000000000000000, 000000000000000000,
000000000000000000, 000000000000000000, 000000000000000000, 000000000000000000, 0000000000000000, 000000000000000000, 000000000000000000, 000000000000000000
};
const BitBoard MoveKnight8[64] = {
0x0000000000020000, 0x0000000000040000, 0x0000000000080000, 0x0000000000100000, 0x00000000200000, 0x0000000000400000, 0x0000000000800000, 000000000000000000,
0x0000000002000000, 0x0000000004000000, 0x0000000008000000, 0x0000000010000000, 0x00000020000000, 0x0000000040000000, 0x0000000080000000, 000000000000000000,
0x0000000200000000, 0x0000000400000000, 0x0000000800000000, 0x0000001000000000, 0x00002000000000, 0x0000004000000000, 0x0000008000000000, 000000000000000000,
0x0000020000000000, 0x0000040000000000, 0x0000080000000000, 0x0000100000000000, 0x00200000000000, 0x0000400000000000, 0x0000800000000000, 000000000000000000,
0x0002000000000000, 0x0004000000000000, 0x0008000000000000, 0x0010000000000000, 0x20000000000000, 0x0040000000000000, 0x0080000000000000, 000000000000000000,
0x0200000000000000, 0x0400000000000000, 0x0800000000000000, 0x1000000000000000, 0x2000000000000000, 0x4000000000000000, 0x8000000000000000, 000000000000000000,
000000000000000000, 000000000000000000, 000000000000000000, 000000000000000000, 0000000000000000, 000000000000000000, 000000000000000000, 000000000000000000,
000000000000000000, 000000000000000000, 000000000000000000, 000000000000000000, 0000000000000000, 000000000000000000, 000000000000000000, 000000000000000000
};
// Pre-Calculated Move tables: Square-Index
const int MoveUpSquareIndex[64] = {
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,
56, 57, 58, 59, 60, 61, 62, 63
};
const int MoveUpRightSquareIndex[64] = {
0, 8, 9, 10, 11, 12, 13, 14,
8, 16, 17, 18, 19, 20, 21, 22,
16, 24, 25, 26, 27, 28, 29, 30,
24, 32, 33, 34, 35, 36, 37, 38,
32, 40, 41, 42, 43, 44, 45, 46,
40, 48, 49, 50, 51, 52, 53, 54,
48, 56, 57, 58, 59, 60, 61, 62,
56, 57, 58, 59, 60, 61, 62, 63
};
const int MoveRightSquareIndex[64] = {
0, 0, 1, 2, 3, 4, 5, 6,
8, 8, 9, 10, 11, 12, 13, 14,
16, 16, 17, 18, 19, 20, 21, 22,
24, 24, 25, 26, 27, 28, 29, 30,
32, 32, 33, 34, 35, 36, 37, 38,
40, 40, 41, 42, 43, 44, 45, 46,
48, 48, 49, 50, 51, 52, 53, 54,
56, 56, 57, 58, 59, 60, 61, 62
};
const int MoveDownRightSquareIndex[64] = {
0, 1, 2, 3, 4, 5, 6, 7,
8, 0, 1, 2, 3, 4, 5, 6,
16, 8, 9, 10, 11, 12, 13, 14,
24, 16, 17, 18, 19, 20, 21, 22,
32, 24, 25, 26, 27, 28, 29, 30,
40, 32, 33, 34, 35, 36, 37, 38,
48, 40, 41, 42, 43, 44, 45, 46,
56, 48, 49, 50, 51, 52, 53, 54
};
const int MoveDownSquareIndex[64] = {
0, 1, 2, 3, 4, 5, 6, 7,
0, 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
};
const int MoveDownLeftSquareIndex[64] = {
0, 1, 2, 3, 4, 5, 6, 7,
1, 2, 3, 4, 5, 6, 7, 15,
9, 10, 11, 12, 13, 14, 15, 23,
17, 18, 19, 20, 21, 22, 23, 31,
25, 26, 27, 28, 29, 30, 31, 39,
33, 34, 35, 36, 37, 38, 39, 47,
41, 42, 43, 44, 45, 46, 47, 55,
49, 50, 51, 52, 53, 54, 55, 63
};
const int MoveLeftSquareIndex[64] = {
1, 2, 3, 4, 5, 6, 7, 7,
9, 10, 11, 12, 13, 14, 15, 15,
17, 18, 19, 20, 21, 22, 23, 23,
25, 26, 27, 28, 29, 30, 31, 31,
33, 34, 35, 36, 37, 38, 39, 39,
41, 42, 43, 44, 45, 46, 47, 47,
49, 50, 51, 52, 53, 54, 55, 55,
57, 58, 59, 60, 61, 62, 63, 63
};
const int MoveUpLeftSquareIndex[64] = {
9, 10, 11, 12, 13, 14, 15, 7,
17, 18, 19, 20, 21, 22, 23, 15,
25, 26, 27, 28, 29, 30, 31, 23,
33, 34, 35, 36, 37, 38, 39, 31,
41, 42, 43, 44, 45, 46, 47, 39,
49, 50, 51, 52, 53, 54, 55, 47,
57, 58, 59, 60, 61, 62, 63, 55,
56, 57, 58, 59, 60, 61, 62, 63
};
const int MoveKnight1SquareIndex[64] = {
0, 16, 17, 18, 19, 20, 21, 22,
8, 24, 25, 26, 27, 28, 29, 30,
16, 32, 33, 34, 35, 36, 37, 38,
24, 40, 41, 42, 43, 44, 45, 46,
32, 48, 49, 50, 51, 52, 53, 54,
40, 56, 57, 58, 59, 60, 61, 62,
48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63
};
const int MoveKnight2SquareIndex[64] = {
0, 1, 8, 9, 10, 11, 12, 13,
8, 9, 16, 17, 18, 19, 20, 21,
16, 17, 24, 25, 26, 27, 28, 29,
24, 25, 32, 33, 34, 35, 36, 37,
32, 33, 40, 41, 42, 43, 44, 45,
40, 41, 48, 49, 50, 51, 52, 53,
48, 49, 56, 57, 58, 59, 60, 61,
56, 57, 58, 59, 60, 61, 62, 63
};
const int MoveKnight3SquareIndex[64] = {
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 0, 1, 2, 3, 4, 5,
16, 17, 8, 9, 10, 11, 12, 13,
24, 25, 16, 17, 18, 19, 20, 21,
32, 33, 24, 25, 26, 27, 28, 29,
40, 41, 32, 33, 34, 35, 36, 37,
48, 49, 40, 41, 42, 43, 44, 45,
56, 57, 48, 49, 50, 51, 52, 53
};
const int MoveKnight4SquareIndex[64] = {
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15,
16, 0, 1, 2, 3, 4, 5, 6,
24, 8, 9, 10, 11, 12, 13, 14,
32, 16, 17, 18, 19, 20, 21, 22,
40, 24, 25, 26, 27, 28, 29, 30,
48, 32, 33, 34, 35, 36, 37, 38,
56, 40, 41, 42, 43, 44, 45, 46
};
const int MoveKnight5SquareIndex[64] = {
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15,
1, 2, 3, 4, 5, 6, 7, 23,
9, 10, 11, 12, 13, 14, 15, 31,
17, 18, 19, 20, 21, 22, 23, 39,
25, 26, 27, 28, 29, 30, 31, 47,
33, 34, 35, 36, 37, 38, 39, 55,
41, 42, 43, 44, 45, 46, 47, 63
};
const int MoveKnight6SquareIndex[64] = {
0, 1, 2, 3, 4, 5, 6, 7,
2, 3, 4, 5, 6, 7, 14, 15,
10, 11, 12, 13, 14, 15, 22, 23,
18, 19, 20, 21, 22, 23, 30, 31,
26, 27, 28, 29, 30, 31, 38, 39,
34, 35, 36, 37, 38, 39, 46, 47,
42, 43, 44, 45, 46, 47, 54, 55,
50, 51, 52, 53, 54, 55, 62, 63
};
const int MoveKnight7SquareIndex[64] = {
10, 11, 12, 13, 14, 15, 6, 7,
18, 19, 20, 21, 22, 23, 14, 15,
26, 27, 28, 29, 30, 31, 22, 23,
34, 35, 36, 37, 38, 39, 30, 31,
42, 43, 44, 45, 46, 47, 38, 39,
50, 51, 52, 53, 54, 55, 46, 47,
58, 59, 60, 61, 62, 63, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63
};
const int MoveKnight8SquareIndex[64] = {
17, 18, 19, 20, 21, 22, 23, 7,
25, 26, 27, 28, 29, 30, 31, 15,
33, 34, 35, 36, 37, 38, 39, 23,
41, 42, 43, 44, 45, 46, 47, 31,
49, 50, 51, 52, 53, 54, 55, 39,
57, 58, 59, 60, 61, 62, 63, 47,
48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63
};
// Note : Inline Functions to follow
inline unsigned long getSquareIndex(BitBoard b);
//////// Fill Functions ////////////////////
////////////////////////////////////////////
// Fill in straight attacks //
// Note: Fill excludes attacking piece(s) //
////////////////////////////////////////////
inline BitBoard fillStraightAttacksOccluded(BitBoard g, BitBoard p)
{
BitBoard a;
a =fillRightOccluded(g,p);
a |= fillLeftOccluded(g,p);
a |= fillUpOccluded(g,p);
a |= fillDownOccluded(g,p);
a &= ~g; // exclude attacking pieces
return a;
// (Broken into single operations):
//BitBoard a, b, c, d, e, f, i, j;
//BitBoard s, t, u, v, w, x, y, z;
//BitBoard r;
//const BitBoard m1 = 0xfefefefefefefefe;
//const BitBoard m2 = 0x7f7f7f7f7f7f7f7f;
//// up
//a = g; b = p; s = a; s <<= 8; s &= b; a |= s; t = b; t <<= 8; b &= t; s = a; s <<= 16; s &= b;
//a |= s; t = b; t <<= 16; b &= t; s = a; s <<= 32; s &= b; a |= s; r = a;
//// down:
//c = g; d = p; u = c; u >>= 8; u &= d; c |= u; v = d; v >>= 8; d &= v; u = c; u >>= 16; u &= d;
//c |= u; v = d; v >>= 16; d &= v; u = c; u >>= 32; u &= d; c |= u; r |= c;
//// Left:
//e = g; f = p; f &= m1; w = e; w <<= 1; w &= f; e |= w; x = f; x <<= 1; f &= x; w = e; w <<= 2; w &= f;
//e |= w; x = f; x <<= 2; f &= x; w = e; w <<= 4; w &= f; e |= w; r |= e;
//// Right:
//i = g; j = p; j &= m2; y = i; y >>= 1; y &= j; i |= y; z = j; z >>= 1; j &= z; y = i; y >>= 2; y &= j;
//i |= y; z = j; z >>= 2; j &= z; y = i; y >>= 4; y &= j; i |= y; r |= i;
//r&=~g;
// return r;
}
////////////////////////////////////////////
// Fill in diagonal attacks //
// Note: Fill excludes attacking piece(s) //
////////////////////////////////////////////
inline BitBoard fillDiagonalAttacksOccluded(BitBoard g, BitBoard p)
{
BitBoard a;
a = fillUpRightOccluded(g,p);
a |= fillDownRightOccluded(g,p);
a |= fillDownLeftOccluded(g,p);
a |= fillUpLeftOccluded(g,p);
a &= ~g; // exclude attacking pieces
return a;
// (Broken into single operations):
//BitBoard a, b, c, d, e, f, i, j;
//BitBoard s, t, u, v, w, x, y, z;
//const BitBoard m1 = 0xfefefefefefefefe;
//const BitBoard m2 = 0x7f7f7f7f7f7f7f7f;
//BitBoard r;
//// UpRight
//a = g; b = p; b &= m2; s = a; s <<= 7; s &= b; a |= s; t = b; t <<= 7; b &= t; s = a; s <<= 14; s &= b;
//a |= s; t = b; t <<= 14; b &= t; s = a; s <<= 28; s &= b; a |= s; r = a;
//// downRight:
//c = g; d = p; d &= m2; u = c; u >>= 9; u &= d; c |= u; v = d; v >>= 9; d &= v; u = c; u >>= 18; u &= d;
//c |= u; v = d; v >>= 18; d &= v; u = c; u >>= 36; u &= d; c |= u; r |= c;
//// DownLeft:
//e = g; f = p; f &= m1; w = e; w >>= 7; w &= f; e |= w; x = f; x >>= 7; f &= x; w = e; w >>= 14; w &= f;
//e |= w; x = f; x >>= 14; f &= x; w = e; w >>= 28; w &= f; e |= w; r |= e;
//// UpLeft:
//i = g; j = p; j &= m1; y = i; y <<= 9; y &= j; i |= y; z = j; z <<= 9; j &= z; y = i; y <<= 18; y &= j;
//i |= y; z = j; z <<= 18; j &= z; y = i; y <<= 36; y &= j; i |= y; r |= i;
////
//r &= ~g;
//return r;
}
////////////////////////////////////////////
// Fill in king attacks //
// Note: Fill excludes attacking piece(s) //
////////////////////////////////////////////
inline BitBoard fillKingAttacksOccluded(BitBoard g, BitBoard p)
{
#ifdef V1
BitBoard a;
a = moveUpSingleOccluded(g,p);
a |= moveUpRightSingleOccluded(g,p);
a |= moveRightSingleOccluded(g,p);
a |= moveDownRightSingleOccluded(g,p);
a |= moveDownSingleOccluded(g,p);
a |= moveDownLeftSingleOccluded(g,p);
a |= moveLeftSingleOccluded(g,p);
a |= moveUpLeftSingleOccluded(g,p);
//a &= ~g; // exclude attacking pieces
return a;
#else
// Alternative:
//const BitBoard m1=0xfefefefefefefefe;
//const BitBoard m2=0x7f7f7f7f7f7f7f7f;
/* BitBoard a;
a = g | (0xfefefefefefefefe & (g << 1));
a |= a << 8;
a |= 0x7f7f7f7f7f7f7f7f & (a >> 1);
a |= a >> 8;
a &=~g;
a &=p;
return a; */
//
BitBoard a, b;
BitBoard t, u;
a = g; t = g; t <<= 1; t &= 0xfefefefefefefefe; a |= t;
b = a; b <<= 8; a |= b; u = a; u >>= 1; u &= 0x7f7f7f7f7f7f7f7f; a |= u;
b = a; b >>= 8; a |= b; a &= ~g; a &= p;
return a;
#endif
}
inline BitBoard fillKingAttacks(BitBoard g)
{
BitBoard a, b;
BitBoard t, u;
a = g; t = g; t <<= 1; t &= 0xfefefefefefefefe; a |= t;
b = a; b <<= 8; a |= b; u = a; u >>= 1; u &= 0x7f7f7f7f7f7f7f7f; a |= u;
b = a; b >>= 8; a |= b; a &= ~g;
return a;
}
////////////////////////////////////////////
// Fill in knight attacks //
// Note: Fill excludes attacking piece(s) //
////////////////////////////////////////////
inline BitBoard fillKnightAttacksOccluded(BitBoard g, BitBoard p)
{
/*
BitBoard a;
a = moveKnight1Occluded(g,p);
a |= moveKnight2Occluded(g,p);
a |= moveKnight3Occluded(g,p);
a |= moveKnight4Occluded(g,p);
a |= moveKnight5Occluded(g,p);
a |= moveKnight6Occluded(g,p);
a |= moveKnight7Occluded(g,p);
a |= moveKnight8Occluded(g,p);
//a &= ~g; // exclude attacking pieces
return a;
*/
BitBoard l1 = (g >> 1) & 0x7f7f7f7f7f7f7f7f;
BitBoard l2 = (g >> 2) & 0x3f3f3f3f3f3f3f3f;
BitBoard r1 = (g << 1) & 0xfefefefefefefefe;
BitBoard r2 = (g << 2) & 0xfcfcfcfcfcfcfcfc;
BitBoard h1 = l1 | r1;
BitBoard h2 = l2 | r2;
return p &((h1 << 16) | (h1 >> 16) | (h2 << 8) | (h2 >> 8));
}
inline BitBoard FillKnightAttacks(BitBoard g)
{
BitBoard l1 = (g >> 1) & 0x7f7f7f7f7f7f7f7f;
BitBoard l2 = (g >> 2) & 0x3f3f3f3f3f3f3f3f;
BitBoard r1 = (g << 1) & 0xfefefefefefefefe;
BitBoard r2 = (g << 2) & 0xfcfcfcfcfcfcfcfc;
BitBoard h1 = l1 | r1;
BitBoard h2 = l2 | r2;
return (h1 << 16) | (h1 >> 16) | (h2 << 8) | (h2 >> 8);
}
inline BitBoard fillUpOccluded(BitBoard g, BitBoard p)
{
// Note: Fill includes pieces.
g |= p & (g << 8);
p &= (p << 8);
g |= p & (g << 16);
p &= (p << 16);
g |= p & (g << 32);
return g;
}
inline BitBoard fillDownOccluded(BitBoard g, BitBoard p)
{
// Note: Fill includes pieces.
g |= p & (g >> 8);
p &= (p >> 8);
g |= p & (g >> 16);
p &= (p >> 16);
g |= p & (g >> 32);
return g;
}
inline BitBoard fillLeftOccluded(BitBoard g, BitBoard p)
{
// Note: Fill includes pieces.
p &= 0xfefefefefefefefe;
g |= p & (g << 1);
p &= (p << 1);
g |= p & (g << 2);
p &= (p << 2);
g |= p & (g << 4);
return g;
}
inline BitBoard fillRightOccluded(BitBoard g, BitBoard p)
{
// Note: Fill includes pieces.
p &= 0x7f7f7f7f7f7f7f7f;
g |= p & (g >> 1);
p &= (p >> 1);