mirror of
https://github.com/HChaZZY/Stockfish.git
synced 2025-12-17 15:46:24 +08:00
Use bsfq asm instruction to count bits
On 64 bit systems we can use bsfq instruction to count set bits in a bitboard. This is a patch for GCC and Intel compilers to take advantage of that and get a 2% speed up. Original patch from Heinz van Saanen, adapted to current tree by me. No functional change. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
@@ -161,7 +161,7 @@ const int RShift[64] = {
|
|||||||
21, 22, 22, 22, 22, 22, 22, 21, 20, 21, 21, 21, 21, 21, 21, 20
|
21, 22, 22, 22, 22, 22, 22, 21, 20, 21, 21, 21, 21, 21, 21, 20
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif // defined(IS_64BIT)
|
||||||
|
|
||||||
|
|
||||||
Bitboard RMask[64];
|
Bitboard RMask[64];
|
||||||
@@ -245,7 +245,7 @@ void init_bitboards() {
|
|||||||
/// pop_1st_bit() finds and clears the least significant nonzero bit in a
|
/// pop_1st_bit() finds and clears the least significant nonzero bit in a
|
||||||
/// nonzero bitboard.
|
/// nonzero bitboard.
|
||||||
|
|
||||||
#if defined(IS_64BIT)
|
#if defined(IS_64BIT) && !defined(USE_BSFQ)
|
||||||
|
|
||||||
Square pop_1st_bit(Bitboard* b) {
|
Square pop_1st_bit(Bitboard* b) {
|
||||||
Bitboard bb = *b ^ (*b - 1);
|
Bitboard bb = *b ^ (*b - 1);
|
||||||
@@ -254,7 +254,7 @@ Square pop_1st_bit(Bitboard *b) {
|
|||||||
return Square(BitTable[(fold * 0x783a9b23) >> 26]);
|
return Square(BitTable[(fold * 0x783a9b23) >> 26]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#elif !defined(USE_BSFQ)
|
||||||
|
|
||||||
// Use type-punning
|
// Use type-punning
|
||||||
union b_union {
|
union b_union {
|
||||||
|
|||||||
@@ -36,6 +36,11 @@
|
|||||||
#define IS_64BIT
|
#define IS_64BIT
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(IS_64BIT) && (defined(__GNUC__) || defined(__INTEL_COMPILER))
|
||||||
|
#define USE_BSFQ
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
////
|
////
|
||||||
//// Includes
|
//// Includes
|
||||||
////
|
////
|
||||||
@@ -383,14 +388,24 @@ inline Bitboard isolated_pawn_mask(Square s) {
|
|||||||
|
|
||||||
|
|
||||||
/// first_1() finds the least significant nonzero bit in a nonzero bitboard.
|
/// 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.
|
||||||
|
|
||||||
#if defined(IS_64BIT)
|
#if defined(USE_BSFQ) // Assembly code by Heinz van Saanen
|
||||||
|
|
||||||
inline Square first_1(Bitboard b) {
|
inline Square __attribute__((always_inline)) first_1(Bitboard b) {
|
||||||
return Square(BitTable[((b & -b) * 0x218a392cd3d5dbfULL) >> 58]);
|
Bitboard dummy;
|
||||||
|
__asm__("bsfq %1, %0": "=r"(dummy): "rm"(b) );
|
||||||
|
return (Square)(dummy);
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
inline Square __attribute__((always_inline)) pop_1st_bit(Bitboard* b) {
|
||||||
|
const Square s = first_1(*b);
|
||||||
|
*b &= ~(1ULL<<s);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
#else // if !defined(USE_BSFQ)
|
||||||
|
|
||||||
inline Square first_1(Bitboard b) {
|
inline Square first_1(Bitboard b) {
|
||||||
b ^= (b - 1);
|
b ^= (b - 1);
|
||||||
@@ -398,6 +413,8 @@ inline Square first_1(Bitboard b) {
|
|||||||
return Square(BitTable[(fold * 0x783a9b23) >> 26]);
|
return Square(BitTable[(fold * 0x783a9b23) >> 26]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern Square pop_1st_bit(Bitboard* b);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@@ -407,7 +424,6 @@ inline Square first_1(Bitboard b) {
|
|||||||
|
|
||||||
extern void print_bitboard(Bitboard b);
|
extern void print_bitboard(Bitboard b);
|
||||||
extern void init_bitboards();
|
extern void init_bitboards();
|
||||||
extern Square pop_1st_bit(Bitboard *b);
|
|
||||||
|
|
||||||
|
|
||||||
#endif // !defined(BITBOARD_H_INCLUDED)
|
#endif // !defined(BITBOARD_H_INCLUDED)
|
||||||
|
|||||||
Reference in New Issue
Block a user