2424#include " bitcount.h"
2525#include " misc.h"
2626
27- Bitboard RookMasks[SQUARE_NB];
28- Bitboard RookMagics[SQUARE_NB];
27+ int SquareDistance[SQUARE_NB][SQUARE_NB];
28+
29+ Bitboard RookMasks [SQUARE_NB];
30+ Bitboard RookMagics [SQUARE_NB];
2931Bitboard* RookAttacks[SQUARE_NB];
30- unsigned RookShifts[SQUARE_NB];
32+ unsigned RookShifts [SQUARE_NB];
3133
32- Bitboard BishopMasks[SQUARE_NB];
33- Bitboard BishopMagics[SQUARE_NB];
34+ Bitboard BishopMasks [SQUARE_NB];
35+ Bitboard BishopMagics [SQUARE_NB];
3436Bitboard* BishopAttacks[SQUARE_NB];
35- unsigned BishopShifts[SQUARE_NB];
37+ unsigned BishopShifts [SQUARE_NB];
3638
3739Bitboard SquareBB[SQUARE_NB];
3840Bitboard FileBB[FILE_NB];
@@ -42,51 +44,44 @@ Bitboard InFrontBB[COLOR_NB][RANK_NB];
4244Bitboard StepAttacksBB[PIECE_NB][SQUARE_NB];
4345Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
4446Bitboard LineBB[SQUARE_NB][SQUARE_NB];
45- Bitboard DistanceRingsBB [SQUARE_NB][8 ];
47+ Bitboard DistanceRingBB [SQUARE_NB][8 ];
4648Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
4749Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
4850Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
4951Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
5052
51- int SquareDistance[SQUARE_NB][SQUARE_NB];
52-
5353namespace {
5454
5555 // De Bruijn sequences. See chessprogramming.wikispaces.com/BitScan
56- const uint64_t DeBruijn_64 = 0x3F79D71B4CB0A89ULL ;
57- const uint32_t DeBruijn_32 = 0x783A9B23 ;
56+ const uint64_t DeBruijn64 = 0x3F79D71B4CB0A89ULL ;
57+ const uint32_t DeBruijn32 = 0x783A9B23 ;
5858
59- int MS1BTable[256 ];
60- Square BSFTable[SQUARE_NB];
61- Bitboard RookTable[0x19000 ]; // Storage space for rook attacks
62- Bitboard BishopTable[0x1480 ]; // Storage space for bishop attacks
59+ int MS1BTable[256 ]; // To implement software msb()
60+ Square BSFTable[SQUARE_NB]; // To implement software bitscan
61+ Bitboard RookTable[0x19000 ]; // To store rook attacks
62+ Bitboard BishopTable[0x1480 ]; // To store bishop attacks
6363
6464 typedef unsigned (Fn)(Square, Bitboard);
6565
6666 void init_magics (Bitboard table[], Bitboard* attacks[], Bitboard magics[],
6767 Bitboard masks[], unsigned shifts[], Square deltas[], Fn index);
6868
69- FORCE_INLINE unsigned bsf_index (Bitboard b) {
69+ // bsf_index() returns the index into BSFTable[] to look up the bitscan. Uses
70+ // Matt Taylor's folding for 32 bit case, extended to 64 bit by Kim Walisch.
7071
71- // Matt Taylor's folding for 32 bit systems, extended to 64 bits by Kim Walisch
72- b ^= ( b - 1 ) ;
73- return Is64Bit ? (b * DeBruijn_64 ) >> 58
74- : ((unsigned (b) ^ unsigned (b >> 32 )) * DeBruijn_32 ) >> 26 ;
72+ FORCE_INLINE unsigned bsf_index (Bitboard b) {
73+ b ^= b - 1 ;
74+ return Is64Bit ? (b * DeBruijn64 ) >> 58
75+ : ((unsigned (b) ^ unsigned (b >> 32 )) * DeBruijn32 ) >> 26 ;
7576 }
7677}
7778
78- // / lsb()/msb() finds the least/most significant bit in a non-zero bitboard.
79- // / pop_lsb() finds and clears the least significant bit in a non-zero bitboard.
80-
8179#ifndef USE_BSFQ
8280
83- Square lsb (Bitboard b) { return BSFTable[bsf_index (b)]; }
84-
85- Square pop_lsb (Bitboard* b) {
81+ // / Software fall-back of lsb() and msb() for CPU lacking hardware support
8682
87- Bitboard bb = *b;
88- *b = bb & (bb - 1 );
89- return BSFTable[bsf_index (bb)];
83+ Square lsb (Bitboard b) {
84+ return BSFTable[bsf_index (b)];
9085}
9186
9287Square msb (Bitboard b) {
@@ -120,8 +115,8 @@ Square msb(Bitboard b) {
120115#endif // ifndef USE_BSFQ
121116
122117
123- // / Bitboards::pretty() returns an ASCII representation of a bitboard to be
124- // / printed to standard output. This is sometimes useful for debugging.
118+ // / Bitboards::pretty() returns an ASCII representation of a bitboard suitable
119+ // / to be printed to standard output. Useful for debugging.
125120
126121const std::string Bitboards::pretty (Bitboard b) {
127122
@@ -178,7 +173,7 @@ void Bitboards::init() {
178173 if (s1 != s2)
179174 {
180175 SquareDistance[s1][s2] = std::max (distance<File>(s1, s2), distance<Rank>(s1, s2));
181- DistanceRingsBB [s1][SquareDistance[s1][s2] - 1 ] |= s2;
176+ DistanceRingBB [s1][SquareDistance[s1][s2] - 1 ] |= s2;
182177 }
183178
184179 int steps[][9 ] = { {}, { 7 , 9 }, { 17 , 15 , 10 , 6 , -6 , -10 , -15 , -17 },
0 commit comments