Files
Rigby/src/position.h
T
rektdieandclaude 695d2dd30d 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 <noreply@anthropic.com>
2026-11-15 14:52:58 +01:00

64 lines
1.8 KiB
C++

#pragma once
#include <string>
#include <string_view>
#include "bitboard.h"
#include "types.h"
namespace rigby {
namespace zobrist {
void init();
}
// Plain copy-make position. Small enough to copy at every tree step.
struct Position {
u64 pieces[6] = {};
u64 colors[2] = {};
u8 board[64];
int stm = WHITE;
int castling = 0;
int ep = NO_SQUARE; // only set when a capture there is pseudo-legal
int halfmove = 0;
int fullmove = 1;
u64 key = 0;
Position();
bool set_fen(std::string_view fen);
std::string fen() const;
void make_move(Move m);
u64 occupancy() const { return colors[WHITE] | colors[BLACK]; }
u64 pieces_of(int color, int type) const { return pieces[type] & colors[color]; }
int king_square(int color) const { return lsb(pieces_of(color, KING)); }
int piece_on(int sq) const { return board[sq]; }
u64 attackers_to(int sq, u64 occ) const;
bool square_attacked(int sq, int by, u64 occ) const;
u64 checkers() const { return attackers_to(king_square(stm), occupancy()) & colors[stm ^ 1]; }
bool in_check() const { return checkers() != 0; }
bool insufficient_material() const;
// Returns a null move when the string is not a legal move here.
Move parse_uci(std::string_view uci) const;
std::string pretty() const;
private:
void put_piece(int piece, int sq);
void remove_piece(int sq);
void move_piece(int from, int to);
};
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