#endif // defined(IS_64BIT)
-static const Bitboard DarkSquaresBB = 0xAA55AA55AA55AA55ULL;
-
-const Bitboard SquaresByColorBB[2] = { DarkSquaresBB, ~DarkSquaresBB };
-
-const Bitboard FileBB[8] = {
- FileABB, FileBBB, FileCBB, FileDBB, FileEBB, FileFBB, FileGBB, FileHBB
-};
-
-const Bitboard NeighboringFilesBB[8] = {
- FileBBB, FileABB|FileCBB, FileBBB|FileDBB, FileCBB|FileEBB,
- FileDBB|FileFBB, FileEBB|FileGBB, FileFBB|FileHBB, FileGBB
-};
-
-const Bitboard ThisAndNeighboringFilesBB[8] = {
- FileABB|FileBBB, FileABB|FileBBB|FileCBB,
- FileBBB|FileCBB|FileDBB, FileCBB|FileDBB|FileEBB,
- FileDBB|FileEBB|FileFBB, FileEBB|FileFBB|FileGBB,
- FileFBB|FileGBB|FileHBB, FileGBB|FileHBB
-};
-
-const Bitboard RankBB[8] = {
- Rank1BB, Rank2BB, Rank3BB, Rank4BB, Rank5BB, Rank6BB, Rank7BB, Rank8BB
-};
-
-const Bitboard InFrontBB[2][8] = {
- { Rank2BB | Rank3BB | Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
- Rank3BB | Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
- Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
- Rank5BB | Rank6BB | Rank7BB | Rank8BB,
- Rank6BB | Rank7BB | Rank8BB,
- Rank7BB | Rank8BB,
- Rank8BB,
- EmptyBoardBB
- },
- { EmptyBoardBB,
- Rank1BB,
- Rank2BB | Rank1BB,
- Rank3BB | Rank2BB | Rank1BB,
- Rank4BB | Rank3BB | Rank2BB | Rank1BB,
- Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB,
- Rank6BB | Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB,
- Rank7BB | Rank6BB | Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB
- }
-};
-
// Global bitboards definitions with static storage duration are
// automatically set to zero before enter main().
Bitboard RMask[64];
Bitboard SetMaskBB[65];
Bitboard ClearMaskBB[65];
-Bitboard NonSlidingAttacksBB[16][64];
+Bitboard SquaresByColorBB[2];
+Bitboard FileBB[8];
+Bitboard RankBB[8];
+Bitboard NeighboringFilesBB[8];
+Bitboard ThisAndNeighboringFilesBB[8];
+Bitboard InFrontBB[2][8];
+Bitboard StepAttacksBB[16][64];
Bitboard BetweenBB[64][64];
-
Bitboard SquaresInFrontMask[2][64];
Bitboard PassedPawnMask[2][64];
Bitboard AttackSpanMask[2][64];
namespace {
void init_masks();
- void init_non_sliding_attacks();
+ void init_step_attacks();
void init_pseudo_attacks();
void init_between_bitboards();
Bitboard index_to_bitboard(int index, Bitboard mask);
- Bitboard sliding_attacks(int sq, Bitboard block, int dirs, int deltas[][2],
+ Bitboard sliding_attacks(int sq, Bitboard occupied, int deltas[][2],
int fmin, int fmax, int rmin, int rmax);
void init_sliding_attacks(Bitboard attacks[], int attackIndex[], Bitboard mask[],
const int shift[], const Bitboard mult[], int deltas[][2]);
int bishopDeltas[4][2] = {{1,1},{-1,1},{1,-1},{-1,-1}};
init_masks();
- init_non_sliding_attacks();
+ init_step_attacks();
init_sliding_attacks(RAttacks, RAttackIndex, RMask, RShift, RMult, rookDeltas);
init_sliding_attacks(BAttacks, BAttackIndex, BMask, BShift, BMult, bishopDeltas);
init_pseudo_attacks();
void init_masks() {
+ SquaresByColorBB[DARK] = 0xAA55AA55AA55AA55ULL;
+ SquaresByColorBB[LIGHT] = ~SquaresByColorBB[DARK];
+
+ FileBB[FILE_A] = FileABB;
+ RankBB[RANK_1] = Rank1BB;
+
+ for (int f = FILE_B; f <= FILE_H; f++)
+ {
+ FileBB[f] = FileBB[f - 1] << 1;
+ RankBB[f] = RankBB[f - 1] << 8;
+ }
+
+ for (int f = FILE_A; f <= FILE_H; f++)
+ {
+ NeighboringFilesBB[f] = (f > FILE_A ? FileBB[f - 1] : 0) | (f < FILE_H ? FileBB[f + 1] : 0);
+ ThisAndNeighboringFilesBB[f] = FileBB[f] | NeighboringFilesBB[f];
+ }
+
+ for (int rw = RANK_7, rb = RANK_2; rw >= RANK_1; rw--, rb++)
+ {
+ InFrontBB[WHITE][rw] = InFrontBB[WHITE][rw + 1] | RankBB[rw + 1];
+ InFrontBB[BLACK][rb] = InFrontBB[BLACK][rb - 1] | RankBB[rb - 1];
+ }
+
SetMaskBB[SQ_NONE] = EmptyBoardBB;
ClearMaskBB[SQ_NONE] = ~SetMaskBB[SQ_NONE];
BitCount8Bit[b] = (uint8_t)count_1s<CNT32>(b);
}
- void init_non_sliding_attacks() {
+ void init_step_attacks() {
const int step[][9] = {
{0},
Square to = s + Square(step[pc][k]);
if (square_is_ok(to) && square_distance(s, to) < 3)
- set_bit(&NonSlidingAttacksBB[pc][s], to);
+ set_bit(&StepAttacksBB[pc][s], to);
}
}
- Bitboard sliding_attacks(int sq, Bitboard block, int dirs, int deltas[][2],
- int fmin=0, int fmax=7, int rmin=0, int rmax=7) {
- Bitboard result = 0;
+ Bitboard sliding_attacks(int sq, Bitboard occupied, int deltas[][2],
+ int fmin, int fmax, int rmin, int rmax) {
+ int dx, dy, f, r;
int rk = sq / 8;
int fl = sq % 8;
+ Bitboard attacks = EmptyBoardBB;
- for (int i = 0; i < dirs; i++)
+ for (int i = 0; i < 4; i++)
{
- int dx = deltas[i][0];
- int dy = deltas[i][1];
- int f = fl + dx;
- int r = rk + dy;
+ dx = deltas[i][0];
+ dy = deltas[i][1];
+ f = fl + dx;
+ r = rk + dy;
while ( (dx == 0 || (f >= fmin && f <= fmax))
&& (dy == 0 || (r >= rmin && r <= rmax)))
{
- result |= (1ULL << (f + r*8));
- if (block & (1ULL << (f + r*8)))
+ attacks |= (1ULL << (f + r * 8));
+
+ if (occupied & (1ULL << (f + r * 8)))
break;
f += dx;
r += dy;
}
}
- return result;
+ return attacks;
}
Bitboard index_to_bitboard(int index, Bitboard mask) {
void init_sliding_attacks(Bitboard attacks[], int attackIndex[], Bitboard mask[],
const int shift[], const Bitboard mult[], int deltas[][2]) {
+ Bitboard b, v;
+ int i, j, index;
- for (int i = 0, index = 0; i < 64; i++)
+ for (i = index = 0; i < 64; i++)
{
attackIndex[i] = index;
- mask[i] = sliding_attacks(i, 0, 4, deltas, 1, 6, 1, 6);
-
-#if defined(IS_64BIT)
- int j = (1 << (64 - shift[i]));
-#else
- int j = (1 << (32 - shift[i]));
-#endif
+ mask[i] = sliding_attacks(i, 0, deltas, 1, 6, 1, 6);
+ j = 1 << ((CpuIs64Bit ? 64 : 32) - shift[i]);
for (int k = 0; k < j; k++)
{
-#if defined(IS_64BIT)
- Bitboard b = index_to_bitboard(k, mask[i]);
- attacks[index + ((b * mult[i]) >> shift[i])] = sliding_attacks(i, b, 4, deltas);
-#else
- Bitboard b = index_to_bitboard(k, mask[i]);
- unsigned v = int(b) * int(mult[i]) ^ int(b >> 32) * int(mult[i] >> 32);
- attacks[index + (v >> shift[i])] = sliding_attacks(i, b, 4, deltas);
-#endif
+ b = index_to_bitboard(k, mask[i]);
+ v = CpuIs64Bit ? b * mult[i] : unsigned(b * mult[i] ^ (b >> 32) * (mult[i] >> 32));
+ attacks[index + (v >> shift[i])] = sliding_attacks(i, b, deltas, 0, 7, 0, 7);
}
index += j;
}
void init_between_bitboards() {
- Square s1, s2, s3;
- SquareDelta d;
+ Square s1, s2, s3, d;
int f, r;
for (s1 = SQ_A1; s1 <= SQ_H8; s1++)
f = file_distance(s1, s2);
r = rank_distance(s1, s2);
- d = SquareDelta(s2 - s1) / Max(f, r);
+ d = (s2 - s1) / Max(f, r);
for (s3 = s1 + d; s3 != s2; s3 += d)
set_bit(&(BetweenBB[s1][s2]), s3);