13 Commits
Author SHA1 Message Date
rektdie 3b2cb8839b minor corrhist | bench 1406680 2026-09-28 21:19:42 +02:00
rektdie ecd57607f3 Merge pull request #187 from rektdie/corrhist-fh
corrhist update on fail high | bench 1202015
2026-09-28 20:51:59 +02:00
rektdie be9982eb75 corrhist update on fail high | bench 1202015 2026-09-28 19:13:14 +02:00
rektdie babe3d0db0 Merge pull request #186 from rektdie/50mr-scaling
50 mr scaling | bench 1331806
2026-09-26 09:52:42 +02:00
rektdie 30e54c2b71 50 mr scaling | bench 1331806 2026-09-19 21:31:08 +02:00
rektdie 1470ad34a8 Merge pull request #185 from rektdie/more-stages
more stages in movepicker | bench 1468577
2026-09-19 21:22:25 +02:00
rektdie f23a59730b more stages in movepicker | bench 1468577 | bench 1468577 2026-09-19 15:59:05 +02:00
rektdie dbd56e0c21 Update README.md 2026-05-13 22:46:52 +02:00
rektdie 390ee23e75 updated version number | bench 1246472 2026-04-22 01:23:31 +02:00
rektdie 4ebdeb54f6 Merge pull request #183 from rektdie/windows-build-fix
windows builds with clang++ too | bench 1246472
2026-04-22 01:08:05 +02:00
rektdie 4e437d52fc windows builds with clang++ too | bench 1246472 2026-04-22 00:52:00 +02:00
rektdie a3255581fb Merge pull request #182 from rektdie/smp-crash-fix
smp crash fix | bench 1246472
2026-04-21 15:08:27 +02:00
rektdie 24a36414f8 smp crash fix | bench 1246472 2026-04-21 00:26:57 +02:00
15 changed files with 614 additions and 392 deletions

No files matched your search

+10 -4
View File
@@ -5,14 +5,16 @@ OBJ_DIR := obj
EXE ?= Eleanor
EVALFILE := ./nnue.bin
CXX := clang++
ifeq ($(OS),Windows_NT)
UNAME_S := Windows
ARCH := x86_64
EXE_EXT := .exe
MKDIR := mkdir
RM := rmdir /s /q
DEL := del /q
PLATFORM := windows
CXX := g++
else
UNAME_S := $(shell uname -s)
ARCH := $(shell uname -m)
@@ -20,7 +22,6 @@ else
MKDIR := mkdir -p
RM := rm -rf
DEL := rm -f
CXX := clang++
ifeq ($(UNAME_S),Darwin)
PLATFORM := mac
else
@@ -28,6 +29,8 @@ else
endif
endif
CXX_BASE := $(notdir $(firstword $(CXX)))
CXX_DRIVER_FLAGS :=
CXXFLAGS := -std=c++20 -O3 -flto -Wall
LDFLAGS := -O3 -flto
@@ -39,6 +42,9 @@ ifeq ($(PLATFORM),mac)
endif
else ifeq ($(PLATFORM),windows)
ARCH_FLAGS := -march=native
CXX_DRIVER_FLAGS += --target=$(ARCH)-w64-windows-gnu
CXXFLAGS += -pthread
LDFLAGS += -pthread
LDFLAGS += -static -Wl,--stack,8388608
else
ifeq ($(ARCH),aarch64)
@@ -58,10 +64,10 @@ SRCS := $(wildcard $(SRC_DIR)/*.cpp)
OBJS := $(patsubst $(SRC_DIR)/%.cpp, $(OBJ_DIR)/%.o, $(SRCS))
$(EXE)$(EXE_EXT): $(OBJS)
$(CXX) $(CXXFLAGS) -DEVALFILE=\"$(EVALFILE)\" $^ -o $@ $(LDFLAGS)
$(CXX) $(CXX_DRIVER_FLAGS) $(CXXFLAGS) -DEVALFILE=\"$(EVALFILE)\" $^ -o $@ $(LDFLAGS)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | $(OBJ_DIR)
$(CXX) $(CXXFLAGS) -c -o $@ $<
$(CXX) $(CXX_DRIVER_FLAGS) $(CXXFLAGS) -c -o $@ $<
$(OBJ_DIR):
$(MKDIR) $(OBJ_DIR)
+8 -7
View File
@@ -12,12 +12,12 @@
![GitHub Downloads (all assets, all releases)](https://img.shields.io/github/downloads/rektdie/Eleanor/total?color=orange)
| Version | Release Date | [CCRL 40/15](https://www.computerchess.org.uk/ccrl/4040/cgi/compare_engines.cgi?family=Eleanor&print=Rating+list&print=Results+table&print=LOS+table&print=Ponder+hit+table&print=Eval+difference+table&print=Comopp+gamenum+table&print=Overlap+table&print=Score+with+common+opponents) | [CCRL Blitz](https://www.computerchess.org.uk/ccrl/404/cgi/compare_engines.cgi?family=Eleanor&print=Rating+list&print=Results+table&print=LOS+table&print=Ponder+hit+table&print=Eval+difference+table&print=Comopp+gamenum+table&print=Overlap+table&print=Score+with+common+opponents) |
| --- | --- | --- | --- |
| 4.0 | 2026-04-18 | - | -
| 3.0 | 2025-12-05 | 3405 (#84) | 3495 (#79-80)
| 2.0 | 2025-08-23 | 3312 (#115) | 3392 (#105)
| 1.0 | 2025-06-02 | 2856 (#244) | 2759 (#256)
| Version | Release Date | [CCRL 40/15](https://www.computerchess.org.uk/ccrl/4040/cgi/compare_engines.cgi?family=Eleanor&print=Rating+list&print=Results+table&print=LOS+table&print=Ponder+hit+table&print=Eval+difference+table&print=Comopp+gamenum+table&print=Overlap+table&print=Score+with+common+opponents) | [CCRL Blitz](https://www.computerchess.org.uk/ccrl/404/cgi/compare_engines.cgi?family=Eleanor&print=Rating+list&print=Results+table&print=LOS+table&print=Ponder+hit+table&print=Eval+difference+table&print=Comopp+gamenum+table&print=Overlap+table&print=Score+with+common+opponents) | [CEGT 40/20](http://www.cegt.net/40_40%20Rating%20List/40_40%20SingleVersion/rangliste.html)
| --- | --- | --- | --- | --- |
| 4.1 | 2026-04-22 | 3528 (#60-61) | 3590 (#64) | 3443 (#59)
| 3.0 | 2025-12-05 | 3405 (#84) | 3495 (#79-80) | -
| 2.0 | 2025-08-23 | 3312 (#115) | 3392 (#105) | -
| 1.0 | 2025-06-02 | 2856 (#244) | 2759 (#256) | -
</div>
@@ -32,7 +32,8 @@ A 1536-hidden-layer *perspective net* with 8 input buckets, 8 output buckets and
## Installation
> **Prerequisites:**
> - `clang++` on linux, `g++` on windows
> - `clang++` compiler
> - On Windows, `clang++` must have access to a MinGW GNU toolchain/runtime because the build targets `x86_64-w64-windows-gnu`
> - Access to `make` command
1. Clone the repository
+6
View File
@@ -45,6 +45,7 @@ void Board::Reset() {
pawnKey = 0ULL;
nonPawnKey = 0ULL;
majorKey = 0ULL;
minorKey = 0ULL;
checkZones = std::array<Bitboard, 4>();
}
@@ -97,6 +98,7 @@ void Board::SetByFen(std::string_view fen) {
pawnKey = 0ULL;
nonPawnKey = 0ULL;
majorKey = 0ULL;
minorKey = 0ULL;
occupied = colors[White] | colors[Black];
hashKey = UTILS::GetHashKey(*this);
@@ -310,6 +312,8 @@ void Board::SetPiece(int piece, int square, bool color) {
if (piece == King || piece == Rook || piece == Queen) {
majorKey ^= UTILS::zKeys[color][piece][square];
} else if (piece == Knight || piece == Bishop) {
minorKey ^= UTILS::zKeys[color][piece][square];
}
}
}
@@ -330,6 +334,8 @@ void Board::RemovePiece(int piece, int square, bool color) {
if (piece == King || piece == Rook || piece == Queen) {
majorKey ^= UTILS::zKeys[color][piece][square];
} else if (piece == Knight || piece == Bishop) {
minorKey ^= UTILS::zKeys[color][piece][square];
}
}
}
+2 -1
View File
@@ -48,6 +48,7 @@ public:
U64 pawnKey = 0ULL;
U64 nonPawnKey = 0ULL;
U64 majorKey = 0ULL;
U64 minorKey = 0ULL;
std::array<Bitboard, 4> checkZones;
@@ -90,4 +91,4 @@ public:
bool IsLegal(Move &move);
bool IsPseudoLegal(Move &move);
};
};
+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++;
}
};
}
+30 -16
View File
@@ -78,7 +78,12 @@ static int AdjustEval(Board &board, SearchContext* ctx, int eval) {
int mateFound = MATE_SCORE - MAX_DEPTH;
return std::clamp(eval + corrhist / CORRHIST_GRAIN, -mateFound + 1, mateFound - 1);
int adjusted = eval + corrhist / CORRHIST_GRAIN;
const int scaleNumerator = std::max(0, fiftyMoveScaleBase - board.halfMoves);
adjusted = adjusted * scaleNumerator / fiftyMoveScaleBase;
return std::clamp(adjusted, -mateFound + 1, mateFound - 1);
}
static bool IsTwoFold(Board &board, SearchContext* ctx) {
@@ -236,20 +241,20 @@ bool SEE(Board& board, Move& move, int threshold) {
template <searchMode mode>
static bool ShouldStop(SearchContext* ctx) {
if constexpr (mode == normal) {
if (ctx->nodes % 1024 == 0) {
if (ctx->sw.GetElapsedMS() >= ctx->timeToSearch) {
searchStopped = true;
if (ctx->nodes % 1024 == 0) {
if (ctx->sw.GetElapsedMS() >= ctx->timeToSearch) {
searchStopped.store(true, std::memory_order_relaxed);
return true;
}
}
} else if constexpr (mode == nodesMode) {
if (ctx->nodes > ctx->nodesToGo) {
searchStopped = true;
searchStopped.store(true, std::memory_order_relaxed);
return true;
}
} else if constexpr (mode == datagen) {
if (ctx->nodes > DATAGEN::HARD_NODES) {
searchStopped = true;
searchStopped.store(true, std::memory_order_relaxed);
return true;
}
}
@@ -378,7 +383,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
}
results.score = bestScore;
if (searchStopped) return 0;
if (searchStopped.load(std::memory_order_relaxed)) return 0;
ctx->TT->WriteEntry(board.hashKey, 0, results.score, nodeType, results.bestMove, ttpv);
return results;
}
@@ -474,7 +479,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
int score = -PVS<false, mode>(copy, depth - reduction, -beta, -beta + 1, ply + 1, ctx, !cutnode).score;
if (searchStopped) return 0;
if (searchStopped.load(std::memory_order_relaxed)) return 0;
if (score >= beta) {
if (depth <= 14 || ctx->minNmpPly > 0) {
return score > MATE_SCORE - MAX_DEPTH ? beta : score;
@@ -533,7 +538,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
ply + 1, ctx, !cutnode).score;
}
if (searchStopped) return 0;
if (searchStopped.load(std::memory_order_relaxed)) return 0;
if (score >= probcutBeta) {
ctx->TT->WriteEntry(board.hashKey, probcutDepth, score, CutNode, currMove, ttpv);
@@ -701,7 +706,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
ctx->nodesTable[currMove % 4096] += ctx->nodes - nodesBeforeSearch;
}
if (searchStopped) return 0;
if (searchStopped.load(std::memory_order_relaxed)) return 0;
if (currMove != 0 && currMove.IsQuiet()) {
seenQuiets[seenQuietsCount] = currMove;
@@ -775,6 +780,12 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
UpdateCaptHist(seenCaptures[i], -captHistoryMalus);
}
// Corrhist update on fail high
if (!ctx->excluded && !inCheck && currMove.IsQuiet() && score > staticEval) {
int corrHistBonus = std::clamp(score - staticEval, -CORRHIST_LIMIT, CORRHIST_LIMIT);
ctx->corrhist.UpdateAll(board, depth, corrHistBonus);
}
if (!ctx->excluded)
ctx->TT->WriteEntry(board.hashKey, depth, score, CutNode, currMove, ttpv);
return score;
@@ -789,7 +800,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
}
}
if (searchStopped) return 0;
if (searchStopped.load(std::memory_order_relaxed)) return 0;
if (!ctx->excluded) {
if (!inCheck && ((results.bestMove.IsQuiet() || !results.bestMove))
@@ -888,7 +899,7 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) {
aw.Set(currentResults.score);
if (searchStopped) {
if (searchStopped.load(std::memory_order_relaxed)) {
break;
} else {
if (currentResults.bestMove) {
@@ -901,13 +912,13 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) {
if constexpr (mode == normal) {
if (ctx->sw.GetElapsedMS() >= softTime) {
searchStopped = true;
searchStopped.store(true, std::memory_order_relaxed);
break;
}
}
} else if constexpr (mode == datagen) {
if (ctx->nodes >= DATAGEN::SOFT_NODES) {
searchStopped = true;
searchStopped.store(true, std::memory_order_relaxed);
break;
}
}
@@ -919,8 +930,11 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) {
template <searchMode mode>
SearchResults SearchPosition(Board &board, SearchParams params, SearchContext* ctx) {
searchStopped = false;
ctx->TT->IncreaseAge();
if constexpr (mode == bench || mode == datagen) {
searchStopped.store(false, std::memory_order_relaxed);
ctx->TT->IncreaseAge();
}
ctx->seldepth = 0;
ctx->nodesTable = {};
if constexpr (mode != bench) {
+5 -1
View File
@@ -82,6 +82,7 @@ private:
MultiArray<int, 2, CORRHIST_SIZE> pawnHist;
MultiArray<int, 2, CORRHIST_SIZE> nonPawnHist;
MultiArray<int, 2, CORRHIST_SIZE> majorHist;
MultiArray<int, 2, CORRHIST_SIZE> minorHist;
public:
void Update(Board& board, int depth, int diff, int* entry) {
const int scaledDiff = diff * CORRHIST_GRAIN;
@@ -95,18 +96,21 @@ public:
Update(board, depth, diff, &pawnHist[board.sideToMove][board.pawnKey % CORRHIST_SIZE]);
Update(board, depth, diff, &nonPawnHist[board.sideToMove][board.nonPawnKey % CORRHIST_SIZE]);
Update(board, depth, diff, &majorHist[board.sideToMove][board.majorKey % CORRHIST_SIZE]);
Update(board, depth, diff, &minorHist[board.sideToMove][board.minorKey % CORRHIST_SIZE]);
}
int GetAllHist(Board& board) {
return pawnHist[board.sideToMove][board.pawnKey % CORRHIST_SIZE]
+ nonPawnHist[board.sideToMove][board.nonPawnKey % CORRHIST_SIZE]
+ majorHist[board.sideToMove][board.majorKey % CORRHIST_SIZE];
+ majorHist[board.sideToMove][board.majorKey % CORRHIST_SIZE]
+ minorHist[board.sideToMove][board.minorKey % CORRHIST_SIZE];
}
void Clear() {
std::fill(&pawnHist[0][0], &pawnHist[0][0] + sizeof(pawnHist) / sizeof(int), 0);
std::fill(&nonPawnHist[0][0], &nonPawnHist[0][0] + sizeof(nonPawnHist) / sizeof(int), 0);
std::fill(&majorHist[0][0], &majorHist[0][0] + sizeof(majorHist) / sizeof(int), 0);
std::fill(&minorHist[0][0], &minorHist[0][0] + sizeof(minorHist) / sizeof(int), 0);
}
};
+1 -1
View File
@@ -47,4 +47,4 @@ void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move b
current->age = age;
current->ttpv = ttpv;
}
}
+3 -5
View File
@@ -67,13 +67,11 @@ public:
TTable() : table(defaultHash) {};
void Resize(U64 size) {
table.resize(size);
table.assign(size, {});
}
void Clear() {
U64 size = table.size();
table.clear();
table.resize(size);
std::fill(table.begin(), table.end(), TTBucket());
}
void PrefetchEntry(U64 &hashKey) {
@@ -109,4 +107,4 @@ public:
void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove, bool ttpv);
};
extern TTable SharedTT;
extern TTable SharedTT;
+3 -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) \
@@ -52,7 +53,8 @@
X_INT(captHistoryBonusMultiplier, 302, 280, 320) \
X_INT(captHistoryBonusSub, 249, 235, 265) \
X_INT(captHistoryMalusMultiplier, 300, 280, 320) \
X_INT(captHistoryMalusSub, 249, 235, 265)
X_INT(captHistoryMalusSub, 249, 235, 265) \
X_INT(fiftyMoveScaleBase, 200, 100, 400)
#ifdef TUNING
#define X_DOUBLE(name, default_val, min_val, max_val) \
+89 -15
View File
@@ -4,6 +4,7 @@
#include <string>
#include <tuple>
#include <memory>
#include <algorithm>
#include "types.h"
#include "movegen.h"
#include "search.h"
@@ -20,9 +21,55 @@
#include <pthread.h>
#endif
namespace {
constexpr size_t DefaultPositionHistorySize = 1000;
static void EnsurePositionHistory(SEARCH::SearchContext* ctx, int positionIndex) {
if (positionIndex >= static_cast<int>(ctx->positionHistory.size())) {
ctx->positionHistory.resize(positionIndex + 100);
}
}
static void ResetPositionHistory(SEARCH::SearchContext* ctx, const Board& board) {
const size_t historySize = std::max(DefaultPositionHistorySize, static_cast<size_t>(board.positionIndex + 100));
ctx->positionHistory.assign(historySize, 0);
ctx->positionHistory[board.positionIndex] = board.hashKey;
}
#ifdef _WIN32
static std::vector<std::thread> searchThreads;
#else
static std::vector<pthread_t> searchThreads;
#endif
static void JoinSearchThreads() {
#ifdef _WIN32
for (auto& thread : searchThreads) {
if (thread.joinable()) {
thread.join();
}
}
#else
for (pthread_t& thread : searchThreads) {
pthread_join(thread, nullptr);
}
#endif
searchThreads.clear();
}
static void StopSearchThreads() {
searchStopped.store(true, std::memory_order_relaxed);
JoinSearchThreads();
}
} // namespace
static void ParsePosition(Board &board, std::string_view command, SEARCH::SearchContext* ctx) {
if (command.find("startpos") != std::string::npos) {
board.SetByFen(StartingFen);
ResetPositionHistory(ctx, board);
}
int fenIndex = command.find("fen");
@@ -34,6 +81,8 @@ static void ParsePosition(Board &board, std::string_view command, SEARCH::Search
} else {
board.SetByFen(command.substr(fenIndex + 4, command.length() - (fenIndex + 3)));
}
ResetPositionHistory(ctx, board);
}
if (movesIndex != std::string::npos) {
@@ -41,6 +90,7 @@ static void ParsePosition(Board &board, std::string_view command, SEARCH::Search
for (std::string_view move : moves) {
board.MakeMove(UTILS::parseMove(board, move));
EnsurePositionHistory(ctx, board.positionIndex);
ctx->positionHistory[board.positionIndex] = board.hashKey;
}
}
@@ -89,22 +139,21 @@ static double ReadParam(const std::string& param, const std::string &command) {
#ifdef _WIN32
// Windows implementation using std::thread
template <SEARCH::searchMode mode>
static void ThreadFunc(Board* board, SearchParams params, SEARCH::SearchContext* ctx) {
SEARCH::SearchPosition<mode>(*board, params, ctx);
static void ThreadFunc(Board board, SearchParams params, SEARCH::SearchContext* ctx) {
SEARCH::SearchPosition<mode>(board, params, ctx);
delete ctx;
}
static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchContext* ctx, int id) {
std::thread searchThread;
SEARCH::SearchContext* ctxCopy = new SEARCH::SearchContext(*ctx);
if (id == 0)
ctxCopy->doPrint = true;
if (params.nodes) {
searchThread = std::thread(ThreadFunc<SEARCH::nodesMode>, &board, params, ctxCopy);
searchThreads.emplace_back(ThreadFunc<SEARCH::nodesMode>, board, params, ctxCopy);
} else {
searchThread = std::thread(ThreadFunc<SEARCH::normal>, &board, params, ctxCopy);
searchThreads.emplace_back(ThreadFunc<SEARCH::normal>, board, params, ctxCopy);
}
searchThread.detach();
}
#else
@@ -124,25 +173,46 @@ static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchC
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 8 * 1024 * 1024); // 8 MB stack
pthread_t thread;
SEARCH::SearchContext* ctxCopy = new SEARCH::SearchContext(*ctx);
if (id == 0)
ctxCopy->doPrint = true;
pthread_t thread;
int rc = 0;
if (params.nodes) {
auto* arg = new std::tuple<Board, SearchParams, SEARCH::SearchContext*>(board, params, ctxCopy);
pthread_create(&thread, &attr, ThreadFunc<SEARCH::nodesMode>, arg);
rc = pthread_create(&thread, &attr, ThreadFunc<SEARCH::nodesMode>, arg);
if (rc != 0) {
delete std::get<2>(*arg);
delete arg;
ctxCopy = nullptr;
}
} else {
auto* arg = new std::tuple<Board, SearchParams, SEARCH::SearchContext*>(board, params, ctxCopy);
pthread_create(&thread, &attr, ThreadFunc<SEARCH::normal>, arg);
rc = pthread_create(&thread, &attr, ThreadFunc<SEARCH::normal>, arg);
if (rc != 0) {
delete std::get<2>(*arg);
delete arg;
ctxCopy = nullptr;
}
}
pthread_attr_destroy(&attr);
pthread_detach(thread);
if (rc == 0) {
searchThreads.push_back(thread);
} else {
delete ctxCopy;
}
}
#endif
static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* ctx) {
StopSearchThreads();
searchStopped.store(false, std::memory_order_relaxed);
ctx->TT->IncreaseAge();
SearchParams params;
params.wtime = ReadParam("wtime", command);
@@ -160,12 +230,15 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* c
params.btime = 99999999;
}
searchThreads.reserve(threads);
for (int i = 0; i < threads; i++) {
StartSearchThread(board, params, ctx, i);
}
}
static void SetOption(std::string& command, SEARCH::SearchContext* ctx) {
StopSearchThreads();
if (command.find("Hash") != std::string::npos) {
U64 hashSize = (ReadParam("value", command) * 1000000ULL) / sizeof(TTEntry);
ctx->TT->Resize(hashSize);
@@ -228,7 +301,7 @@ static void SetOption(std::string& command, SEARCH::SearchContext* ctx) {
}
static void PrintEngineInfo() {
std::cout << "id name Eleanor v4.0" << std::endl;
std::cout << "id name Eleanor v4.1" << std::endl;
std::cout << "id author rektdie" << std::endl;
std::cout << "option name Hash type spin default 8 min 1 max 1024" << std::endl;
std::cout << "option name Threads type spin default 1 min 1 max 512" << std::endl;
@@ -265,6 +338,7 @@ void UCILoop(Board &board) {
// parse UCI "ucinewgame" command
if (input.find("ucinewgame") != std::string::npos) {
StopSearchThreads();
board.SetByFen(StartingFen);
// Clearing
@@ -274,7 +348,7 @@ void UCILoop(Board &board) {
ctx->history.Clear();
ctx->conthist.Clear();
ctx->corrhist.Clear();
ctx->positionHistory = {};
ResetPositionHistory(ctx.get(), board);
ctx->ss = {};
continue;
@@ -288,14 +362,14 @@ void UCILoop(Board &board) {
// parse UCI "quit" command
if (input.find("quit") != std::string::npos) {
StopSearchThreads();
// stop the loop
break;
}
// parse UCI "stop" command
if (input.find("stop") != std::string::npos) {
// stop the loop
searchStopped = true;
StopSearchThreads();
continue;
}
+2 -1
View File
@@ -1,11 +1,12 @@
#pragma once
#include <atomic>
#include "move.h"
#include "board.h"
inline bool UCIEnabled = false;
inline bool UCIShowWDL = false;
inline int threads = 1;
inline bool searchStopped = false;
inline std::atomic<bool> searchStopped = false;
class SearchParams {
public: