Skip to content

Use threats in quiet move generation and simplify castling check in pos.legal()#6288

Open
ces42 wants to merge 8 commits into
official-stockfish:masterfrom
ces42:qks-rebase
Open

Use threats in quiet move generation and simplify castling check in pos.legal()#6288
ces42 wants to merge 8 commits into
official-stockfish:masterfrom
ces42:qks-rebase

Conversation

@ces42

@ces42 ces42 commented Sep 2, 2025

Copy link
Copy Markdown
Contributor

Recent changes to movepicker mean that we're computing (almost all) threatened squares for quiet move ranking. This patch uses the bitboard of all threatened square in movegen to avoid generating any illegal king move in the quiet stage. This allows removing the code in pos.legal(Move) that checks whether the king moves through a threatened square when castling.
I was hoping this would lead to a speedup since we no longer need to rank and sort the illegal king moves, but that doesn't seem to be the case.

Tests were done agains an older version of master, but rebasing is straightforward.

passed STC: https://tests.stockfishchess.org/tests/view/68ade27c6217b8721dca9765
LLR: 2.97 (-2.94,2.94) <-1.75,0.25>
Total: 198720 W: 51786 L: 51741 D: 95193
Ptnml(0-2): 712, 23321, 51261, 23342, 724

passed LTC: https://tests.stockfishchess.org/tests/view/68b1c4c26217b8721dca9c56
LLR: 2.95 (-2.94,2.94) <-1.75,0.25>
Total: 277068 W: 70944 L: 70984 D: 135140
Ptnml(0-2): 154, 30225, 77815, 30187, 153

@github-actions

github-actions Bot commented Sep 2, 2025

Copy link
Copy Markdown

clang-format 20 needs to be run on this PR.
If you do not have clang-format installed, the maintainer will run it when merging.
For the exact version please see https://packages.ubuntu.com/plucky/clang-format-20.

(execution 17411532491 / attempt 1)

@Disservin

Copy link
Copy Markdown
Member

mh i don't really see this as an improvement.. curious what others think

@pb00067

pb00067 commented Jan 5, 2026

Copy link
Copy Markdown

I would like to see this merged for the following reasons:

Generating king moves that are scored and sorted in the move picker, only to be discarded later in the search by pos.legal(), has always seemed a bit odd to me.
This patch allows us to have an almost exact estimation of legal moves for a given position.
This could help in several ways:

Improve move-count pruning
Currently, move-count pruning does not take the number of legal moves into account.
Pruning the last legal move might be suboptimal. For example, if we have already analyzed 19 out of 20 legal moves, it might be better to analyze the last ranked move instead of pruning it.

Improve null-move pruning / verification search
Null-move pruning is prone to zugzwang, which is especially likely when there are no legal king moves available.
Currently in master it is not cheap to establish if there are no legal king moves available.

Improve evaluation correction
From what I know, the current NNUE evaluation does not consider empty squares at all. Threatened squares around the king might influence the correct evaluation.

Having this committed would give us the opportunity to experiment without too much effort.
If it turns out to be useless, we can still revert it after a few months of testing. ;-)

@vondele

vondele commented May 6, 2026

Copy link
Copy Markdown
Member

This is pretty outdated by now, and I wasn't too excited about this. However @ces42 you feel this is still relevant?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants