X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fbitboard.h;h=85852e5e5b8567557a75ac289025073a2df61ef3;hb=d6e3a40c819d29db00866bc7f09b9315dff9b95a;hp=241970f834c398b765069d89f261185e700db6d4;hpb=96eefc4af65204cd2ddad64ac53b8d449f49efa5;p=stockfish diff --git a/src/bitboard.h b/src/bitboard.h index 241970f8..85852e5e 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -207,6 +207,14 @@ inline Bitboard same_color_squares(Square s) { } +/// single_bit() returns true if in the 'b' bitboard is set a single bit (or if +/// b == 0). + +inline bool single_bit(Bitboard b) { + return !(b & (b - 1)); +} + + /// first_1() finds the least significant nonzero bit in a nonzero bitboard. /// pop_1st_bit() finds and clears the least significant nonzero bit in a /// nonzero bitboard. @@ -216,9 +224,15 @@ inline Bitboard same_color_squares(Square s) { #if defined(_MSC_VER) && !defined(__INTEL_COMPILER) FORCE_INLINE Square first_1(Bitboard b) { - unsigned long index; - _BitScanForward64(&index, b); - return (Square) index; + unsigned long index; + _BitScanForward64(&index, b); + return (Square) index; +} + +FORCE_INLINE Square last_1(Bitboard b) { + unsigned long index; + _BitScanReverse64(&index, b); + return (Square) index; } #else @@ -227,6 +241,12 @@ FORCE_INLINE Square first_1(Bitboard b) { // Assembly code by Heinz van Saanen __asm__("bsfq %1, %0": "=r"(dummy): "rm"(b) ); return (Square) dummy; } + +FORCE_INLINE Square last_1(Bitboard b) { + Bitboard dummy; + __asm__("bsrq %1, %0": "=r"(dummy): "rm"(b) ); + return (Square) dummy; +} #endif FORCE_INLINE Square pop_1st_bit(Bitboard* b) { @@ -238,10 +258,20 @@ FORCE_INLINE Square pop_1st_bit(Bitboard* b) { #else // if !defined(USE_BSFQ) extern Square first_1(Bitboard b); +extern Square last_1(Bitboard b); extern Square pop_1st_bit(Bitboard* b); #endif +// relative_rank() returns the relative rank of the closest bit set on the Bitboard. +// Only to be used with bitboards that contain a single file. + +template +inline Rank relative_rank(Bitboard b) { + Square s = Us == WHITE ? first_1(b) + : ~last_1(b); + return rank_of(s); +} extern void print_bitboard(Bitboard b); extern void bitboards_init();