-inline bool cpu_has_popcnt() {
-
- int CPUInfo[4] = {-1};
- __cpuid(CPUInfo, 0x00000001);
- return (CPUInfo[2] >> 23) & 1;
-}
-
-#define POPCNT_INTRINSIC(x) _mm_popcnt_u64(x)
-#define BITSCAN_INTRINSIC(idx, x) _BitScanForward64(idx, x)
-
-#else // Safe fallback for unsupported compilers
-
-inline bool cpu_has_popcnt() { return false; }
-
-#define POPCNT_INTRINSIC(x) sw_count_1s(x)
-#define BITSCAN_INTRINSIC(idx, x) sw_count_1s(x) // dummy
-
-#endif
-
-
-/// Software implementation of bit count functions
-
-#if defined(BITCOUNT_LOOP)
-
-inline int sw_count_1s(Bitboard b) {
- int r;
- for(r = 0; b; r++, b &= b - 1);
- return r;
+template<>
+inline int popcount<CNT_64>(Bitboard b) {
+ b -= ((b>>1) & 0x5555555555555555ULL);
+ b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
+ b = ((b>>4) + b) & 0x0F0F0F0F0F0F0F0FULL;
+ b *= 0x0101010101010101ULL;
+ return int(b >> 56);