From 695d2dd30da8f3068e5566d412ad88e379a4be12 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 15 Nov 2026 14:52:58 +0100 Subject: [PATCH] perf: generate only captures in quiescence Out of check the quiescence search only searches captures and queen promotions, but every node generated all legal moves and threw the quiet ones away. It now generates just those (generate_noisy: the same legal generator with captures-only targets, no quiet pawn pushes, no castling), in the same order as the full generation, so search is unchanged. A node without any noisy move still has to tell stalemate from a quiet position; has_legal_move answers that and stops at the first legal move it finds. Playouts per second at 100k playouts, main -> this (two runs each): startpos 631k -> 725k +15% QGD middlegame 390k -> 446k +14% Kiwipete 106k -> 142k +34% Sicilian middle 334k -> 423k +27% rook endgame 851k -> 982k +15% Checked: perftsuite 7 passes. On 252,893 positions from random games (captures preferred, so they reach endgames, promotions and 400 positions without legal moves) generate_noisy equals generate_legal filtered to captures and queen promotions, in the same order, and has_legal_move equals a non-empty generate_legal. The bench is identical to main. Bench: 546966 Co-Authored-By: Claude Opus 5.5 --- src/eval.cpp | 14 ++++-- src/movegen.cpp | 115 +++++++++++++++++++++++++++++++++++++++--------- src/position.h | 5 +++ 3 files changed, 109 insertions(+), 25 deletions(-) diff --git a/src/eval.cpp b/src/eval.cpp index 9db1c43..612f14e 100644 --- a/src/eval.cpp +++ b/src/eval.cpp @@ -128,10 +128,18 @@ int quiescence(const Position& pos, int alpha, int beta, int ply) { alpha = std::max(alpha, stand_pat); } + // Out of check only captures and queen promotions are searched, so only + // those are generated; a position without any of them is still checked + // for stalemate. MoveList list; - generate_legal(pos, list); - if (list.empty()) return in_check ? -MATE + ply : 0; - if (in_check && ply >= QS_MAX_PLY) return static_eval(pos); + if (in_check) { + generate_legal(pos, list); + if (list.empty()) return -MATE + ply; + if (ply >= QS_MAX_PLY) return static_eval(pos); + } else { + generate_noisy(pos, list); + if (list.empty()) return has_legal_move(pos) ? best : 0; + } Move moves[256]; int scores[256]; diff --git a/src/movegen.cpp b/src/movegen.cpp index 8a21d77..e17778a 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -21,10 +21,25 @@ void add_pawn_move(MoveList& list, int from, int to, bool capture, bool promotio } } -} // namespace +// Our pieces pinned to our king by an enemy slider. +u64 pinned_pieces(const Position& pos, int us, int ksq, u64 occ) { + const u64 diag_sliders = pos.pieces[BISHOP] | pos.pieces[QUEEN]; + const u64 orth_sliders = pos.pieces[ROOK] | pos.pieces[QUEEN]; + u64 pinned = 0; + u64 snipers = ((attacks::rook(ksq, 0) & orth_sliders) | (attacks::bishop(ksq, 0) & diag_sliders)) & pos.colors[us ^ 1]; + while (snipers) { + const int s = pop_lsb(snipers); + const u64 blockers = attacks::between[ksq][s] & occ; + if (blockers && !(blockers & (blockers - 1)) && (blockers & pos.colors[us])) pinned |= blockers; + } + return pinned; +} -// Fully legal generation using check and pin masks. -void generate_legal(const Position& pos, MoveList& list) { +// Fully legal generation using check and pin masks. With NOISY only captures +// (en passant included) and queen promotions, in the same order as the full +// generation lists them. +template +void generate(const Position& pos, MoveList& list) { list.clear(); const int us = pos.stm; @@ -40,7 +55,7 @@ void generate_legal(const Position& pos, MoveList& list) { // King moves: the king itself must not block a slider ray on its destination. { const u64 occ_no_king = occ ^ bb(ksq); - u64 targets = attacks::king[ksq] & ~ours; + u64 targets = attacks::king[ksq] & (NOISY ? theirs : ~ours); while (targets) { const int to = pop_lsb(targets); if (!pos.square_attacked(to, them, occ_no_king)) @@ -51,30 +66,23 @@ void generate_legal(const Position& pos, MoveList& list) { if (num_checkers > 1) return; const u64 check_mask = num_checkers ? (attacks::between[ksq][lsb(checkers)] | checkers) : ~0ULL; + // Squares a piece may move to: anything but our own pieces, or only theirs. + const u64 target_mask = check_mask & (NOISY ? theirs : ~ours); const u64 diag_sliders = pos.pieces[BISHOP] | pos.pieces[QUEEN]; const u64 orth_sliders = pos.pieces[ROOK] | pos.pieces[QUEEN]; - - u64 pinned = 0; - { - u64 snipers = ((attacks::rook(ksq, 0) & orth_sliders) | (attacks::bishop(ksq, 0) & diag_sliders)) & theirs; - while (snipers) { - const int s = pop_lsb(snipers); - const u64 blockers = attacks::between[ksq][s] & occ; - if (blockers && !(blockers & (blockers - 1)) && (blockers & ours)) pinned |= blockers; - } - } + const u64 pinned = pinned_pieces(pos, us, ksq, occ); // A pinned knight can never move. u64 knights = pos.pieces_of(us, KNIGHT) & ~pinned; while (knights) { const int from = pop_lsb(knights); - add_targets(list, from, attacks::knight[from] & ~ours & check_mask, theirs); + add_targets(list, from, attacks::knight[from] & target_mask, theirs); } u64 diag = diag_sliders & ours; while (diag) { const int from = pop_lsb(diag); - u64 targets = attacks::bishop(from, occ) & ~ours & check_mask; + u64 targets = attacks::bishop(from, occ) & target_mask; if (pinned & bb(from)) targets &= attacks::line[ksq][from]; add_targets(list, from, targets, theirs); } @@ -82,7 +90,7 @@ void generate_legal(const Position& pos, MoveList& list) { u64 orth = orth_sliders & ours; while (orth) { const int from = pop_lsb(orth); - u64 targets = attacks::rook(from, occ) & ~ours & check_mask; + u64 targets = attacks::rook(from, occ) & target_mask; if (pinned & bb(from)) targets &= attacks::line[ksq][from]; add_targets(list, from, targets, theirs); } @@ -99,10 +107,14 @@ void generate_legal(const Position& pos, MoveList& list) { const int to = from + up; if (!(occ & bb(to))) { - if (allowed & bb(to)) add_pawn_move(list, from, to, false, promo_rank & bb(to)); - const int to2 = to + up; - if ((start_rank & bb(from)) && !(occ & bb(to2)) && (allowed & bb(to2))) - list.add(Move(from, to2, Move::DOUBLE_PUSH)); + if constexpr (NOISY) { + if ((allowed & bb(to)) && (promo_rank & bb(to))) list.add(Move(from, to, Move::PROMO + 3)); + } else { + if (allowed & bb(to)) add_pawn_move(list, from, to, false, promo_rank & bb(to)); + const int to2 = to + up; + if ((start_rank & bb(from)) && !(occ & bb(to2)) && (allowed & bb(to2))) + list.add(Move(from, to2, Move::DOUBLE_PUSH)); + } } u64 captures = attacks::pawn[us][from] & theirs & allowed; @@ -124,7 +136,7 @@ void generate_legal(const Position& pos, MoveList& list) { } } - if (num_checkers) return; + if (NOISY || num_checkers) return; const int rank_base = us == WHITE ? 0 : 56; const int oo = us == WHITE ? WHITE_OO : BLACK_OO; @@ -142,4 +154,63 @@ void generate_legal(const Position& pos, MoveList& list) { list.add(Move(ksq, rank_base + 2, Move::QUEEN_CASTLE)); } +} // namespace + +void generate_legal(const Position& pos, MoveList& list) { generate(pos, list); } + +void generate_noisy(const Position& pos, MoveList& list) { generate(pos, list); } + +bool has_legal_move(const Position& pos) { + const int us = pos.stm; + const int them = us ^ 1; + const u64 occ = pos.occupancy(); + const u64 ours = pos.colors[us]; + const int ksq = pos.king_square(us); + + // In check or with an en passant capture around, just generate everything. + if (pos.in_check() || pos.ep != NO_SQUARE) { + MoveList list; + generate(pos, list); + return !list.empty(); + } + + // Not in check, so castling only adds moves when the king can already step + // to the square next to it; the king, the pieces and the pawns decide. + const u64 pinned = pinned_pieces(pos, us, ksq, occ); + + u64 knights = pos.pieces_of(us, KNIGHT) & ~pinned; + while (knights) + if (attacks::knight[pop_lsb(knights)] & ~ours) return true; + + u64 targets = attacks::king[ksq] & ~ours; + while (targets) + if (!pos.square_attacked(pop_lsb(targets), them, occ ^ bb(ksq))) return true; + + u64 sliders = (pos.pieces[BISHOP] | pos.pieces[QUEEN]) & ours; + while (sliders) { + const int from = pop_lsb(sliders); + u64 t = attacks::bishop(from, occ) & ~ours; + if (pinned & bb(from)) t &= attacks::line[ksq][from]; + if (t) return true; + } + sliders = (pos.pieces[ROOK] | pos.pieces[QUEEN]) & ours; + while (sliders) { + const int from = pop_lsb(sliders); + u64 t = attacks::rook(from, occ) & ~ours; + if (pinned & bb(from)) t &= attacks::line[ksq][from]; + if (t) return true; + } + + const int up = us == WHITE ? 8 : -8; + u64 pawns = pos.pieces_of(us, PAWN); + while (pawns) { + const int from = pop_lsb(pawns); + u64 t = attacks::pawn[us][from] & pos.colors[them]; + if (!(occ & bb(from + up))) t |= bb(from + up); + if (pinned & bb(from)) t &= attacks::line[ksq][from]; + if (t) return true; + } + return false; +} + } // namespace rigby diff --git a/src/position.h b/src/position.h index 38d869d..ecdf874 100644 --- a/src/position.h +++ b/src/position.h @@ -54,5 +54,10 @@ private: }; void generate_legal(const Position& pos, MoveList& list); +// Legal captures (en passant included) and queen promotions, in the order +// generate_legal lists them. +void generate_noisy(const Position& pos, MoveList& list); +// Same as a non-empty generate_legal, but stops at the first move it finds. +bool has_legal_move(const Position& pos); } // namespace rigby