1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 08:43:09 +00:00

Revert _BitScanForward64 support

It shows almost no improvment and adds a good
bunch of complexity.

So remove for now. No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2009-05-25 07:28:55 +01:00
parent d63ff85a43
commit f1591447cf
2 changed files with 50 additions and 98 deletions

View file

@ -66,7 +66,6 @@ inline bool cpu_has_popcnt() {
} }
#define POPCNT_INTRINSIC(x) __popcnt64(x) #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 #elif defined(__INTEL_COMPILER) && (defined(__x86_64) || defined(_M_X64)) // Intel compiler
@ -80,14 +79,12 @@ inline bool cpu_has_popcnt() {
} }
#define POPCNT_INTRINSIC(x) _mm_popcnt_u64(x) #define POPCNT_INTRINSIC(x) _mm_popcnt_u64(x)
#define BITSCAN_INTRINSIC(idx, x) _BitScanForward64(idx, x)
#else // Safe fallback for unsupported compilers #else // Safe fallback for unsupported compilers
inline bool cpu_has_popcnt() { return false; } inline bool cpu_has_popcnt() { return false; }
#define POPCNT_INTRINSIC(x) count_1s(x) #define POPCNT_INTRINSIC(x) count_1s(x)
#define BITSCAN_INTRINSIC(idx, x) count_1s(x) // dummy
#endif #endif
@ -168,27 +165,6 @@ inline int count_1s_max_15(Bitboard b) {
} }
/// 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);
}
// Global variable initialized at startup that is set to true if // Global variable initialized at startup that is set to true if
// CPU on which application runs supports POPCNT intrinsic. Unless // CPU on which application runs supports POPCNT intrinsic. Unless
// DISABLE_POPCNT_SUPPORT is defined. // DISABLE_POPCNT_SUPPORT is defined.

View file

@ -29,7 +29,7 @@
// Simple macro to wrap a very common while loop, no facny, no flexibility, // Simple macro to wrap a very common while loop, no facny, no flexibility,
// hardcoded list name 'mlist' and from square 'from'. // hardcoded list name 'mlist' and from square 'from'.
#define SERIALIZE_MOVES(b, bsf) while (b) (*mlist++).move = make_move(from, pop_1st_bit<bsf>(&b)) #define SERIALIZE_MOVES(b) while (b) (*mlist++).move = make_move(from, pop_1st_bit(&b))
//// ////
//// Local definitions //// Local definitions
@ -83,22 +83,22 @@ namespace {
} }
// Template generate_piece_checks() with specializations // Template generate_piece_checks() with specializations
template<PieceType, bool HasBSF> template<PieceType>
MoveStack* generate_piece_checks(const Position&, MoveStack*, Color, Bitboard, Square); MoveStack* generate_piece_checks(const Position&, MoveStack*, Color, Bitboard, Square);
template<> template<>
inline MoveStack* generate_piece_checks<PAWN, false>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) { inline MoveStack* generate_piece_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
return (us == WHITE ? generate_pawn_checks<WHITE>(p, dc, ksq, m) return (us == WHITE ? generate_pawn_checks<WHITE>(p, dc, ksq, m)
: generate_pawn_checks<BLACK>(p, dc, ksq, m)); : generate_pawn_checks<BLACK>(p, dc, ksq, m));
} }
// Template generate_piece_moves() with specializations and overloads // Template generate_piece_moves() with specializations and overloads
template<PieceType, bool HasBSF> template<PieceType>
MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard); MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard);
template<> template<>
MoveStack* generate_piece_moves<KING, false>(const Position&, MoveStack*, Color, Bitboard); MoveStack* generate_piece_moves<KING>(const Position&, MoveStack*, Color, Bitboard);
template<PieceType Piece, MoveType Type> template<PieceType Piece, MoveType Type>
inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us) { inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us) {
@ -113,12 +113,12 @@ namespace {
: generate_pawn_noncaptures<BLACK>(p, m)); : generate_pawn_noncaptures<BLACK>(p, m));
} }
template<PieceType, bool HasBSF> template<PieceType>
MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard, Bitboard); MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard, Bitboard);
template<> template<>
inline MoveStack* generate_piece_moves<PAWN, false>(const Position& p, MoveStack* m, inline MoveStack* generate_piece_moves<PAWN>(const Position& p, MoveStack* m,
Color us, Bitboard t, Bitboard pnd) { Color us, Bitboard t, Bitboard pnd) {
return (us == WHITE ? generate_pawn_blocking_evasions<WHITE>(p, pnd, t, m) return (us == WHITE ? generate_pawn_blocking_evasions<WHITE>(p, pnd, t, m)
: generate_pawn_blocking_evasions<BLACK>(p, pnd, t, m)); : generate_pawn_blocking_evasions<BLACK>(p, pnd, t, m));
@ -133,7 +133,7 @@ namespace {
/// generate_captures generates() all pseudo-legal captures and queen /// generate_captures generates() all pseudo-legal captures and queen
/// promotions. The return value is the number of moves generated. /// promotions. The return value is the number of moves generated.
template<bool HasBSF>
int generate_captures(const Position& pos, MoveStack* mlist) { int generate_captures(const Position& pos, MoveStack* mlist) {
assert(pos.is_ok()); assert(pos.is_ok());
@ -143,25 +143,19 @@ int generate_captures(const Position& pos, MoveStack* mlist) {
Bitboard target = pos.pieces_of_color(opposite_color(us)); Bitboard target = pos.pieces_of_color(opposite_color(us));
MoveStack* mlist_start = mlist; MoveStack* mlist_start = mlist;
mlist = generate_piece_moves<QUEEN, HasBSF>(pos, mlist, us, target); mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
mlist = generate_piece_moves<ROOK, HasBSF>(pos, mlist, us, target); mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
mlist = generate_piece_moves<BISHOP, HasBSF>(pos, mlist, us, target); mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
mlist = generate_piece_moves<KNIGHT, HasBSF>(pos, mlist, us, target); mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
mlist = generate_piece_moves<PAWN, CAPTURE>(pos, mlist, us); mlist = generate_piece_moves<PAWN, CAPTURE>(pos, mlist, us);
mlist = generate_piece_moves<KING, false>(pos, mlist, us, target); mlist = generate_piece_moves<KING>(pos, mlist, us, target);
return int(mlist - mlist_start); return int(mlist - mlist_start);
} }
int generate_captures(const Position& pos, MoveStack* mlist) {
return CpuHasPOPCNT ? generate_captures<true>(pos, mlist)
: generate_captures<false>(pos, mlist);
}
/// generate_noncaptures() generates all pseudo-legal non-captures and /// generate_noncaptures() generates all pseudo-legal non-captures and
/// underpromotions. The return value is the number of moves generated. /// underpromotions. The return value is the number of moves generated.
template<bool HasBSF>
int generate_noncaptures(const Position& pos, MoveStack* mlist) { int generate_noncaptures(const Position& pos, MoveStack* mlist) {
assert(pos.is_ok()); assert(pos.is_ok());
@ -172,26 +166,20 @@ int generate_noncaptures(const Position& pos, MoveStack* mlist) {
MoveStack* mlist_start = mlist; MoveStack* mlist_start = mlist;
mlist = generate_piece_moves<PAWN, NON_CAPTURE>(pos, mlist, us); mlist = generate_piece_moves<PAWN, NON_CAPTURE>(pos, mlist, us);
mlist = generate_piece_moves<KNIGHT, HasBSF>(pos, mlist, us, target); mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
mlist = generate_piece_moves<BISHOP, HasBSF>(pos, mlist, us, target); mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
mlist = generate_piece_moves<ROOK, HasBSF>(pos, mlist, us, target); mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
mlist = generate_piece_moves<QUEEN, HasBSF>(pos, mlist, us, target); mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
mlist = generate_piece_moves<KING, false>(pos, mlist, us, target); mlist = generate_piece_moves<KING>(pos, mlist, us, target);
mlist = generate_castle_moves<KING_SIDE>(pos, mlist); mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist); mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
return int(mlist - mlist_start); return int(mlist - mlist_start);
} }
int generate_noncaptures(const Position& pos, MoveStack* mlist) {
return CpuHasPOPCNT ? generate_noncaptures<true>(pos, mlist)
: generate_noncaptures<false>(pos, mlist);
}
/// generate_non_capture_checks() generates all pseudo-legal non-capturing, /// generate_non_capture_checks() generates all pseudo-legal non-capturing,
/// non-promoting checks. It returns the number of generated moves. /// non-promoting checks. It returns the number of generated moves.
template<bool HasBSF>
int generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard dc) { int generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
assert(pos.is_ok()); assert(pos.is_ok());
@ -204,12 +192,12 @@ int generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard
assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING)); assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING));
// Pieces moves // Pieces moves
mlist = generate_piece_checks<PAWN, false>(pos, mlist, us, dc, ksq); mlist = generate_piece_checks<PAWN>(pos, mlist, us, dc, ksq);
mlist = generate_piece_checks<KNIGHT, HasBSF>(pos, mlist, us, dc, ksq); mlist = generate_piece_checks<KNIGHT>(pos, mlist, us, dc, ksq);
mlist = generate_piece_checks<BISHOP, HasBSF>(pos, mlist, us, dc, ksq); mlist = generate_piece_checks<BISHOP>(pos, mlist, us, dc, ksq);
mlist = generate_piece_checks<ROOK, HasBSF>(pos, mlist, us, dc, ksq); mlist = generate_piece_checks<ROOK>(pos, mlist, us, dc, ksq);
mlist = generate_piece_checks<QUEEN, HasBSF>(pos, mlist, us, dc, ksq); mlist = generate_piece_checks<QUEEN>(pos, mlist, us, dc, ksq);
mlist = generate_piece_checks<KING, false>(pos, mlist, us, dc, ksq); mlist = generate_piece_checks<KING>(pos, mlist, us, dc, ksq);
// Castling moves that give check. Very rare but nice to have! // Castling moves that give check. Very rare but nice to have!
if ( pos.can_castle_queenside(us) if ( pos.can_castle_queenside(us)
@ -225,17 +213,11 @@ int generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard
return int(mlist - mlist_start); return int(mlist - mlist_start);
} }
int generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
return CpuHasPOPCNT ? generate_non_capture_checks<true>(pos, mlist, dc)
: generate_non_capture_checks<false>(pos, mlist, dc);
}
/// generate_evasions() generates all check evasions when the side to move is /// generate_evasions() generates all check evasions when the side to move is
/// in check. Unlike the other move generation functions, this one generates /// in check. Unlike the other move generation functions, this one generates
/// only legal moves. It returns the number of generated moves. /// only legal moves. It returns the number of generated moves.
template<bool HasBSF>
int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) { int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
assert(pos.is_ok()); assert(pos.is_ok());
@ -262,14 +244,14 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
Bitboard b = checkers & (pos.queens() | pos.bishops()); Bitboard b = checkers & (pos.queens() | pos.bishops());
while (b) while (b)
{ {
from = pop_1st_bit<HasBSF>(&b); from = pop_1st_bit(&b);
checkersAttacks |= bishop_attacks_bb(from, b_noKing); checkersAttacks |= bishop_attacks_bb(from, b_noKing);
} }
b = checkers & (pos.queens() | pos.rooks()); b = checkers & (pos.queens() | pos.rooks());
while (b) while (b)
{ {
from = pop_1st_bit<HasBSF>(&b); from = pop_1st_bit(&b);
checkersAttacks |= rook_attacks_bb(from, b_noKing); checkersAttacks |= rook_attacks_bb(from, b_noKing);
} }
@ -277,7 +259,7 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us) & ~checkersAttacks; Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us) & ~checkersAttacks;
while (b1) while (b1)
{ {
to = pop_1st_bit<HasBSF>(&b1); to = pop_1st_bit(&b1);
// Note that we can use square_is_attacked() only because we // Note that we can use square_is_attacked() only because we
// have already removed slider checkers. // have already removed slider checkers.
if (!pos.square_is_attacked(to, them)) if (!pos.square_is_attacked(to, them))
@ -299,7 +281,7 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & ~pinned; b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & ~pinned;
while (b1) while (b1)
{ {
from = pop_1st_bit<HasBSF>(&b1); from = pop_1st_bit(&b1);
if (relative_rank(us, checksq) == RANK_8) if (relative_rank(us, checksq) == RANK_8)
{ {
(*mlist++).move = make_promotion_move(from, checksq, QUEEN); (*mlist++).move = make_promotion_move(from, checksq, QUEEN);
@ -317,7 +299,7 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
while (b1) while (b1)
{ {
from = pop_1st_bit<HasBSF>(&b1); from = pop_1st_bit(&b1);
(*mlist++).move = make_move(from, checksq); (*mlist++).move = make_move(from, checksq);
} }
@ -331,11 +313,11 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
if (blockSquares != EmptyBoardBB) if (blockSquares != EmptyBoardBB)
{ {
mlist = generate_piece_moves<PAWN, false>(pos, mlist, us, blockSquares, pinned); mlist = generate_piece_moves<PAWN>(pos, mlist, us, blockSquares, pinned);
mlist = generate_piece_moves<KNIGHT, HasBSF>(pos, mlist, us, blockSquares, pinned); mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, blockSquares, pinned);
mlist = generate_piece_moves<BISHOP, HasBSF>(pos, mlist, us, blockSquares, pinned); mlist = generate_piece_moves<BISHOP>(pos, mlist, us, blockSquares, pinned);
mlist = generate_piece_moves<ROOK, HasBSF>(pos, mlist, us, blockSquares, pinned); mlist = generate_piece_moves<ROOK>(pos, mlist, us, blockSquares, pinned);
mlist = generate_piece_moves<QUEEN, HasBSF>(pos, mlist, us, blockSquares, pinned); mlist = generate_piece_moves<QUEEN>(pos, mlist, us, blockSquares, pinned);
} }
} }
@ -357,7 +339,7 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
b1 &= ~pinned; b1 &= ~pinned;
while (b1) while (b1)
{ {
from = pop_1st_bit<HasBSF>(&b1); from = pop_1st_bit(&b1);
// Move is always legal because checking pawn is not a discovered // Move is always legal because checking pawn is not a discovered
// check candidate and our capturing pawn has been already tested // check candidate and our capturing pawn has been already tested
// against pinned pieces. // against pinned pieces.
@ -368,12 +350,6 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
return int(mlist - mlist_start); return int(mlist - mlist_start);
} }
int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
return CpuHasPOPCNT ? generate_evasions<true>(pos, mlist, pinned)
: generate_evasions<false>(pos, mlist, pinned);
}
/// generate_legal_moves() computes a complete list of legal moves in the /// generate_legal_moves() computes a complete list of legal moves in the
/// current position. This function is not very fast, and should be used /// current position. This function is not very fast, and should be used
@ -590,7 +566,7 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
namespace { namespace {
template<PieceType Piece, bool HasBSF> template<PieceType Piece>
MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) { MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
Square from; Square from;
@ -600,12 +576,12 @@ namespace {
{ {
from = pos.piece_list(us, Piece, i); from = pos.piece_list(us, Piece, i);
b = pos.piece_attacks<Piece>(from) & target; b = pos.piece_attacks<Piece>(from) & target;
SERIALIZE_MOVES(b, HasBSF); SERIALIZE_MOVES(b);
} }
return mlist; return mlist;
} }
template<PieceType Piece, bool HasBSF> template<PieceType Piece>
MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist,
Color us, Bitboard target, Bitboard pinned) { Color us, Bitboard target, Bitboard pinned) {
Square from; Square from;
@ -618,19 +594,19 @@ namespace {
continue; continue;
b = pos.piece_attacks<Piece>(from) & target; b = pos.piece_attacks<Piece>(from) & target;
SERIALIZE_MOVES(b, HasBSF); SERIALIZE_MOVES(b);
} }
return mlist; return mlist;
} }
template<> template<>
MoveStack* generate_piece_moves<KING, false>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) { MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
Bitboard b; Bitboard b;
Square from = pos.king_square(us); Square from = pos.king_square(us);
b = pos.piece_attacks<KING>(from) & target; b = pos.piece_attacks<KING>(from) & target;
SERIALIZE_MOVES(b, false); SERIALIZE_MOVES(b);
return mlist; return mlist;
} }
@ -840,7 +816,7 @@ namespace {
return mlist; return mlist;
} }
template<PieceType Piece, bool HasBSF> template<PieceType Piece>
MoveStack* generate_piece_checks(const Position& pos, MoveStack* mlist, Color us, MoveStack* generate_piece_checks(const Position& pos, MoveStack* mlist, Color us,
Bitboard dc, Square ksq) { Bitboard dc, Square ksq) {
@ -850,12 +826,12 @@ namespace {
Bitboard b = target & dc; Bitboard b = target & dc;
while (b) while (b)
{ {
Square from = pop_1st_bit<HasBSF>(&b); Square from = pop_1st_bit(&b);
Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares(); Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
if (Piece == KING) if (Piece == KING)
bb &= ~QueenPseudoAttacks[ksq]; bb &= ~QueenPseudoAttacks[ksq];
SERIALIZE_MOVES(bb, HasBSF); SERIALIZE_MOVES(bb);
} }
// Direct checks // Direct checks
@ -868,14 +844,14 @@ namespace {
while (b) while (b)
{ {
Square from = pop_1st_bit<HasBSF>(&b); Square from = pop_1st_bit(&b);
if ( (Piece == QUEEN && !(QueenPseudoAttacks[from] & checkSqs)) if ( (Piece == QUEEN && !(QueenPseudoAttacks[from] & checkSqs))
|| (Piece == ROOK && !(RookPseudoAttacks[from] & checkSqs)) || (Piece == ROOK && !(RookPseudoAttacks[from] & checkSqs))
|| (Piece == BISHOP && !(BishopPseudoAttacks[from] & checkSqs))) || (Piece == BISHOP && !(BishopPseudoAttacks[from] & checkSqs)))
continue; continue;
Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs; Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
SERIALIZE_MOVES(bb, HasBSF); SERIALIZE_MOVES(bb);
} }
} }
return mlist; return mlist;