-// Select type of intrinsic bit count instruction to use, see
-// README.txt on how to pgo compile with POPCNT support.
-#if !defined(USE_POPCNT)
-#define POPCNT_INTRINSIC(x) 0
-#elif defined(_MSC_VER)
-#define POPCNT_INTRINSIC(x) (int)__popcnt64(x)
-#elif defined(__GNUC__)
-
-#define POPCNT_INTRINSIC(x) ({ \
- unsigned long __ret; \
- __asm__("popcnt %1, %0" : "=r" (__ret) : "r" (x)); \
- __ret; })
-
-#endif
-
-
-/// Software implementation of bit count functions
-
-#if defined(IS_64BIT)
-
-inline int count_1s(Bitboard b) {
- b -= ((b>>1) & 0x5555555555555555ULL);
- b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
- b = ((b>>4) + b) & 0x0F0F0F0F0F0F0F0FULL;
- b *= 0x0101010101010101ULL;
- return int(b >> 56);
+enum BitCountType {
+ CNT_64,
+ CNT_64_MAX15,
+ CNT_32,
+ CNT_32_MAX15,
+ CNT_HW_POPCNT
+};
+
+/// Determine at compile time the best popcount<> specialization according to
+/// whether the platform is 32 or 64 bit, the maximum number of non-zero
+/// bits to count and if the hardware popcnt instruction is available.
+const BitCountType Full = HasPopCnt ? CNT_HW_POPCNT : Is64Bit ? CNT_64 : CNT_32;
+const BitCountType Max15 = HasPopCnt ? CNT_HW_POPCNT : Is64Bit ? CNT_64_MAX15 : CNT_32_MAX15;
+
+
+/// popcount() counts the number of non-zero bits in a bitboard
+template<BitCountType> inline int popcount(Bitboard);
+
+template<>
+inline int popcount<CNT_64>(Bitboard b) {
+ b -= (b >> 1) & 0x5555555555555555ULL;
+ b = ((b >> 2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
+ b = ((b >> 4) + b) & 0x0F0F0F0F0F0F0F0FULL;
+ return (b * 0x0101010101010101ULL) >> 56;