Skip to content

Commit

Permalink
Fix #21 bitboard operations for MSVC
Browse files Browse the repository at this point in the history
No functional change for other compilers.
  • Loading branch information
ianfab committed May 19, 2019
1 parent 08bbbda commit a39deba
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/bitbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ bool Bitbases::probe(Square wksq, Square wpsq, Square bksq, Color us) {

void Bitbases::init() {

#ifndef LARGEBOARDS
std::vector<KPKPosition> db(MAX_INDEX);
unsigned idx, repeat = 1;

Expand All @@ -101,6 +102,7 @@ void Bitbases::init() {
for (idx = 0; idx < MAX_INDEX; ++idx)
if (db[idx] == WIN)
KPKBitbase[idx / 32] |= 1 << (idx & 0x1F);
#endif
}


Expand Down
63 changes: 59 additions & 4 deletions src/bitboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,11 @@ inline int popcount(Bitboard b) {

#elif defined(_MSC_VER) || defined(__INTEL_COMPILER)

#ifdef LARGEBOARDS
return (int)_mm_popcnt_u64(uint64_t(b >> 64)) + (int)_mm_popcnt_u64(uint64_t(b));
#else
return (int)_mm_popcnt_u64(b);
#endif

#else // Assumed gcc or compatible compiler

Expand Down Expand Up @@ -417,15 +421,41 @@ inline Square msb(Bitboard b) {
inline Square lsb(Bitboard b) {
assert(b);
unsigned long idx;
#ifdef LARGEBOARDS
if (uint64_t(b))
{
_BitScanForward64(&idx, uint64_t(b));
return Square(idx);
}
else
{
_BitScanForward64(&idx, uint64_t(b >> 64));
return Square(idx + 64);
}
#else
_BitScanForward64(&idx, b);
return (Square) idx;
#endif
}

inline Square msb(Bitboard b) {
assert(b);
unsigned long idx;
#ifdef LARGEBOARDS
if (b >> 64)
{
_BitScanReverse64(&idx, uint64_t(b >> 64));
return Square(idx + 64);
}
else
{
_BitScanReverse64(&idx, uint64_t(b));
return Square(idx);
}
#else
_BitScanReverse64(&idx, b);
return (Square) idx;
#endif
}

#else // MSVC, WIN32
Expand All @@ -434,24 +464,49 @@ inline Square lsb(Bitboard b) {
assert(b);
unsigned long idx;

#ifdef LARGEBOARDS
if (b << 96) {
_BitScanForward(&idx, uint32_t(b));
return Square(idx);
} else if (b << 64) {
_BitScanForward(&idx, uint32_t(b >> 32));
return Square(idx + 32);
} else if (b << 32) {
_BitScanForward(&idx, uint32_t(b >> 64));
return Square(idx + 64);
} else {
_BitScanForward(&idx, uint32_t(b >> 96));
return Square(idx + 96);
}
#else
if (b & 0xffffffff) {
_BitScanForward(&idx, int32_t(b));
_BitScanForward(&idx, uint32_t(b));
return Square(idx);
} else {
_BitScanForward(&idx, int32_t(b >> 32));
_BitScanForward(&idx, uint32_t(b >> 32));
return Square(idx + 32);
}
#endif
}

inline Square msb(Bitboard b) {
assert(b);
unsigned long idx;

#ifdef LARGEBOARDS
if (b >> 96) {
_BitScanReverse(&idx, uint32_t(b >> 96));
return Square(idx + 96);
} else if (b >> 64) {
_BitScanReverse(&idx, uint32_t(b >> 64));
return Square(idx + 64);
} else
#endif
if (b >> 32) {
_BitScanReverse(&idx, int32_t(b >> 32));
_BitScanReverse(&idx, uint32_t(b >> 32));
return Square(idx + 32);
} else {
_BitScanReverse(&idx, int32_t(b));
_BitScanReverse(&idx, uint32_t(b));
return Square(idx);
}
}
Expand Down

0 comments on commit a39deba

Please sign in to comment.