Skip to content

Commit

Permalink
Simplify BSFTable initialization
Browse files Browse the repository at this point in the history
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
  • Loading branch information
mcostalba committed Sep 19, 2012
1 parent 22a5f91 commit e4a0482
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions src/bitboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ namespace {

CACHE_LINE_ALIGNMENT

int BSFTable[64];
int MS1BTable[256];
Square BSFTable[64];
Bitboard RTable[0x19000]; // Storage space for rook attacks
Bitboard BTable[0x1480]; // Storage space for bishop attacks
uint8_t BitCount8Bit[256];
Expand All @@ -79,27 +79,23 @@ namespace {

#if !defined(USE_BSFQ)

Square lsb(Bitboard b) {
FORCE_INLINE unsigned bsf_index(Bitboard b) {

if (Is64Bit)
return Square(BSFTable[((b & -b) * DeBruijn_64) >> 58]);
return ((b & -b) * DeBruijn_64) >> 58;

// Use Matt Taylor's folding trick for 32 bit systems
b ^= (b - 1);
uint32_t fold = unsigned(b) ^ unsigned(b >> 32);
return Square(BSFTable[(fold * DeBruijn_32) >> 26]);
return ((unsigned(b) ^ unsigned(b >> 32)) * DeBruijn_32) >> 26;
}

Square lsb(Bitboard b) { return BSFTable[bsf_index(b)]; }

Square pop_lsb(Bitboard* b) {

Bitboard bb = *b;
*b = bb & (bb - 1);

if (Is64Bit)
return Square(BSFTable[((bb & -bb) * DeBruijn_64) >> 58]);

bb ^= (bb - 1);
uint32_t fold = unsigned(bb) ^ unsigned(bb >> 32);
return Square(BSFTable[(fold * DeBruijn_32) >> 26]);
return BSFTable[bsf_index(bb)];
}

Square msb(Bitboard b) {
Expand Down Expand Up @@ -127,7 +123,7 @@ Square msb(Bitboard b) {
result += 8;
}

return Square(result + MS1BTable[b32]);
return (Square)(result + MS1BTable[b32]);
}

#endif // !defined(USE_BSFQ)
Expand Down Expand Up @@ -162,6 +158,9 @@ void Bitboards::init() {
while (k < (2 << i))
MS1BTable[k++] = i;

for (int i = 0; i < 64; i++)
BSFTable[bsf_index(1ULL << i)] = Square(i);

for (Bitboard b = 0; b < 256; b++)
BitCount8Bit[b] = (uint8_t)popcount<Max15>(b);

Expand Down Expand Up @@ -204,17 +203,6 @@ void Bitboards::init() {
if (SquareDistance[s1][s2] == d)
DistanceRingsBB[s1][d - 1] |= s2;

for (int i = 0; i < 64; i++)
if (!Is64Bit) // Matt Taylor's folding trick for 32 bit systems
{
Bitboard b = 1ULL << i;
b ^= b - 1;
b ^= b >> 32;
BSFTable[(uint32_t)(b * DeBruijn_32) >> 26] = i;
}
else
BSFTable[((1ULL << i) * DeBruijn_64) >> 58] = i;

int steps[][9] = { {}, { 7, 9 }, { 17, 15, 10, 6, -6, -10, -15, -17 },
{}, {}, {}, { 9, 7, -7, -9, 8, 1, -1, -8 } };

Expand Down

0 comments on commit e4a0482

Please sign in to comment.