Merge pull request 'Generate only captures in quiescence' (#8) from qs-captures-gen into main

SPRT https://bench.rektdie.com/test/5289/ (non-regression)
Elo   | 66.75 +- 16.38 (95%)
SPRT  | 8.0+0.08s Threads=1 Hash=64MB
LLR   | 3.07 (-2.94, 2.94) [-5.00, 0.00]
Games | N: 764 W: 311 L: 166 D: 287
Penta | [6, 46, 172, 113, 45]

Bench: 546966
This commit was merged in pull request #8.
This commit is contained in:
claude committed 2026-11-15 15:00:51 +01:00
commit 807ddd90c1
3 files changed
+109 -25

No files matched your search

+11 -3
View File
@@ -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];
+93 -22
View File
@@ -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 <bool NOISY>
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<false>(pos, list); }
void generate_noisy(const Position& pos, MoveList& list) { generate<true>(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<false>(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
+5
View File
@@ -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