Merge pull request #185 from rektdie/more-stages

more stages in movepicker | bench 1468577
This commit was merged in pull request #185.
This commit is contained in:
rektdie authored and GitHub committed 2026-09-19 21:22:25 +02:00
commit 1470ad34a8
5 files changed
+456 -340

No files matched your search

+203 -203
View File
@@ -1,203 +1,203 @@
#pragma once
#include "bitboard.h"
#include "move.h"
#include "board.h"
enum MovegenMode {
Quiet,
Noisy,
All
};
namespace MOVEGEN {
inline Bitboard pawnAttacks[2][64];
inline Bitboard knightAttacks[64];
inline Bitboard kingAttacks[64];
void initLeaperAttacks();
void initSliderAttacks();
Bitboard getBishopAttack(int square, U64 occupancy);
Bitboard getRookAttack(int square, U64 occupancy);
Bitboard getQueenAttack(int square, U64 occupancy);
Bitboard getPawnPushes(int square, bool color, Bitboard &occupancy);
Bitboard getPieceAttacks(int square, int piece, int color, U64 occupancy);
template <MovegenMode mode>
void GenPawnMoves(Board &board);
template <MovegenMode mode>
void GenKnightMoves(Board &board);
template <MovegenMode mode>
void GenRookMoves(Board &board);
template <MovegenMode mode>
void GenBishopMoves(Board &board);
template <MovegenMode mode>
void GenQueenMoves(Board &board);
template <MovegenMode mode>
void GenKingMoves(Board &board);
template <MovegenMode mode>
void GenerateMoves(Board &board, bool clear);
void GenThreatMaps(Board &board);
// bishop relevant occupancy bit count
constexpr int bishopRelevantBits[64] = {
6, 5, 5, 5, 5, 5, 5, 6,
5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 7, 7, 7, 7, 5, 5,
5, 5, 7, 9, 9, 7, 5, 5,
5, 5, 7, 9, 9, 7, 5, 5,
5, 5, 7, 7, 7, 7, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5,
6, 5, 5, 5, 5, 5, 5, 6
};
// rook relevant occupancy bit count
constexpr int rookRelevantBits[64] = {
12, 11, 11, 11, 11, 11, 11, 12,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
12, 11, 11, 11, 11, 11, 11, 12
};
constexpr U64 rookMagicNumbers[64] = {
0x8a80104000800020ULL,
0x140002000100040ULL,
0x2801880a0017001ULL,
0x100081001000420ULL,
0x200020010080420ULL,
0x3001c0002010008ULL,
0x8480008002000100ULL,
0x2080088004402900ULL,
0x800098204000ULL,
0x2024401000200040ULL,
0x100802000801000ULL,
0x120800800801000ULL,
0x208808088000400ULL,
0x2802200800400ULL,
0x2200800100020080ULL,
0x801000060821100ULL,
0x80044006422000ULL,
0x100808020004000ULL,
0x12108a0010204200ULL,
0x140848010000802ULL,
0x481828014002800ULL,
0x8094004002004100ULL,
0x4010040010010802ULL,
0x20008806104ULL,
0x100400080208000ULL,
0x2040002120081000ULL,
0x21200680100081ULL,
0x20100080080080ULL,
0x2000a00200410ULL,
0x20080800400ULL,
0x80088400100102ULL,
0x80004600042881ULL,
0x4040008040800020ULL,
0x440003000200801ULL,
0x4200011004500ULL,
0x188020010100100ULL,
0x14800401802800ULL,
0x2080040080800200ULL,
0x124080204001001ULL,
0x200046502000484ULL,
0x480400080088020ULL,
0x1000422010034000ULL,
0x30200100110040ULL,
0x100021010009ULL,
0x2002080100110004ULL,
0x202008004008002ULL,
0x20020004010100ULL,
0x2048440040820001ULL,
0x101002200408200ULL,
0x40802000401080ULL,
0x4008142004410100ULL,
0x2060820c0120200ULL,
0x1001004080100ULL,
0x20c020080040080ULL,
0x2935610830022400ULL,
0x44440041009200ULL,
0x280001040802101ULL,
0x2100190040002085ULL,
0x80c0084100102001ULL,
0x4024081001000421ULL,
0x20030a0244872ULL,
0x12001008414402ULL,
0x2006104900a0804ULL,
0x1004081002402ULL
};
constexpr U64 bishopMagicNumbers[64] = {
0x40040844404084ULL,
0x2004208a004208ULL,
0x10190041080202ULL,
0x108060845042010ULL,
0x581104180800210ULL,
0x2112080446200010ULL,
0x1080820820060210ULL,
0x3c0808410220200ULL,
0x4050404440404ULL,
0x21001420088ULL,
0x24d0080801082102ULL,
0x1020a0a020400ULL,
0x40308200402ULL,
0x4011002100800ULL,
0x401484104104005ULL,
0x801010402020200ULL,
0x400210c3880100ULL,
0x404022024108200ULL,
0x810018200204102ULL,
0x4002801a02003ULL,
0x85040820080400ULL,
0x810102c808880400ULL,
0xe900410884800ULL,
0x8002020480840102ULL,
0x220200865090201ULL,
0x2010100a02021202ULL,
0x152048408022401ULL,
0x20080002081110ULL,
0x4001001021004000ULL,
0x800040400a011002ULL,
0xe4004081011002ULL,
0x1c004001012080ULL,
0x8004200962a00220ULL,
0x8422100208500202ULL,
0x2000402200300c08ULL,
0x8646020080080080ULL,
0x80020a0200100808ULL,
0x2010004880111000ULL,
0x623000a080011400ULL,
0x42008c0340209202ULL,
0x209188240001000ULL,
0x400408a884001800ULL,
0x110400a6080400ULL,
0x1840060a44020800ULL,
0x90080104000041ULL,
0x201011000808101ULL,
0x1a2208080504f080ULL,
0x8012020600211212ULL,
0x500861011240000ULL,
0x180806108200800ULL,
0x4000020e01040044ULL,
0x300000261044000aULL,
0x802241102020002ULL,
0x20906061210001ULL,
0x5a84841004010310ULL,
0x4010801011c04ULL,
0xa010109502200ULL,
0x4a02012000ULL,
0x500201010098b028ULL,
0x8040002811040900ULL,
0x28000010020204ULL,
0x6000020202d0240ULL,
0x8918844842082200ULL,
0x4010011029020020ULL
};
#include "movegen.tpp"
}
#pragma once
#include "bitboard.h"
#include "move.h"
#include "board.h"
enum MovegenMode {
Quiet,
Noisy,
All
};
namespace MOVEGEN {
inline Bitboard pawnAttacks[2][64];
inline Bitboard knightAttacks[64];
inline Bitboard kingAttacks[64];
void initLeaperAttacks();
void initSliderAttacks();
Bitboard getBishopAttack(int square, U64 occupancy);
Bitboard getRookAttack(int square, U64 occupancy);
Bitboard getQueenAttack(int square, U64 occupancy);
Bitboard getPawnPushes(int square, bool color, Bitboard &occupancy);
Bitboard getPieceAttacks(int square, int piece, int color, U64 occupancy);
template <MovegenMode mode>
void GenPawnMoves(Board &board);
template <MovegenMode mode>
void GenKnightMoves(Board &board);
template <MovegenMode mode>
void GenRookMoves(Board &board);
template <MovegenMode mode>
void GenBishopMoves(Board &board);
template <MovegenMode mode>
void GenQueenMoves(Board &board);
template <MovegenMode mode>
void GenKingMoves(Board &board);
template <MovegenMode mode>
void GenerateMoves(Board &board, bool clear, bool recalcThreats = true);
void GenThreatMaps(Board &board);
// bishop relevant occupancy bit count
constexpr int bishopRelevantBits[64] = {
6, 5, 5, 5, 5, 5, 5, 6,
5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 7, 7, 7, 7, 5, 5,
5, 5, 7, 9, 9, 7, 5, 5,
5, 5, 7, 9, 9, 7, 5, 5,
5, 5, 7, 7, 7, 7, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5,
6, 5, 5, 5, 5, 5, 5, 6
};
// rook relevant occupancy bit count
constexpr int rookRelevantBits[64] = {
12, 11, 11, 11, 11, 11, 11, 12,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
12, 11, 11, 11, 11, 11, 11, 12
};
constexpr U64 rookMagicNumbers[64] = {
0x8a80104000800020ULL,
0x140002000100040ULL,
0x2801880a0017001ULL,
0x100081001000420ULL,
0x200020010080420ULL,
0x3001c0002010008ULL,
0x8480008002000100ULL,
0x2080088004402900ULL,
0x800098204000ULL,
0x2024401000200040ULL,
0x100802000801000ULL,
0x120800800801000ULL,
0x208808088000400ULL,
0x2802200800400ULL,
0x2200800100020080ULL,
0x801000060821100ULL,
0x80044006422000ULL,
0x100808020004000ULL,
0x12108a0010204200ULL,
0x140848010000802ULL,
0x481828014002800ULL,
0x8094004002004100ULL,
0x4010040010010802ULL,
0x20008806104ULL,
0x100400080208000ULL,
0x2040002120081000ULL,
0x21200680100081ULL,
0x20100080080080ULL,
0x2000a00200410ULL,
0x20080800400ULL,
0x80088400100102ULL,
0x80004600042881ULL,
0x4040008040800020ULL,
0x440003000200801ULL,
0x4200011004500ULL,
0x188020010100100ULL,
0x14800401802800ULL,
0x2080040080800200ULL,
0x124080204001001ULL,
0x200046502000484ULL,
0x480400080088020ULL,
0x1000422010034000ULL,
0x30200100110040ULL,
0x100021010009ULL,
0x2002080100110004ULL,
0x202008004008002ULL,
0x20020004010100ULL,
0x2048440040820001ULL,
0x101002200408200ULL,
0x40802000401080ULL,
0x4008142004410100ULL,
0x2060820c0120200ULL,
0x1001004080100ULL,
0x20c020080040080ULL,
0x2935610830022400ULL,
0x44440041009200ULL,
0x280001040802101ULL,
0x2100190040002085ULL,
0x80c0084100102001ULL,
0x4024081001000421ULL,
0x20030a0244872ULL,
0x12001008414402ULL,
0x2006104900a0804ULL,
0x1004081002402ULL
};
constexpr U64 bishopMagicNumbers[64] = {
0x40040844404084ULL,
0x2004208a004208ULL,
0x10190041080202ULL,
0x108060845042010ULL,
0x581104180800210ULL,
0x2112080446200010ULL,
0x1080820820060210ULL,
0x3c0808410220200ULL,
0x4050404440404ULL,
0x21001420088ULL,
0x24d0080801082102ULL,
0x1020a0a020400ULL,
0x40308200402ULL,
0x4011002100800ULL,
0x401484104104005ULL,
0x801010402020200ULL,
0x400210c3880100ULL,
0x404022024108200ULL,
0x810018200204102ULL,
0x4002801a02003ULL,
0x85040820080400ULL,
0x810102c808880400ULL,
0xe900410884800ULL,
0x8002020480840102ULL,
0x220200865090201ULL,
0x2010100a02021202ULL,
0x152048408022401ULL,
0x20080002081110ULL,
0x4001001021004000ULL,
0x800040400a011002ULL,
0xe4004081011002ULL,
0x1c004001012080ULL,
0x8004200962a00220ULL,
0x8422100208500202ULL,
0x2000402200300c08ULL,
0x8646020080080080ULL,
0x80020a0200100808ULL,
0x2010004880111000ULL,
0x623000a080011400ULL,
0x42008c0340209202ULL,
0x209188240001000ULL,
0x400408a884001800ULL,
0x110400a6080400ULL,
0x1840060a44020800ULL,
0x90080104000041ULL,
0x201011000808101ULL,
0x1a2208080504f080ULL,
0x8012020600211212ULL,
0x500861011240000ULL,
0x180806108200800ULL,
0x4000020e01040044ULL,
0x300000261044000aULL,
0x802241102020002ULL,
0x20906061210001ULL,
0x5a84841004010310ULL,
0x4010801011c04ULL,
0xa010109502200ULL,
0x4a02012000ULL,
0x500201010098b028ULL,
0x8040002811040900ULL,
0x28000010020204ULL,
0x6000020202d0240ULL,
0x8918844842082200ULL,
0x4010011029020020ULL
};
#include "movegen.tpp"
}
+4 -4
View File
@@ -270,14 +270,14 @@ void GenKingMoves(Board &board) {
}
template <MovegenMode mode>
void GenerateMoves(Board &board, bool clear) {
void GenerateMoves(Board &board, bool clear, bool recalcThreats) {
if (clear) {
board.ResetMoves();
board.pieceThreats = {};
board.colorThreats = {};
}
GenThreatMaps(board);
if (recalcThreats) {
GenThreatMaps(board);
}
GenPawnMoves<mode>(board);
GenKnightMoves<mode>(board);
+209
View File
@@ -0,0 +1,209 @@
#include "movepicker.h"
#include "search.h"
#include "tunables.h"
#include <utility>
namespace SEARCH {
template <MovegenMode mode>
MovePicker<mode>::MovePicker(Board &b, SearchContext *c, int p, Move tt)
: board(b), ctx(c), ply(p), ttMove(tt), killerMove(), stage(PickStage::TTMove) {
if (!ttMove || !board.IsPseudoLegal(ttMove)) {
ttMove = Move();
}
if constexpr (mode == Noisy) {
if (ttMove && !(ttMove.IsCapture() || ttMove.IsPromo())) {
ttMove = Move();
}
} else {
Move killer = ctx->killerMoves[ply];
if (killer && killer != ttMove && !killer.IsCapture() && !killer.IsPromo()
&& board.IsPseudoLegal(killer)) {
killerMove = killer;
}
}
}
template <MovegenMode mode>
int MovePicker<mode>::ScoreNoisyMove(Move move) {
int score = MoveEstimatedValue(board, move) * noisyScoreScale;
if (move.IsCapture()) {
int attackerType = board.GetPieceType(move.MoveFrom());
int targetType = (move.GetFlags() == epCapture) ? Pawn : board.GetPieceType(move.MoveTo());
score += ctx->capthist[board.sideToMove][attackerType][targetType][move.MoveTo()];
}
return score;
}
template <MovegenMode mode>
int MovePicker<mode>::ScoreQuietMove(Move move) {
bool sourceThreatened = board.IsSquareThreatened(board.sideToMove, move.MoveFrom());
bool targetThreatened = board.IsSquareThreatened(board.sideToMove, move.MoveTo());
int score = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()][sourceThreatened][targetThreatened];
if (ply > 0) score += ctx->conthist.GetNPly(board, move, ctx, ply, 1);
if (ply > 1) score += ctx->conthist.GetNPly(board, move, ctx, ply, 2);
return score;
}
template <MovegenMode mode>
void MovePicker<mode>::ScoreNoisyRange() {
noisyStart = 0;
MOVEGEN::GenerateMoves<Noisy>(board, true, false);
int rawEnd = board.currentMoveIndex;
int write = noisyStart;
for (int read = noisyStart; read < rawEnd; read++) {
Move m = board.moveList[read];
if (m == ttMove || m == killerMove) continue;
board.moveList[write] = m;
scores[write] = ScoreNoisyMove(m);
write++;
}
noisyEnd = write;
board.currentMoveIndex = noisyEnd;
int goodWrite = noisyStart;
for (int i = noisyStart; i < noisyEnd; i++) {
Move m = board.moveList[i];
if (SEE(board, m, seeOrderingThreshold)) {
if (i != goodWrite) {
std::swap(board.moveList[i], board.moveList[goodWrite]);
std::swap(scores[i], scores[goodWrite]);
}
goodWrite++;
}
}
goodNoisyEnd = goodWrite;
noisyCursor = noisyStart;
badNoisyCursor = goodNoisyEnd;
}
template <MovegenMode mode>
void MovePicker<mode>::ScoreQuietRange() {
quietStart = noisyEnd;
MOVEGEN::GenerateMoves<Quiet>(board, false, false);
int rawEnd = board.currentMoveIndex;
int write = quietStart;
for (int read = quietStart; read < rawEnd; read++) {
Move m = board.moveList[read];
if (m == ttMove || m == killerMove) continue;
board.moveList[write] = m;
scores[write] = ScoreQuietMove(m);
write++;
}
quietEnd = write;
board.currentMoveIndex = quietEnd;
quietCursor = quietStart;
}
template <MovegenMode mode>
Move MovePicker<mode>::PopBest(int &cursor, int end) {
if (cursor >= end) return Move();
int bestIdx = cursor;
for (int i = cursor + 1; i < end; i++) {
if (scores[i] > scores[bestIdx]) bestIdx = i;
}
if (bestIdx != cursor) {
std::swap(board.moveList[bestIdx], board.moveList[cursor]);
std::swap(scores[bestIdx], scores[cursor]);
}
Move best = board.moveList[cursor];
cursor++;
return best;
}
template <MovegenMode mode>
Move MovePicker<mode>::Next() {
while (true) {
switch (stage) {
case PickStage::TTMove: {
stage = PickStage::GenNoisy;
if (ttMove) return ttMove;
continue;
}
case PickStage::GenNoisy: {
ScoreNoisyRange();
stage = PickStage::GoodNoisy;
continue;
}
case PickStage::GoodNoisy: {
Move m = PopBest(noisyCursor, goodNoisyEnd);
if (m) return m;
if constexpr (mode == Noisy) {
stage = PickStage::BadNoisy;
} else {
stage = PickStage::Killer;
}
continue;
}
case PickStage::Killer: {
stage = PickStage::GenQuiet;
if (killerMove) return killerMove;
continue;
}
case PickStage::GenQuiet: {
ScoreQuietRange();
stage = PickStage::Quiet;
continue;
}
case PickStage::Quiet: {
Move m = PopBest(quietCursor, quietEnd);
if (m) return m;
stage = PickStage::BadNoisy;
continue;
}
case PickStage::BadNoisy: {
Move m = PopBest(badNoisyCursor, noisyEnd);
if (m) return m;
stage = PickStage::Finished;
return Move();
}
case PickStage::Finished:
return Move();
default:
return Move();
}
}
}
template class MovePicker<Noisy>;
template class MovePicker<All>;
}
+39 -133
View File
@@ -1,155 +1,61 @@
#pragma once
#include "search.h"
#include <array>
#include "board.h"
#include "movegen.h"
#include <limits>
namespace SEARCH {
using namespace SEARCH;
class SearchContext;
enum class PickStage {
TTMove,
GenNoisy,
GoodNoisy,
Killer,
GenQuiet,
Quiet,
BadNoisy,
Finished
};
template <MovegenMode mode>
class MovePicker {
private:
enum Stage {
TT,
Rest,
End
};
Board& board;
SearchContext* ctx;
Move ttMove;
int ply;
Stage stage;
int index;
bool generated;
int scores[MAX_MOVES];
public:
MovePicker(Board& b, SearchContext* c, int p, Move tt)
: board(b), ctx(c), ttMove(tt), ply(p), stage(TT), index(0), generated(false)
{
}
MovePicker(Board &board, SearchContext *ctx, int ply, Move ttMove);
Move Next() {
while (true) {
switch (stage) {
case TT: {
stage = Rest;
if (ttMove && board.IsPseudoLegal(ttMove))
return ttMove;
break;
}
case Rest: {
if (!generated) {
generated = true;
MOVEGEN::GenerateMoves<mode>(board, true);
ScoreAllMoves();
index = 0;
}
while (index < board.currentMoveIndex) {
int idx = FindNext();
Move m = board.moveList[idx];
if (m == ttMove)
continue;
return m;
}
stage = End;
break;
}
case End:
return Move();
}
}
}
Move Next();
private:
Board &board;
SearchContext *ctx;
int ply;
int ScoreMove(Move& move) {
Move ttMove;
Move killerMove;
if (move.IsCapture()) {
PickStage stage;
int attackerType = board.GetPieceType(move.MoveFrom());
int targetType = board.GetPieceType(move.MoveTo());
int noisyStart = 0;
int noisyEnd = 0;
int goodNoisyEnd = 0;
if (move.GetFlags() == epCapture)
targetType = Pawn;
int quietStart = 0;
int quietEnd = 0;
int capthistScore =
ctx->capthist[board.sideToMove][attackerType][targetType][move.MoveTo()];
int noisyCursor = 0;
int badNoisyCursor = 0;
int quietCursor = 0;
return 50000 * SEE(board, move, seeOrderingThreshold)
+ (100 * targetType - attackerType + 105)
+ capthistScore;
}
std::array<int, MAX_MOVES> scores{};
if (ctx->killerMoves[ply] == move)
return 41000;
void ScoreNoisyRange();
bool sourceThreatened = board.IsSquareThreatened(board.sideToMove, move.MoveFrom());
bool targetThreatened = board.IsSquareThreatened(board.sideToMove, move.MoveTo());
void ScoreQuietRange();
int historyScore =
ctx->history[board.sideToMove]
[move.MoveFrom()]
[move.MoveTo()]
[sourceThreatened]
[targetThreatened];
int ScoreNoisyMove(Move move);
int ScoreQuietMove(Move move);
int conthistScore = 0;
Move PopBest(int &cursor, int end);
};
if (ply > 0) {
conthistScore = ctx->conthist.GetNPly(board, move, ctx, ply, 1);
if (ply > 1)
conthistScore += ctx->conthist.GetNPly(board, move, ctx, ply, 2);
}
return 20000 + historyScore + conthistScore;
}
void ScoreAllMoves() {
int moveCount = board.currentMoveIndex;
for (int i = 0; i < moveCount; i++)
scores[i] = ScoreMove(board.moveList[i]);
}
int FindNext() {
const auto toU64 = [](int s) {
int64_t widened = s;
widened -= std::numeric_limits<int32_t>::min();
return static_cast<uint64_t>(widened) << 32;
};
int moveCount = board.currentMoveIndex;
uint64_t best = toU64(scores[index]) | static_cast<uint64_t>(256 - index);
for (int i = index + 1; i < moveCount; i++) {
uint64_t curr = toU64(scores[i]) | static_cast<uint64_t>(256 - i);
if (curr > best)
best = curr;
}
int bestIdx = 256 - static_cast<int>(best & 0xFFFFFFFF);
if (bestIdx != index) {
std::swap(board.moveList[index], board.moveList[bestIdx]);
std::swap(scores[index], scores[bestIdx]);
}
return index++;
}
};
}
+1
View File
@@ -41,6 +41,7 @@
X_INT(seeQsThreshold, -13, -120, 120) \
X_INT(seeOrderingThreshold, -102, -200, 0) \
X_INT(ldseMargin, 26, 0, 96) \
X_INT(noisyScoreScale, 32, 0, 96) \
X_INT(historyBonusMultiplier, 306, 280, 320) \
X_INT(historyBonusSub, 252, 235, 265) \
X_INT(historyMalusMultiplier, 301, 280, 320) \