X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fbitboard.cpp;h=110609d294c8ce75180b15d01daf094807f450a5;hp=dd12dd3ad509775276ebee5345836b9b194ce35b;hb=d372f2e39a65e2b14fc0335841e652a9d208d3ec;hpb=59c85346d28f96ae69e172ff7187ccfbbf78d180 diff --git a/src/bitboard.cpp b/src/bitboard.cpp index dd12dd3a..110609d2 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -156,51 +156,6 @@ const int RShift[64] = { #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]; @@ -214,9 +169,14 @@ Bitboard BAttacks[0x1480]; 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]; @@ -231,7 +191,7 @@ uint8_t BitCount8Bit[256]; 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); @@ -347,7 +307,7 @@ void init_bitboards() { 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(); @@ -363,6 +323,30 @@ namespace { 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]; @@ -384,7 +368,7 @@ namespace { BitCount8Bit[b] = (uint8_t)count_1s(b); } - void init_non_sliding_attacks() { + void init_step_attacks() { const int step[][9] = { {0}, @@ -401,7 +385,7 @@ namespace { 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); } } @@ -451,27 +435,19 @@ namespace { 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, deltas, 1, 6, 1, 6); - -#if defined(IS_64BIT) - int j = (1 << (64 - shift[i])); -#else - int j = (1 << (32 - shift[i])); -#endif + j = 1 << ((CpuIs64Bit ? 64 : 32) - shift[i]); for (int k = 0; k < j; k++) { - Bitboard b = index_to_bitboard(k, mask[i]); - -#if defined(IS_64BIT) - Bitboard v = b * mult[i]; -#else - unsigned v = int(b) * int(mult[i]) ^ int(b >> 32) * int(mult[i] >> 32); -#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; @@ -490,8 +466,7 @@ namespace { 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++) @@ -501,7 +476,7 @@ namespace { 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);