[go: up one dir, main page]

Menu

[r49]: / bitboard.h  Maximize  Restore  History

Download this file

711 lines (605 with data), 18.6 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
/*
Protector -- a UCI chess engine
Copyright (C) 2008 Raimund Heid (Raimund_Heid@yahoo.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _bitboard_h_
#define _bitboard_h_
#include "protector.h"
#include <string.h>
#ifdef TARGET_WINDOWS
#include <intrin.h>
#endif
typedef UINT64 Bitboard;
#define EMPTY_BITBOARD ULONG_ZERO
#define IMAX_ROOK 4096
#define IMAX_BISHOP 512
typedef struct
{
int hLane, vLane, uLane, dLane;
BYTE hLaneSetMask, vLaneSetMask, uLaneSetMask, dLaneSetMask;
BYTE hLaneClearMask, vLaneClearMask, uLaneClearMask, dLaneClearMask;
}
SquareLaneInfo;
typedef struct
{
int numSetSquares;
UINT8 setSquares[16];
}
SetSquaresInfo;
typedef struct
{
int hLaneNumber, vLaneNumber, uLaneNumber, dLaneNumber;
Bitboard hLane[256], vLane[256], uLane[256], dLane[256];
}
ObstacleSquareInfo;
typedef struct
{
Bitboard preMask, magicNumber;
Bitboard moves[IMAX_ROOK];
}
MagicSquareInfoRook;
typedef struct
{
Bitboard preMask, magicNumber;
Bitboard moves[IMAX_BISHOP];
}
MagicSquareInfoBishop;
#define SLI(square) (squareLaneInfo[(square)])
extern UINT64 minValue[_64_];
extern UINT64 maxValue[_64_];
extern INT8 highestBit[0x10000];
extern Bitboard hlane[_64_][256];
extern Bitboard vlane[_64_][256];
extern Bitboard ulane[_64_][256];
extern Bitboard dlane[_64_][256];
extern ObstacleSquareInfo obsi[_64_];
extern Bitboard castlings[2][16][256];
extern int castlingLane[2];
extern int castlingsOfColor[2];
extern SquareLaneInfo squareLaneInfo[_64_];
extern Bitboard generalMoves[0x0f][_64_];
extern Bitboard squaresOfFile[8];
extern Bitboard squaresOfRank[8];
extern Bitboard squaresOfLateralFiles[8];
extern Bitboard squaresOfFileRange[8][8];
extern Bitboard squaresOfRankRange[8][8];
extern Bitboard pawnMoves[2][_64_][256];
extern Bitboard promotionCandidates[2];
extern SetSquaresInfo setSquares[4][0x10000];
extern INT8 numSetBits[0x10000];
extern UINT8 rankOverlay[0x10000];
extern UINT8 bitshiftGap[8][256];
extern Bitboard squaresBehind[_64_][_64_];
extern Bitboard squaresBetween[_64_][_64_];
extern Bitboard squaresInDistance[8][_64_];
extern Bitboard squaresInTaxiDistance[15][_64_];
extern Bitboard squaresAbove[2][_64_];
extern Bitboard squaresBelow[2][_64_];
extern Bitboard orthoKingAttackers[_64_];
extern Bitboard diaKingAttackers[_64_];
extern Bitboard knightKingAttackers[_64_];
extern Bitboard pawnKingAttackers[2][_64_];
extern Bitboard interestedPawns[2][_64_][256];
extern Bitboard nonA, nonH, border, center, lightSquares, darkSquares,
queenSide, kingSide, centerFiles, extendedCenter;
extern int hLaneNumber[_64_], vLaneNumber[_64_];
extern int uLaneNumber[_64_], dLaneNumber[_64_];
extern Bitboard preMaskRook[64], preMaskBishop[64];
extern Bitboard magicRookMoves[64][IMAX_ROOK];
extern Bitboard magicBishopMoves[64][IMAX_BISHOP];
extern const Bitboard magicRookNumber[64];
extern const Bitboard magicBishopNumber[64];
extern MagicSquareInfoRook magicSquareInfoRook[64];
extern MagicSquareInfoBishop magicSquareInfoBishop[64];
#define setSquare(bitboard,square) ((bitboard) |= minValue[(square)]);
#define clearSquare(bitboard,square) ((bitboard) &= maxValue[(square)]);
#define excludeSquares(bitboard,toBeExcluded) ((bitboard) &= ~(toBeExcluded))
INLINE bool testSquare(const Bitboard bitboard, const Square square)
{
return (bool) (((bitboard) & minValue[(square)]) != EMPTY_BITBOARD);
}
/**
* Get the king moves for the specified square.
*/
INLINE Bitboard getKingMoves(const Square square)
{
return generalMoves[KING][square];
}
/**
* Get castling moves depending from the castling rights and
* the current position.
*/
INLINE Bitboard getCastlingMoves(const Color color, const BYTE castlingRights,
const Bitboard obstacles)
{
if (color == WHITE)
{
return castlings[WHITE][castlingRights][obstacles & 0xFF];
}
else
{
return castlings[BLACK][castlingRights][obstacles >> 56];
}
}
/**
* Get the queen moves for the specified square.
*/
/*
INLINE Bitboard getQueenMoves(const Square square, const BYTE * obstacles)
{
const ObstacleSquareInfo *_obsi = &obsi[square];
return _obsi->hLane[obstacles[_obsi->hLaneNumber]] |
_obsi->vLane[obstacles[_obsi->vLaneNumber]] |
_obsi->uLane[obstacles[_obsi->uLaneNumber]] |
_obsi->dLane[obstacles[_obsi->dLaneNumber]];
}
*/
INLINE int getWidth(const Bitboard set)
{
if (set == EMPTY_BITBOARD)
{
return 0;
}
else
{
File leftMost = FILE_A, rightMost = FILE_H;
while ((set & squaresOfFile[leftMost]) == EMPTY_BITBOARD)
{
leftMost++;
}
while ((set & squaresOfFile[rightMost]) == EMPTY_BITBOARD)
{
rightMost--;
}
return rightMost - leftMost;
}
}
/**
* Get the queen moves for the specified square.
*/
INLINE Bitboard getMagicQueenMoves(const Square square,
const Bitboard obstacles)
{
MagicSquareInfoRook *msir = &magicSquareInfoRook[square];
MagicSquareInfoBishop *msib = &magicSquareInfoBishop[square];
return
msir->moves[((obstacles & msir->preMask) * msir->magicNumber) >> 52] |
msib->moves[((obstacles & msib->preMask) * msib->magicNumber) >> 55];
}
/**
* Get the rook moves for the specified square.
*/
INLINE Bitboard getRookMoves(const Square square, const BYTE * obstacles)
{
const ObstacleSquareInfo *_obsi = &obsi[square];
return _obsi->hLane[obstacles[_obsi->hLaneNumber]] |
_obsi->vLane[obstacles[_obsi->vLaneNumber]];
}
/**
* Get the rook moves for the specified square.
*/
INLINE Bitboard getMagicRookMoves(const Square square,
const Bitboard obstacles)
{
MagicSquareInfoRook *msir = &magicSquareInfoRook[square];
return
msir->moves[((obstacles & msir->preMask) * msir->magicNumber) >> 52];
}
/**
* Get the bishop moves for the specified square.
*/
INLINE Bitboard getBishopMoves(const Square square, const BYTE * obstacles)
{
const ObstacleSquareInfo *_obsi = &obsi[square];
return _obsi->uLane[obstacles[_obsi->uLaneNumber]] |
_obsi->dLane[obstacles[_obsi->dLaneNumber]];
}
/**
* Get the bishop moves for the specified square.
*/
INLINE Bitboard getMagicBishopMoves(const Square square,
const Bitboard obstacles)
{
MagicSquareInfoBishop *msib = &magicSquareInfoBishop[square];
return
msib->moves[((obstacles & msib->preMask) * msib->magicNumber) >> 55];
}
/**
* Get the knight moves for the specified square.
*/
INLINE Bitboard getKnightMoves(const Square square)
{
return generalMoves[KNIGHT][square];
}
/**
* Get the pawn captures for the specified square.
*/
INLINE Bitboard getPawnCaptures(const Piece piece, const Square square,
const Bitboard allPieces)
{
return generalMoves[piece][square] & allPieces;
}
/**
* Get the pawn advances for the specified square.
*/
INLINE Bitboard getPawnAdvances(const Color color, const Square square,
const Bitboard obstacles)
{
Bitboard advances;
if (color == WHITE)
{
advances = (minValue[square] << 8) & ~obstacles;
if (rank(square) == RANK_2)
{
advances |= (advances << 8) & ~obstacles;
}
}
else
{
advances = (minValue[square] >> 8) & ~obstacles;
if (rank(square) == RANK_7)
{
advances |= (advances >> 8) & ~obstacles;
}
}
return advances;
}
/**
* Get the pawns interested in advancing to the specified square.
*/
INLINE Bitboard getInterestedPawns(const Color color, const Square square,
const Bitboard obstacles)
{
Bitboard interestedPawns;
if (color == WHITE)
{
interestedPawns = minValue[square] >> 8;
if (rank(square) == RANK_4 &&
(interestedPawns & obstacles) == EMPTY_BITBOARD)
{
interestedPawns = minValue[square] >> 16;
}
}
else
{
interestedPawns = minValue[square] << 8;
if (rank(square) == RANK_5 &&
(interestedPawns & obstacles) == EMPTY_BITBOARD)
{
interestedPawns = minValue[square] << 16;
}
}
return interestedPawns;
}
/**
* Get the squares between the two specified squares.
*/
INLINE Bitboard getSquaresBetween(const Square square1, const Square square2)
{
return squaresBetween[square1][square2];
}
/**
* Get the squares behind 'target', as seen from 'viewpoint'.
*/
INLINE Bitboard getSquaresBehind(const Square target, const Square viewpoint)
{
return squaresBehind[target][viewpoint];
}
/**
* Shift all set squares one file to the left.
*/
INLINE Bitboard shiftLeft(const Bitboard bitboard)
{
return (bitboard & nonA) >> 1;
}
/**
* Shift all set squares one file to the right.
*/
INLINE Bitboard shiftRight(const Bitboard bitboard)
{
return (bitboard & nonH) << 1;
}
/**
* Get all squares lateral to the specified squares.
*/
INLINE Bitboard getLateralSquares(const Bitboard squares)
{
return ((squares & nonA) >> 1) | ((squares & nonH) << 1);
}
/**
* Get the squares of the specified file.
*/
INLINE Bitboard getSquaresOfFile(const File file)
{
return squaresOfFile[file];
}
/**
* Get the squares of the specified rank.
*/
INLINE Bitboard getSquaresOfRank(const Rank rank)
{
return squaresOfRank[rank];
}
#ifdef TARGET_WINDOWS
#define UHEX_FFFF 0xFFFFui64
#endif
#ifdef TARGET_LINUX
#define UHEX_FFFF 0xFFFFllu
#endif
/**
* Get the number of set squares in the specified bitboard.
*/
INLINE int getNumberOfSetSquares(const Bitboard bitboard)
{
return numSetBits[bitboard & UHEX_FFFF] +
numSetBits[(bitboard >> 16) & UHEX_FFFF] +
numSetBits[(bitboard >> 32) & UHEX_FFFF] +
numSetBits[(bitboard >> 48) & UHEX_FFFF];
}
/**
* Get the rank overlay of the specified bitboard.
*/
INLINE int getRankOverlay(const Bitboard bitboard)
{
return rankOverlay[(bitboard) & UHEX_FFFF] |
rankOverlay[(bitboard >> 16) & UHEX_FFFF] |
rankOverlay[(bitboard >> 32) & UHEX_FFFF] |
rankOverlay[(bitboard >> 48) & UHEX_FFFF];
}
/**
* The number of lanes used to hold information
* about the state of all files, rows and diagonals
*/
#define NUM_LANES 46
/**
* Get the moves of the the specified piece.
*/
INLINE Bitboard getMoves(Square square, Piece piece, Bitboard allPieces)
{
switch (pieceType(piece))
{
case PAWN:
return getPawnCaptures(piece, square, allPieces) |
getPawnAdvances(pieceColor(piece), square, allPieces);
case KING:
return getKingMoves(square);
case KNIGHT:
return getKnightMoves(square);
case ROOK:
return getMagicRookMoves(square, allPieces);
case BISHOP:
return getMagicBishopMoves(square, allPieces);
default:
return getMagicQueenMoves(square, allPieces);
}
}
/**
* Set a square in the specified vector of obstacles.
*/
INLINE void setObstacleSquare(Square square, BYTE obstacles[NUM_LANES])
{
SquareLaneInfo *sqi = &squareLaneInfo[square];
obstacles[sqi->hLane] |= sqi->hLaneSetMask;
obstacles[sqi->vLane] |= sqi->vLaneSetMask;
obstacles[sqi->uLane] |= sqi->uLaneSetMask;
obstacles[sqi->dLane] |= sqi->dLaneSetMask;
}
/**
* Clear a square in the specified vector of obstacles.
*/
INLINE void clearObstacleSquare(Square square, BYTE obstacles[NUM_LANES])
{
SquareLaneInfo *sqi = &squareLaneInfo[square];
obstacles[sqi->hLane] &= sqi->hLaneClearMask;
obstacles[sqi->vLane] &= sqi->vLaneClearMask;
obstacles[sqi->uLane] &= sqi->uLaneClearMask;
obstacles[sqi->dLane] &= sqi->dLaneClearMask;
}
/**
* Calculate all obstacles according to the specified bitboard.
*/
INLINE void calculateObstacles(Bitboard board, BYTE obstacles[NUM_LANES])
{
Square square;
memset(obstacles, 0x00, NUM_LANES);
ITERATE(square)
{
if (board & minValue[square])
{
setObstacleSquare(square, obstacles);
}
}
}
/**
* Initialize this module.
*
* @return 0 if no errors occurred.
*/
int initializeModuleBitboard(void);
/**
* Test this module.
*
* @return 0 if all tests succeed.
*/
int testModuleBitboard(void);
/**
* Get the number of a set square and clear the corresponding bit.
*
* @return NO_SQUARE if no square was set.
*/
INLINE Square getNextSquare(Bitboard * vector);
/**
* Extend all set bits by one square into all directions.
*/
INLINE void floodBoard(Bitboard * board);
/**
* Get the targets of all pawns specified by 'whitePawns'.
*/
INLINE Bitboard getWhitePawnTargets(const Bitboard whitePawns)
{
return ((whitePawns & nonA) << 7) | ((whitePawns & nonH) << 9);
}
/**
* Get the targets of all pawns specified by 'blackPawns'.
*/
INLINE Bitboard getBlackPawnTargets(const Bitboard blackPawns)
{
return ((blackPawns & nonA) >> 9) | ((blackPawns & nonH) >> 7);
}
/**
* Iteration shortcuts
*/
#define ITERATE_BITBOARD(b,sq) while ( ( sq = getNextSquare(b) ) >= 0 )
INLINE void floodBoard(Bitboard * board)
{
const Bitboard toLeft = *board & nonA, toRight = *board & nonH;
*board = (toLeft >> 1) | (toLeft << 7) | (*board >> 8) | (toLeft >> 9) |
(toRight << 1) | (toRight >> 7) | (*board << 8) | (toRight << 9);
}
#ifdef TARGET_WINDOWS
#define UHEX_FFFFffff00000000 0xFFFFffff00000000ui64
#define UHEX_00000000FFFF0000 0x00000000FFFF0000ui64
#define UHEX_FFFF000000000000 0xFFFF000000000000ui64
#define UHEX_00000000FFFFffff 0x00000000FFFFffffui64
#endif
#ifdef TARGET_LINUX
#define UHEX_FFFFffff00000000 0xFFFFffff00000000llu
#define UHEX_00000000FFFF0000 0x00000000FFFF0000llu
#define UHEX_FFFF000000000000 0xFFFF000000000000llu
#define UHEX_00000000FFFFffff 0x00000000FFFFffffllu
#endif
#ifdef WIN64
INLINE Square getNextSquare(Bitboard * vector)
{
unsigned long index;
if (_BitScanReverse64(&index, *vector))
{
_bittestandreset64((__int64 *) vector, index);
return (Square) index;
}
else
{
return NO_SQUARE;
}
}
#else
#if !defined NDEBUG || defined TARGET_LINUX
INLINE Square getNextSquare(Bitboard * vector)
{
Square nextSquare;
if ((*vector & UHEX_FFFFffff00000000) == ULONG_ZERO)
{
if ((*vector & UHEX_00000000FFFF0000) == ULONG_ZERO)
{
if ((nextSquare = (Square) highestBit[(int) *vector]) >= 0)
{
*vector &= maxValue[nextSquare];
}
return nextSquare;
}
else
{
nextSquare = (Square) (highestBit[(int) (*vector >> 16)] + 16);
*vector &= maxValue[nextSquare];
return nextSquare;
}
}
else
{
if ((*vector & UHEX_FFFF000000000000) == ULONG_ZERO)
{
nextSquare = (Square) (highestBit[(int) (*vector >> 32)] + 32);
*vector &= maxValue[nextSquare];
return nextSquare;
}
else
{
nextSquare = (Square) (highestBit[(int) (*vector >> 48)] + 48);
*vector &= maxValue[nextSquare];
return nextSquare;
}
}
}
#else
INLINE Square getNextSquare(Bitboard * vector)
{
unsigned long index;
if (*vector > UHEX_00000000FFFFffff)
{
unsigned long *uw = ((unsigned long *) (vector)) + 1;
_BitScanReverse(&index, *uw);
_bittestandreset((long *) uw, index);
return (Square) index + 32;
}
else
{
if (_BitScanReverse(&index, (unsigned long) *vector))
{
_bittestandreset((long *) vector, index);
return (Square) index;
}
else
{
return NO_SQUARE;
}
}
}
#endif
#endif
INLINE int getSetSquares(const Bitboard board, UINT8 squares[_64_])
{
int index;
UINT8 *currentSquare = &squares[0];
if ((index = (int) (0xFFFF & board)) != 0)
{
const SetSquaresInfo *info = &setSquares[0][index];
memcpy(currentSquare, info->setSquares, info->numSetSquares);
currentSquare += info->numSetSquares;
}
if ((index = (int) (0xFFFF & (board >> 16))) != 0)
{
const SetSquaresInfo *info = &setSquares[1][index];
memcpy(currentSquare, info->setSquares, info->numSetSquares);
currentSquare += info->numSetSquares;
}
if ((index = (int) (0xFFFF & (board >> 32))) != 0)
{
const SetSquaresInfo *info = &setSquares[2][index];
memcpy(currentSquare, info->setSquares, info->numSetSquares);
currentSquare += info->numSetSquares;
}
if ((index = (int) (board >> 48)) != 0)
{
const SetSquaresInfo *info = &setSquares[3][index];
memcpy(currentSquare, info->setSquares, info->numSetSquares);
currentSquare += info->numSetSquares;
}
return (int) (currentSquare - &squares[0]);
}
INLINE Bitboard getMultipleSquaresBetween(const Square origin,
Bitboard targets)
{
Bitboard squares = targets;
Square targetSquare;
Bitboard *sqb = &(squaresBetween[origin][0]);
ITERATE_BITBOARD(&targets, targetSquare)
{
squares |= sqb[targetSquare];
}
return squares;
}
unsigned int getMinimumDistance(const Bitboard targets, const Square square);
unsigned int getMaximumDistance(const Bitboard targets, const Square square);
int getFloodValue(const Square origin, const Bitboard targets,
const Bitboard permittedSquares);
#endif