X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fbitcount.h;h=41a1446e01b700b7d6fa08b9425b1a199a2a9122;hp=c6e969a82411d4b37dc1633e512ca5873d4a2912;hb=f90f810ac4de5ea2f5582ca05a9354c33971a953;hpb=ce5d9eb19da890c77d8ef00e078c60edc3e8e4aa diff --git a/src/bitcount.h b/src/bitcount.h index c6e969a8..41a1446e 100644 --- a/src/bitcount.h +++ b/src/bitcount.h @@ -66,6 +66,7 @@ inline bool cpu_has_popcnt() { } #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 @@ -79,12 +80,14 @@ inline bool cpu_has_popcnt() { } #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 @@ -183,4 +186,25 @@ const bool CpuHas64BitPath = true; 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 +inline Square pop_1st_bit(Bitboard *b) { + + return pop_1st_bit(b); +} + +template<> +inline Square pop_1st_bit(Bitboard *b) { + + unsigned long idx; + Bitboard bb = *b; + BITSCAN_INTRINSIC(&idx, bb); + *b &= (bb - 1); + return Square(idx); +} + #endif // !defined(BITCOUNT_H_INCLUDED)