Skip to content

Commit 3fda064

Browse files
committed
Retire one implementation of pop_lsb()
We have two implementations that are equivalent, so retire one. Plus usual tidy up of comments and code reshuffle. No functional change.
1 parent a6e2920 commit 3fda064

File tree

6 files changed

+124
-125
lines changed

6 files changed

+124
-125
lines changed

src/bitbase.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ namespace {
7171
} // namespace
7272

7373

74-
bool Bitbases::probe_kpk(Square wksq, Square wpsq, Square bksq, Color us) {
74+
bool Bitbases::probe(Square wksq, Square wpsq, Square bksq, Color us) {
7575

7676
assert(file_of(wpsq) <= FILE_D);
7777

@@ -80,7 +80,7 @@ bool Bitbases::probe_kpk(Square wksq, Square wpsq, Square bksq, Color us) {
8080
}
8181

8282

83-
void Bitbases::init_kpk() {
83+
void Bitbases::init() {
8484

8585
unsigned idx, repeat = 1;
8686
std::vector<KPKPosition> db;

src/bitboard.cpp

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@
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];
2931
Bitboard* 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];
3436
Bitboard* BishopAttacks[SQUARE_NB];
35-
unsigned BishopShifts[SQUARE_NB];
37+
unsigned BishopShifts [SQUARE_NB];
3638

3739
Bitboard SquareBB[SQUARE_NB];
3840
Bitboard FileBB[FILE_NB];
@@ -42,51 +44,44 @@ Bitboard InFrontBB[COLOR_NB][RANK_NB];
4244
Bitboard StepAttacksBB[PIECE_NB][SQUARE_NB];
4345
Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
4446
Bitboard LineBB[SQUARE_NB][SQUARE_NB];
45-
Bitboard DistanceRingsBB[SQUARE_NB][8];
47+
Bitboard DistanceRingBB[SQUARE_NB][8];
4648
Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
4749
Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
4850
Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
4951
Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
5052

51-
int SquareDistance[SQUARE_NB][SQUARE_NB];
52-
5353
namespace {
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

9287
Square 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

126121
const 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

Comments
 (0)