#if !defined(BITCOUNT_H_INCLUDED)
#define BITCOUNT_H_INCLUDED
+// To disable POPCNT support uncomment following line. You should do it only
+// in PGO compiling to exercise the default fallback path. Don't forget to
+// re-comment the line for the final optimized compile though ;-)
+//#define DISABLE_POPCNT_SUPPORT
+
+
#include "bitboard.h"
// Select type of intrinsic bit count instruction to use
-#if defined(_MSC_VER) // Microsoft compiler
+#if defined(_MSC_VER) && defined(_WIN64) // Microsoft compiler
#include <intrin.h>
}
#define POPCNT_INTRINSIC(x) __popcnt64(x)
+#define BITSCAN_INTRINSIC(idx, x) _BitScanForward64(idx, x)
#elif defined(__INTEL_COMPILER) && (defined(__x86_64) || defined(_M_X64)) // Intel compiler
}
#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
// Global variable initialized at startup that is set to true if
-// CPU on which application runs support POPCNT intrinsic.
-
+// CPU on which application runs supports POPCNT intrinsic. Unless
+// DISABLE_POPCNT_SUPPORT is defined.
+#if defined(DISABLE_POPCNT_SUPPORT)
+const bool CpuHasPOPCNT = false;
+#else
const bool CpuHasPOPCNT = cpu_has_popcnt();
+#endif
+
+
+// Global variable used to print info about the use of 64 optimized
+// functions to verify that a 64bit compile has been correctly built.
+#if defined(BITCOUNT_SWAR_64)
+const bool CpuHas64BitPath = true;
+#else
+const bool CpuHas64BitPath = false;
+#endif
+
+
+/// pop_1st_bit() finds and clears the least significant nonzero bit in a
+/// nonzero bitboard. If template parameter is true an intrinsic is called,
+/// otherwise we fallback on a software implementation.
+
+template<bool UseIntrinsic>
+inline Square pop_1st_bit(Bitboard *b) {
+
+ return pop_1st_bit(b);
+}
+
+template<>
+inline Square pop_1st_bit<true>(Bitboard *b) {
+
+ unsigned long idx;
+ Bitboard bb = *b;
+ BITSCAN_INTRINSIC(&idx, bb);
+ *b &= (bb - 1);
+ return Square(idx);
+}
#endif // !defined(BITCOUNT_H_INCLUDED)