-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoveGeneratorBitboard_backup.h
3586 lines (2974 loc) · 125 KB
/
MoveGeneratorBitboard_backup.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
#include "chess.h"
#include "FancyMagics.h"
#include <intrin.h>
#include <time.h>
#define DEBUG_PRINT_MOVES 0
#if DEBUG_PRINT_MOVES == 1
#define DEBUG_PRINT_DEPTH 6
bool printMoves = false;
#endif
// first add moves to a move list and then use makeMove function to update the board
// when this is set to 0, generateBoards is called to generate the updated boards directly
#define USE_MOVE_LIST 0
// only count moves at leaves (instead of generating/making them)
#define USE_COUNT_ONLY_OPT 1
// move generation functions templated on chance
#define USE_TEMPLATE_CHANCE_OPT 1
// bitwise magic instead of if/else for castle flag updation
#define USE_BITWISE_MAGIC_FOR_CASTLE_FLAG_UPDATION 1
// Move counting (countOnly) doesn't work with old method
#define EN_PASSENT_GENERATION_NEW_METHOD 1
// intel core 2 doesn't have popcnt instruction
#define USE_POPCNT 0
// use lookup tabls for figuring out squares in line and squares in between
#define USE_IN_BETWEEN_LUT 1
// use lookup table for king moves
#define USE_KING_LUT 1
// use lookup table for knight moves
#define USE_KNIGHT_LUT 1
// use lookup table (magics) for sliding moves
#define USE_SLIDING_LUT 1
// use fancy fixed-shift version - ~ 800 KB lookup tables
// (setting this to 0 enables plain magics - with 2.3 MB lookup table)
// plain magics is a bit faster at least for perft (on core 2 duo)
// fancy magics is clearly faster on more recent processors (ivy bridge)
#define USE_FANCY_MAGICS 1
// bit board constants
#define C64(constantU64) constantU64##ULL
// valid locations for pawns
#define RANKS2TO7 C64(0x00FFFFFFFFFFFF00)
#define RANK1 C64(0x00000000000000FF)
#define RANK2 C64(0x000000000000FF00)
#define RANK3 C64(0x0000000000FF0000)
#define RANK4 C64(0x00000000FF000000)
#define RANK5 C64(0x000000FF00000000)
#define RANK6 C64(0x0000FF0000000000)
#define RANK7 C64(0x00FF000000000000)
#define RANK8 C64(0xFF00000000000000)
#define FILEA C64(0x0101010101010101)
#define FILEB C64(0x0202020202020202)
#define FILEC C64(0x0404040404040404)
#define FILED C64(0x0808080808080808)
#define FILEE C64(0x1010101010101010)
#define FILEF C64(0x2020202020202020)
#define FILEG C64(0x4040404040404040)
#define FILEH C64(0x8080808080808080)
#define DIAGONAL_A1H8 C64(0x8040201008040201)
#define DIAGONAL_A8H1 C64(0x0102040810204080)
#define CENTRAL_SQUARES C64(0x007E7E7E7E7E7E00)
// used for castling checks
#define F1G1 C64(0x60)
#define C1D1 C64(0x0C)
#define B1D1 C64(0x0E)
// used for castling checks
#define F8G8 C64(0x6000000000000000)
#define C8D8 C64(0x0C00000000000000)
#define B8D8 C64(0x0E00000000000000)
// used to update castle flags
#define WHITE_KING_SIDE_ROOK C64(0x0000000000000080)
#define WHITE_QUEEN_SIDE_ROOK C64(0x0000000000000001)
#define BLACK_KING_SIDE_ROOK C64(0x8000000000000000)
#define BLACK_QUEEN_SIDE_ROOK C64(0x0100000000000000)
#define ALLSET C64(0xFFFFFFFFFFFFFFFF)
#define EMPTY C64(0x0)
CUDA_CALLABLE_MEMBER __forceinline uint8 popCount(uint64 x)
{
#ifdef __CUDA_ARCH__
return __popcll(x);
#elif USE_POPCNT == 1
#ifdef _WIN64
return _mm_popcnt_u64(x);
#else
uint32 lo = (uint32) x;
uint32 hi = (uint32) (x >> 32);
return _mm_popcnt_u32(lo) + _mm_popcnt_u32(hi);
#endif
#else
// taken from chess prgramming wiki: http://chessprogramming.wikispaces.com/Population+Count
const uint64 k1 = C64(0x5555555555555555); /* -1/3 */
const uint64 k2 = C64(0x3333333333333333); /* -1/5 */
const uint64 k4 = C64(0x0f0f0f0f0f0f0f0f); /* -1/17 */
const uint64 kf = C64(0x0101010101010101); /* -1/255 */
x = x - ((x >> 1) & k1); /* put count of each 2 bits into those 2 bits */
x = (x & k2) + ((x >> 2) & k2); /* put count of each 4 bits into those 4 bits */
x = (x + (x >> 4)) & k4 ; /* put count of each 8 bits into those 8 bits */
x = (x * kf) >> 56; /* returns 8 most significant bits of x + (x<<8) + (x<<16) + (x<<24) + ... */
return (uint8) x;
#endif
}
// return the index of first set LSB
CUDA_CALLABLE_MEMBER __forceinline uint8 bitScan(uint64 x)
{
#ifdef __CUDA_ARCH__
// __ffsll(x) returns position from 1 to 64 instead of 0 to 63
return __ffsll(x) - 1;
#elif _WIN64
unsigned long index;
assert (x != 0);
_BitScanForward64(&index, x);
return (uint8) index;
#else
uint32 lo = (uint32) x;
uint32 hi = (uint32) (x >> 32);
DWORD id;
if (lo)
_BitScanForward(&id, lo);
else
{
_BitScanForward(&id, hi);
id += 32;
}
return (uint8) id;
#endif
}
// bit mask containing squares between two given squares
static uint64 Between[64][64];
// bit mask containing squares in the same 'line' as two given squares
static uint64 Line[64][64];
// squares a piece can attack in an empty board
static uint64 RookAttacks [64];
static uint64 BishopAttacks [64];
static uint64 QueenAttacks [64];
static uint64 KingAttacks [64];
static uint64 KnightAttacks [64];
static uint64 pawnAttacks[2] [64];
// magic lookup tables
// plain magics (Fancy magic lookup tables in FancyMagics.h)
#define ROOK_MAGIC_BITS 12
#define BISHOP_MAGIC_BITS 9
uint64 rookMagics [64];
uint64 bishopMagics [64];
// same as RookAttacks and BishopAttacks, but corner bits masked off
static uint64 RookAttacksMasked [64];
static uint64 BishopAttacksMasked [64];
uint64 rookMagicAttackTables [64][1 << ROOK_MAGIC_BITS ]; // 2 MB
uint64 bishopMagicAttackTables [64][1 << BISHOP_MAGIC_BITS]; // 256 KB
uint64 findRookMagicForSquare(int square, uint64 magicAttackTable[], uint64 magic = 0);
uint64 findBishopMagicForSquare(int square, uint64 magicAttackTable[], uint64 magic = 0);
#if TEST_GPU_PERFT == 1
// gpu version of the above data structures
// accessed for read only using __ldg() function
// bit mask containing squares between two given squares
__device__ static uint64 gBetween[64][64];
// bit mask containing squares in the same 'line' as two given squares
__device__ static uint64 gLine[64][64];
// squares a piece can attack in an empty board
__device__ static uint64 gRookAttacks [64];
__device__ static uint64 gBishopAttacks [64];
__device__ static uint64 gQueenAttacks [64];
__device__ static uint64 gKingAttacks [64];
__device__ static uint64 gKnightAttacks [64];
__device__ static uint64 gpawnAttacks[2] [64];
// Magical Tables
// same as RookAttacks and BishopAttacks, but corner bits masked off
__device__ static uint64 gRookAttacksMasked [64];
__device__ static uint64 gBishopAttacksMasked [64];
// plain magics
__device__ static uint64 gRookMagics [64];
__device__ static uint64 gBishopMagics [64];
__device__ static uint64 gRookMagicAttackTables [64][1 << ROOK_MAGIC_BITS ]; // 2 MB
__device__ static uint64 gBishopMagicAttackTables [64][1 << BISHOP_MAGIC_BITS]; // 256 KB
// fancy magics (cpu versions in FancyMagics.h)
__device__ static uint64 g_fancy_magic_lookup_table[97264];
__device__ static FancyMagicEntry g_bishop_magics_fancy[64];
__device__ static FancyMagicEntry g_rook_magics_fancy[64];
#endif
CUDA_CALLABLE_MEMBER __forceinline uint64 sqsInBetweenLUT(uint8 sq1, uint8 sq2)
{
#ifdef __CUDA_ARCH__
return __ldg(&gBetween[sq1][sq2]);
#else
return Between[sq1][sq2];
#endif
}
CUDA_CALLABLE_MEMBER __forceinline uint64 sqsInLineLUT(uint8 sq1, uint8 sq2)
{
#ifdef __CUDA_ARCH__
return __ldg(&gLine[sq1][sq2]);
#else
return Line[sq1][sq2];
#endif
}
CUDA_CALLABLE_MEMBER __forceinline uint64 sqKnightAttacks(uint8 sq)
{
#ifdef __CUDA_ARCH__
return __ldg(&gKnightAttacks[sq]);
#else
return KnightAttacks[sq];
#endif
}
CUDA_CALLABLE_MEMBER __forceinline uint64 sqKingAttacks(uint8 sq)
{
#ifdef __CUDA_ARCH__
return __ldg(&gKingAttacks[sq]);
#else
return KingAttacks[sq];
#endif
}
CUDA_CALLABLE_MEMBER __forceinline uint64 sqRookAttacks(uint8 sq)
{
#ifdef __CUDA_ARCH__
return __ldg(&gRookAttacks[sq]);
#else
return RookAttacks[sq];
#endif
}
CUDA_CALLABLE_MEMBER __forceinline uint64 sqBishopAttacks(uint8 sq)
{
#ifdef __CUDA_ARCH__
return __ldg(&gBishopAttacks[sq]);
#else
return BishopAttacks[sq];
#endif
}
CUDA_CALLABLE_MEMBER __forceinline uint64 sqBishopAttacksMasked(uint8 sq)
{
#ifdef __CUDA_ARCH__
return __ldg(&gBishopAttacksMasked[sq]);
#else
return BishopAttacksMasked[sq];
#endif
}
CUDA_CALLABLE_MEMBER __forceinline uint64 sqRookAttacksMasked(uint8 sq)
{
#ifdef __CUDA_ARCH__
return __ldg(&gRookAttacksMasked[sq]);
#else
return RookAttacksMasked[sq];
#endif
}
CUDA_CALLABLE_MEMBER __forceinline uint64 sqRookMagics(uint8 sq)
{
#ifdef __CUDA_ARCH__
return __ldg(&gRookMagics[sq]);
#else
return rookMagics[sq];
#endif
}
CUDA_CALLABLE_MEMBER __forceinline uint64 sqBishopMagics(uint8 sq)
{
#ifdef __CUDA_ARCH__
return __ldg(&gBishopMagics[sq]);
#else
return bishopMagics[sq];
#endif
}
CUDA_CALLABLE_MEMBER __forceinline uint64 sqRookMagicAttackTables(uint8 sq, uint64 index)
{
#ifdef __CUDA_ARCH__
return __ldg(&gRookMagicAttackTables[sq][index]);
#else
return rookMagicAttackTables[sq][index];
#endif
}
CUDA_CALLABLE_MEMBER __forceinline uint64 sqBishopMagicAttackTables(uint8 sq, uint64 index)
{
#ifdef __CUDA_ARCH__
return __ldg(&gBishopMagicAttackTables[sq][index]);
#else
return bishopMagicAttackTables[sq][index];
#endif
}
CUDA_CALLABLE_MEMBER __forceinline uint64 sq_fancy_magic_lookup_table(int index)
{
#ifdef __CUDA_ARCH__
return __ldg(&g_fancy_magic_lookup_table[index]);
#else
return fancy_magic_lookup_table[index];
#endif
}
CUDA_CALLABLE_MEMBER __forceinline FancyMagicEntry sq_bishop_magics_fancy(int sq)
{
#ifdef __CUDA_ARCH__
FancyMagicEntry op;
op.factor = __ldg(&g_bishop_magics_fancy[sq].factor);
op.position = __ldg(&g_bishop_magics_fancy[sq].position);
return op;
#else
return bishop_magics_fancy[sq];
#endif
}
CUDA_CALLABLE_MEMBER __forceinline FancyMagicEntry sq_rook_magics_fancy(int sq)
{
#ifdef __CUDA_ARCH__
FancyMagicEntry op;
op.factor = __ldg(&g_rook_magics_fancy[sq].factor);
op.position = __ldg(&g_rook_magics_fancy[sq].position);
return op;
#else
return rook_magics_fancy[sq];
#endif
}
class MoveGeneratorBitboard
{
public:
// move the bits in the bitboard one square in the required direction
CUDA_CALLABLE_MEMBER __forceinline static uint64 northOne(uint64 x)
{
return x << 8;
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 southOne(uint64 x)
{
return x >> 8;
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 eastOne(uint64 x)
{
return (x << 1) & (~FILEA);
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 westOne(uint64 x)
{
return (x >> 1) & (~FILEH);
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 northEastOne(uint64 x)
{
return (x << 9) & (~FILEA);
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 northWestOne(uint64 x)
{
return (x << 7) & (~FILEH);
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 southEastOne(uint64 x)
{
return (x >> 7) & (~FILEA);
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 southWestOne(uint64 x)
{
return (x >> 9) & (~FILEH);
}
// fill the board in the given direction
// taken from http://chessprogramming.wikispaces.com/
// gen - generator : starting positions
// pro - propogator : empty squares / squares not of current side
// uses kogge-stone algorithm
CUDA_CALLABLE_MEMBER __forceinline static uint64 northFill(uint64 gen, uint64 pro)
{
gen |= (gen << 8) & pro;
pro &= (pro << 8);
gen |= (gen << 16) & pro;
pro &= (pro << 16);
gen |= (gen << 32) & pro;
return gen;
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 southFill(uint64 gen, uint64 pro)
{
gen |= (gen >> 8) & pro;
pro &= (pro >> 8);
gen |= (gen >> 16) & pro;
pro &= (pro >> 16);
gen |= (gen >> 32) & pro;
return gen;
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 eastFill(uint64 gen, uint64 pro)
{
pro &= ~FILEA;
gen |= (gen << 1) & pro;
pro &= (pro << 1);
gen |= (gen << 2) & pro;
pro &= (pro << 2);
gen |= (gen << 3) & pro;
return gen;
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 westFill(uint64 gen, uint64 pro)
{
pro &= ~FILEH;
gen |= (gen >> 1) & pro;
pro &= (pro >> 1);
gen |= (gen >> 2) & pro;
pro &= (pro >> 2);
gen |= (gen >> 3) & pro;
return gen;
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 northEastFill(uint64 gen, uint64 pro)
{
pro &= ~FILEA;
gen |= (gen << 9) & pro;
pro &= (pro << 9);
gen |= (gen << 18) & pro;
pro &= (pro << 18);
gen |= (gen << 36) & pro;
return gen;
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 northWestFill(uint64 gen, uint64 pro)
{
pro &= ~FILEH;
gen |= (gen << 7) & pro;
pro &= (pro << 7);
gen |= (gen << 14) & pro;
pro &= (pro << 14);
gen |= (gen << 28) & pro;
return gen;
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 southEastFill(uint64 gen, uint64 pro)
{
pro &= ~FILEA;
gen |= (gen >> 7) & pro;
pro &= (pro >> 7);
gen |= (gen >> 14) & pro;
pro &= (pro >> 14);
gen |= (gen >> 28) & pro;
return gen;
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 southWestFill(uint64 gen, uint64 pro)
{
pro &= ~FILEH;
gen |= (gen >> 9) & pro;
pro &= (pro >> 9);
gen |= (gen >> 18) & pro;
pro &= (pro >> 18);
gen |= (gen >> 36) & pro;
return gen;
}
// attacks in the given direction
// need to OR with ~(pieces of side to move) to avoid killing own pieces
CUDA_CALLABLE_MEMBER __forceinline static uint64 northAttacks(uint64 gen, uint64 pro)
{
gen |= (gen << 8) & pro;
pro &= (pro << 8);
gen |= (gen << 16) & pro;
pro &= (pro << 16);
gen |= (gen << 32) & pro;
return gen << 8;
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 southAttacks(uint64 gen, uint64 pro)
{
gen |= (gen >> 8) & pro;
pro &= (pro >> 8);
gen |= (gen >> 16) & pro;
pro &= (pro >> 16);
gen |= (gen >> 32) & pro;
return gen >> 8;
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 eastAttacks(uint64 gen, uint64 pro)
{
pro &= ~FILEA;
gen |= (gen << 1) & pro;
pro &= (pro << 1);
gen |= (gen << 2) & pro;
pro &= (pro << 2);
gen |= (gen << 3) & pro;
return (gen << 1) & (~FILEA);
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 westAttacks(uint64 gen, uint64 pro)
{
pro &= ~FILEH;
gen |= (gen >> 1) & pro;
pro &= (pro >> 1);
gen |= (gen >> 2) & pro;
pro &= (pro >> 2);
gen |= (gen >> 3) & pro;
return (gen >> 1) & (~FILEH);
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 northEastAttacks(uint64 gen, uint64 pro)
{
pro &= ~FILEA;
gen |= (gen << 9) & pro;
pro &= (pro << 9);
gen |= (gen << 18) & pro;
pro &= (pro << 18);
gen |= (gen << 36) & pro;
return (gen << 9) & (~FILEA);
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 northWestAttacks(uint64 gen, uint64 pro)
{
pro &= ~FILEH;
gen |= (gen << 7) & pro;
pro &= (pro << 7);
gen |= (gen << 14) & pro;
pro &= (pro << 14);
gen |= (gen << 28) & pro;
return (gen << 7) & (~FILEH);
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 southEastAttacks(uint64 gen, uint64 pro)
{
pro &= ~FILEA;
gen |= (gen >> 7) & pro;
pro &= (pro >> 7);
gen |= (gen >> 14) & pro;
pro &= (pro >> 14);
gen |= (gen >> 28) & pro;
return (gen >> 7) & (~FILEA);
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 southWestAttacks(uint64 gen, uint64 pro)
{
pro &= ~FILEH;
gen |= (gen >> 9) & pro;
pro &= (pro >> 9);
gen |= (gen >> 18) & pro;
pro &= (pro >> 18);
gen |= (gen >> 36) & pro;
return (gen >> 9) & (~FILEH);
}
// attacks by pieces of given type
// pro - empty squares
CUDA_CALLABLE_MEMBER __forceinline static uint64 bishopAttacksKoggeStone(uint64 bishops, uint64 pro)
{
return northEastAttacks(bishops, pro) |
northWestAttacks(bishops, pro) |
southEastAttacks(bishops, pro) |
southWestAttacks(bishops, pro) ;
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 rookAttacksKoggeStone(uint64 rooks, uint64 pro)
{
return northAttacks(rooks, pro) |
southAttacks(rooks, pro) |
eastAttacks (rooks, pro) |
westAttacks (rooks, pro) ;
}
#if USE_SLIDING_LUT == 1
CUDA_CALLABLE_MEMBER __forceinline static uint64 bishopAttacks(uint64 bishop, uint64 pro)
{
uint8 square = bitScan(bishop);
uint64 occ = (~pro) & sqBishopAttacksMasked(square);
#if USE_FANCY_MAGICS == 1
#ifdef __CUDA_ARCH__
FancyMagicEntry magicEntry = sq_bishop_magics_fancy(square);
int index = (magicEntry.factor * occ) >> (64 - BISHOP_MAGIC_BITS);
return sq_fancy_magic_lookup_table(magicEntry.position + index);
#else
// this version is slightly faster for CPUs.. why ?
uint64 magic = bishop_magics_fancy[square].factor;
uint64 *table = &fancy_magic_lookup_table[bishop_magics_fancy[square].position];
uint64 index = (magic * occ) >> (64 - BISHOP_MAGIC_BITS);
return table[index];
#endif
#else
uint64 magic = sqBishopMagics(square);
uint64 index = (magic * occ) >> (64 - BISHOP_MAGIC_BITS);
return sqBishopMagicAttackTables(square, index);
#endif
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 rookAttacks(uint64 rook, uint64 pro)
{
uint8 square = bitScan(rook);
uint64 occ = (~pro) & sqRookAttacksMasked(square);
#if USE_FANCY_MAGICS == 1
#ifdef __CUDA_ARCH__
FancyMagicEntry magicEntry = sq_rook_magics_fancy(square);
int index = (magicEntry.factor * occ) >> (64 - ROOK_MAGIC_BITS);
return sq_fancy_magic_lookup_table(magicEntry.position + index);
#else
// this version is slightly faster for CPUs.. why ?
uint64 magic = rook_magics_fancy[square].factor;
uint64 *table = &fancy_magic_lookup_table[rook_magics_fancy[square].position];
uint64 index = (magic * occ) >> (64 - ROOK_MAGIC_BITS);
return table[index];
#endif
#else
uint64 magic = sqRookMagics(square);
uint64 index = (magic * occ) >> (64 - ROOK_MAGIC_BITS);
return sqRookMagicAttackTables(square, index);
#endif
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 multiBishopAttacks(uint64 bishops, uint64 pro)
{
uint64 attacks = 0;
while(bishops)
{
uint64 bishop = getOne(bishops);
attacks |= bishopAttacks(bishop, pro);
bishops ^= bishop;
}
return attacks;
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 multiRookAttacks(uint64 rooks, uint64 pro)
{
uint64 attacks = 0;
while(rooks)
{
uint64 rook = getOne(rooks);
attacks |= rookAttacks(rook, pro);
rooks ^= rook;
}
return attacks;
}
#else
// kogge stone handles multiple attackers automatically
#define bishopAttacks bishopAttacksKoggeStone
#define rookAttacks rookAttacksKoggeStone
#define multiBishopAttacks bishopAttacksKoggeStone
#define multiRookAttacks rookAttacksKoggeStone
#endif
// not used
#if 0
CUDA_CALLABLE_MEMBER __forceinline static uint64 queenAttacks(uint64 queens, uint64 pro)
{
return rookAttacks (queens, pro) |
bishopAttacks(queens, pro) ;
}
#endif
CUDA_CALLABLE_MEMBER __forceinline static uint64 kingAttacks(uint64 kingSet)
{
uint64 attacks = eastOne(kingSet) | westOne(kingSet);
kingSet |= attacks;
attacks |= northOne(kingSet) | southOne(kingSet);
return attacks;
}
// efficient knight attack generator
// http://chessprogramming.wikispaces.com/Knight+Pattern
CUDA_CALLABLE_MEMBER __forceinline static uint64 knightAttacks(uint64 knights) {
uint64 l1 = (knights >> 1) & C64(0x7f7f7f7f7f7f7f7f);
uint64 l2 = (knights >> 2) & C64(0x3f3f3f3f3f3f3f3f);
uint64 r1 = (knights << 1) & C64(0xfefefefefefefefe);
uint64 r2 = (knights << 2) & C64(0xfcfcfcfcfcfcfcfc);
uint64 h1 = l1 | r1;
uint64 h2 = l2 | r2;
return (h1<<16) | (h1>>16) | (h2<<8) | (h2>>8);
}
// gets one bit (the LSB) from a bitboard
// returns a bitboard containing that bit
CUDA_CALLABLE_MEMBER __forceinline static uint64 getOne(uint64 x)
{
return x & (-x);
}
CUDA_CALLABLE_MEMBER __forceinline static bool isMultiple(uint64 x)
{
return x ^ getOne(x);
}
CUDA_CALLABLE_MEMBER __forceinline static bool isSingular(uint64 x)
{
return !isMultiple(x);
}
// finds the squares in between the two given squares
// taken from
// http://chessprogramming.wikispaces.com/Square+Attacked+By#Legality Test-In Between-Pure Calculation
// Ankan : TODO: this doesn't seem to work for G8 - B3
CUDA_CALLABLE_MEMBER __forceinline static uint64 squaresInBetween(uint8 sq1, uint8 sq2)
{
const uint64 m1 = C64(0xFFFFFFFFFFFFFFFF);
const uint64 a2a7 = C64(0x0001010101010100);
const uint64 b2g7 = C64(0x0040201008040200);
const uint64 h1b7 = C64(0x0002040810204080);
uint64 btwn, line, rank, file;
btwn = (m1 << sq1) ^ (m1 << sq2);
file = (sq2 & 7) - (sq1 & 7);
rank = ((sq2 | 7) - sq1) >> 3 ;
line = ( (file & 7) - 1) & a2a7; // a2a7 if same file
line += 2 * (( (rank & 7) - 1) >> 58); // b1g1 if same rank
line += (((rank - file) & 15) - 1) & b2g7; // b2g7 if same diagonal
line += (((rank + file) & 15) - 1) & h1b7; // h1b7 if same antidiag
line *= btwn & -btwn; // mul acts like shift by smaller square
return line & btwn; // return the bits on that line inbetween
}
// returns the 'line' containing all pieces in the same file/rank/diagonal or anti-diagonal containing sq1 and sq2
CUDA_CALLABLE_MEMBER __forceinline static uint64 squaresInLine(uint8 sq1, uint8 sq2)
{
// TODO: try to make it branchless?
int fileDiff = (sq2 & 7) - (sq1 & 7);
int rankDiff = ((sq2 | 7) - sq1) >> 3 ;
uint8 file = sq1 & 7;
uint8 rank = sq1 >> 3;
if (fileDiff == 0) // same file
{
return FILEA << file;
}
if (rankDiff == 0) // same rank
{
return RANK1 << (rank * 8);
}
if (fileDiff - rankDiff == 0) // same diagonal (with slope equal to a1h8)
{
if (rank - file >= 0)
return DIAGONAL_A1H8 << ((rank - file) * 8);
else
return DIAGONAL_A1H8 >> ((file - rank) * 8);
}
if (fileDiff + rankDiff == 0) // same anti-diagonal (with slope equal to a8h1)
{
// for a8h1, rank + file = 7
int shiftAmount = (rank + file - 7) * 8;
if (shiftAmount >= 0)
return DIAGONAL_A8H1 << shiftAmount;
else
return DIAGONAL_A8H1 >> (-shiftAmount);
}
// squares not on same line
return 0;
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 sqsInBetween(uint8 sq1, uint8 sq2)
{
#if USE_IN_BETWEEN_LUT == 1
return sqsInBetweenLUT(sq1, sq2);
#else
return squaresInBetween(sq1, sq2);
#endif
}
CUDA_CALLABLE_MEMBER __forceinline static uint64 sqsInLine(uint8 sq1, uint8 sq2)
{
#if USE_IN_BETWEEN_LUT == 1
return sqsInLineLUT(sq1, sq2);
#else
return squaresInLine(sq1, sq2);
#endif
}
static void init()
{
// initialize the empty board attack tables
for (uint8 i=0; i < 64; i++)
{
uint64 x = BIT(i);
uint64 north = northAttacks(x, ALLSET);
uint64 south = southAttacks(x, ALLSET);
uint64 east = eastAttacks (x, ALLSET);
uint64 west = westAttacks (x, ALLSET);
uint64 ne = northEastAttacks(x, ALLSET);
uint64 nw = northWestAttacks(x, ALLSET);
uint64 se = southEastAttacks(x, ALLSET);
uint64 sw = southWestAttacks(x, ALLSET);
RookAttacks [i] = north | south | east | west;
BishopAttacks[i] = ne | nw | se | sw;
QueenAttacks [i] = RookAttacks[i] | BishopAttacks[i];
KnightAttacks[i] = knightAttacks(x);
KingAttacks[i] = kingAttacks(x);
// TODO: initialize pawn attack table
// probably not really needed (as pawn attack calculation is simple enough)
}
// initialize the Between and Line tables
for (uint8 i=0; i<64; i++)
for (uint8 j=0; j<64; j++)
{
if (i <= j)
{
Between[i][j] = squaresInBetween(i, j);
Between[j][i] = Between[i][j];
}
Line[i][j] = squaresInLine(i, j);
}
// initialize magic lookup tables
#if USE_SLIDING_LUT == 1
srand (time(NULL));
for (int square = A1; square <= H8; square++)
{
uint64 thisSquare = BIT(square);
uint64 mask = sqRookAttacks(square) & (~thisSquare);
// mask off squares that don't matter
if ((thisSquare & RANK1) == 0)
mask &= ~RANK1;
if ((thisSquare & RANK8) == 0)
mask &= ~RANK8;
if ((thisSquare & FILEA) == 0)
mask &= ~FILEA;
if ((thisSquare & FILEH) == 0)
mask &= ~FILEH;
RookAttacksMasked[square] = mask;
mask = sqBishopAttacks(square) & (~thisSquare) & CENTRAL_SQUARES;
BishopAttacksMasked[square] = mask;
#if USE_FANCY_MAGICS != 1
rookMagics [square] = findRookMagicForSquare (square, rookMagicAttackTables [square]);
bishopMagics[square] = findBishopMagicForSquare(square, bishopMagicAttackTables[square]);
#endif
}
// initialize fancy magic lookup table
for (int square = A1; square <= H8; square++)
{
uint64 rookMagic = findRookMagicForSquare (square, &fancy_magic_lookup_table[rook_magics_fancy[square].position], rook_magics_fancy[square].factor);
assert(rookMagic == rook_magics_fancy[square].factor);
uint64 bishopMagic = findBishopMagicForSquare (square, &fancy_magic_lookup_table[bishop_magics_fancy[square].position], bishop_magics_fancy[square].factor);
assert(bishopMagic == bishop_magics_fancy[square].factor);
}
#endif
#if TEST_GPU_PERFT == 1
// copy all the lookup tables from CPU's memory to GPU memory
cudaError_t err = cudaMemcpyToSymbol(gBetween, Between, sizeof(Between));
if (err != S_OK) printf("For copying between table, Err id: %d, str: %s\n", err, cudaGetErrorString(err));
err = cudaMemcpyToSymbol(gLine, Line, sizeof(Line));
if (err != S_OK) printf("For copying line table, Err id: %d, str: %s\n", err, cudaGetErrorString(err));
err = cudaMemcpyToSymbol(gRookAttacks, RookAttacks, sizeof(RookAttacks));
if (err != S_OK) printf("For copying RookAttacks table, Err id: %d, str: %s\n", err, cudaGetErrorString(err));
err = cudaMemcpyToSymbol(gBishopAttacks, BishopAttacks, sizeof(BishopAttacks));
if (err != S_OK) printf("For copying BishopAttacks table, Err id: %d, str: %s\n", err, cudaGetErrorString(err));
err = cudaMemcpyToSymbol(gQueenAttacks, QueenAttacks, sizeof(QueenAttacks));
if (err != S_OK) printf("For copying QueenAttacks table, Err id: %d, str: %s\n", err, cudaGetErrorString(err));
err = cudaMemcpyToSymbol(gKnightAttacks, KnightAttacks, sizeof(KnightAttacks));
if (err != S_OK) printf("For copying KnightAttacks table, Err id: %d, str: %s\n", err, cudaGetErrorString(err));
err = cudaMemcpyToSymbol(gKingAttacks, KingAttacks, sizeof(KingAttacks));
if (err != S_OK) printf("For copying KingAttacks table, Err id: %d, str: %s\n", err, cudaGetErrorString(err));
// Copy magical tables
err = cudaMemcpyToSymbol(gRookAttacksMasked, RookAttacksMasked, sizeof(RookAttacksMasked));
if (err != S_OK) printf("For copying RookAttacksMasked table, Err id: %d, str: %s\n", err, cudaGetErrorString(err));
err = cudaMemcpyToSymbol(gBishopAttacksMasked, BishopAttacksMasked , sizeof(BishopAttacksMasked));
if (err != S_OK) printf("For copying BishopAttacksMasked table, Err id: %d, str: %s\n", err, cudaGetErrorString(err));
err = cudaMemcpyToSymbol(gRookMagics, rookMagics, sizeof(rookMagics));
if (err != S_OK) printf("For copying rookMagics table, Err id: %d, str: %s\n", err, cudaGetErrorString(err));
err = cudaMemcpyToSymbol(gBishopMagics, bishopMagics, sizeof(bishopMagics));
if (err != S_OK) printf("For copying bishopMagics table, Err id: %d, str: %s\n", err, cudaGetErrorString(err));
err = cudaMemcpyToSymbol(gRookMagicAttackTables, rookMagicAttackTables, sizeof(rookMagicAttackTables));
if (err != S_OK) printf("For copying RookMagicAttackTables, Err id: %d, str: %s\n", err, cudaGetErrorString(err));
err = cudaMemcpyToSymbol(gBishopMagicAttackTables, bishopMagicAttackTables, sizeof(bishopMagicAttackTables));
if (err != S_OK) printf("For copying bishopMagicAttackTables, Err id: %d, str: %s\n", err, cudaGetErrorString(err));
err = cudaMemcpyToSymbol(g_fancy_magic_lookup_table, fancy_magic_lookup_table, sizeof(fancy_magic_lookup_table));
if (err != S_OK) printf("For copying fancy_magic_lookup_table, Err id: %d, str: %s\n", err, cudaGetErrorString(err));
err = cudaMemcpyToSymbol(g_bishop_magics_fancy, bishop_magics_fancy, sizeof(bishop_magics_fancy));
if (err != S_OK) printf("For copying bishop_magics_fancy, Err id: %d, str: %s\n", err, cudaGetErrorString(err));
err = cudaMemcpyToSymbol(g_rook_magics_fancy, rook_magics_fancy, sizeof(rook_magics_fancy));
if (err != S_OK) printf("For copying rook_magics_fancy, Err id: %d, str: %s\n", err, cudaGetErrorString(err));
#endif
}
CUDA_CALLABLE_MEMBER static __forceinline uint64 findPinnedPieces (uint64 myKing, uint64 myPieces, uint64 enemyBishops, uint64 enemyRooks, uint64 allPieces, uint8 kingIndex)
{
// check for sliding attacks to the king's square