From 7c3e06e2d3e37c8dc60e35ffc73c45b8575aa781 Mon Sep 17 00:00:00 2001 From: Terje Date: Sat, 18 Nov 2023 01:14:22 +0100 Subject: [PATCH] Bench: 30658368 --- src/move.c | 9 +++++++++ src/move.h | 1 + src/movegen.c | 29 ++++++++++++++++++++++++++--- src/movegen.h | 2 ++ src/search.c | 11 +---------- src/syzygy.h | 9 ++++----- src/tests.c | 7 +------ src/threads.c | 23 ++--------------------- src/types.h | 2 +- 9 files changed, 47 insertions(+), 46 deletions(-) diff --git a/src/move.c b/src/move.c index 5f7f4ea5..8bc0e3c8 100644 --- a/src/move.c +++ b/src/move.c @@ -116,3 +116,12 @@ Move ParseMove(const char *str, const Position *pos) { return MOVE(from, to, pieceOn(from), pieceOn(to), promo, flag); } + +// Checks if the move is in the list of searchmoves if any were given +bool NotInSearchMoves(Move searchmoves[], Move move) { + if (searchmoves[0] == NOMOVE) return false; + for (Move *m = searchmoves; *m != NOMOVE; ++m) + if (*m == move) + return false; + return true; +} diff --git a/src/move.h b/src/move.h index 8ed00006..d8cb2eef 100644 --- a/src/move.h +++ b/src/move.h @@ -94,3 +94,4 @@ INLINE bool CastleLegal(const Position *pos, Square to) { bool MoveIsPseudoLegal(const Position *pos, Move move); char *MoveToStr(Move move); Move ParseMove(const char *ptrChar, const Position *pos); +bool NotInSearchMoves(Move searchmoves[], Move move); diff --git a/src/movegen.c b/src/movegen.c index 48b4814b..806bc2ca 100644 --- a/src/movegen.c +++ b/src/movegen.c @@ -146,7 +146,7 @@ INLINE void GenPieceType(const Position *pos, MoveList *list, const Color color, } } -// Generate moves +// Generate all quiet or noisy moves for the given color static void GenMoves(const Position *pos, MoveList *list, const Color color, const int type) { if (Multiple(pos->checkers)) @@ -161,12 +161,35 @@ static void GenMoves(const Position *pos, MoveList *list, const Color color, con GenPieceType(pos, list, color, type, KING); } -// Generate quiet moves void GenQuietMoves(const Position *pos, MoveList *list) { GenMoves(pos, list, sideToMove, QUIET); } -// Generate noisy moves void GenNoisyMoves(const Position *pos, MoveList *list) { GenMoves(pos, list, sideToMove, NOISY); } + +void GenAllMoves(const Position *pos, MoveList *list) { + GenNoisyMoves(pos, list); + GenQuietMoves(pos, list); +} + +// Counts the number of legal moves for the given position +int LegalMoveCount(Position *pos, Move searchmoves[]) { + int rootMoveCount = 0; + + MoveList list; + list.count = list.next = 0; + GenAllMoves(pos, &list); + + for (int i = 0; i < list.count; ++i) { + Move move = list.moves[list.next++].move; + if (NotInSearchMoves(searchmoves, move)) continue; + if (!MakeMove(pos, move)) continue; + ++rootMoveCount; + TakeMove(pos); + } + pos->nodes = 0; + + return rootMoveCount; +} diff --git a/src/movegen.h b/src/movegen.h index 67845599..0cabad3a 100644 --- a/src/movegen.h +++ b/src/movegen.h @@ -36,3 +36,5 @@ typedef struct { void GenNoisyMoves(const Position *pos, MoveList *list); void GenQuietMoves(const Position *pos, MoveList *list); +void GenAllMoves(const Position *pos, MoveList *list); +int LegalMoveCount(Position *pos, Move searchmoves[]); diff --git a/src/search.c b/src/search.c index 065b5786..99e04bab 100644 --- a/src/search.c +++ b/src/search.c @@ -51,15 +51,6 @@ CONSTR(1) InitReductions() { Reductions[1][depth][moves] = 1.35 + log(depth) * log(moves) / 2.75; // quiet } -// Checks if the move is in the list of searchmoves if any were given -static bool NotInSearchMoves(Move move) { - if (Limits.searchmoves[0] == NOMOVE) return false; - for (Move *m = Limits.searchmoves; *m != NOMOVE; ++m) - if (*m == move) - return false; - return true; -} - // Small positive score with some random variance static int DrawScore(Position *pos) { return 8 - (pos->nodes & 0x7); @@ -386,7 +377,7 @@ static int AlphaBeta(Thread *thread, Stack *ss, int alpha, int beta, Depth depth if (move == ss->excluded) continue; if (root && AlreadySearchedMultiPV(thread, move)) continue; - if (root && NotInSearchMoves(move)) continue; + if (root && NotInSearchMoves(Limits.searchmoves, move)) continue; bool quiet = moveIsQuiet(move); diff --git a/src/syzygy.h b/src/syzygy.h index e1d1b13c..ce72d655 100644 --- a/src/syzygy.h +++ b/src/syzygy.h @@ -84,9 +84,9 @@ static bool ProbeRoot(Position *pos, Move *move, unsigned *wdl, unsigned *dtz) { return false; // Extract information - unsigned from = TB_GET_FROM(result), - to = TB_GET_TO(result), - promo = TB_GET_PROMOTES(result); + unsigned from = TB_GET_FROM(result); + unsigned to = TB_GET_TO(result); + unsigned promo = TB_GET_PROMOTES(result); *move = MOVE(from, to, 0, 0, promo ? 6 - promo : 0, 0); *wdl = TB_GET_WDL(result); @@ -112,8 +112,7 @@ static bool SyzygyMove(Position *pos) { if (!success) return false; // Print thinking info - printf("info depth %d seldepth %d score cp %d " - "time 0 nodes 0 nps 0 tbhits 1 pv %s\n", + printf("info depth %d seldepth %d score cp %d time 0 nodes 0 nps 0 tbhits 1 pv %s\n", MAX_PLY, MAX_PLY, TBScore(wdl, dtz), MoveToStr(move)); fflush(stdout); diff --git a/src/tests.c b/src/tests.c index f05318bf..afded9e0 100644 --- a/src/tests.c +++ b/src/tests.c @@ -149,12 +149,6 @@ void Benchmark(int argc, char **argv) { #ifdef DEV -static void GenAllMoves(Position *pos, MoveList *list) { - list->count = list->next = 0; - GenNoisyMoves(pos, list); - GenQuietMoves(pos, list); -} - // Helper for Perft() static uint64_t RecursivePerft(Position *pos, const Depth depth) { @@ -163,6 +157,7 @@ static uint64_t RecursivePerft(Position *pos, const Depth depth) { uint64_t leafnodes = 0; MoveList list; + list.count = list.next = 0; GenAllMoves(pos, &list); for (int i = 0; i < list.count; i++) { diff --git a/src/threads.c b/src/threads.c index 7b9b7776..b0988bdd 100644 --- a/src/threads.c +++ b/src/threads.c @@ -22,6 +22,7 @@ #include #include "makemove.h" +#include "move.h" #include "movegen.h" #include "threads.h" @@ -90,29 +91,9 @@ uint64_t TotalTBHits() { return total; } -// Checks if the move is in the list of searchmoves if any were given -static bool NotInSearchMoves(Move searchmoves[], Move move) { - if (searchmoves[0] == 0) return false; - for (Move *m = searchmoves; *m != 0; ++m) - if (*m == move) - return false; - return true; -} - // Setup threads for a new search void PrepareSearch(Position *pos, Move searchmoves[]) { - int rootMoveCount = 0; - MoveList list = { 0 }; - GenNoisyMoves(pos, &list); - GenQuietMoves(pos, &list); - for (int i = 0; i < list.count; ++i) { - Move move = list.moves[list.next++].move; - if (NotInSearchMoves(searchmoves, move)) continue; - if (!MakeMove(pos, move)) continue; - ++rootMoveCount; - TakeMove(pos); - } - pos->nodes = 0; + int rootMoveCount = LegalMoveCount(pos, searchmoves); for (Thread *t = threads; t < threads + threads->count; ++t) { memset(t, 0, offsetof(Thread, pos)); diff --git a/src/types.h b/src/types.h index 6ad79e6c..38fd76e0 100644 --- a/src/types.h +++ b/src/types.h @@ -26,7 +26,7 @@ #define MIN(A, B) ((A) < (B) ? (A) : (B)) #define MAX(A, B) ((A) > (B) ? (A) : (B)) -#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) +#define CLAMP(x, low, high) (MIN((high), MAX((x), (low)))) #define INLINE static inline __attribute__((always_inline)) #define CONSTR(prio) static __attribute__((constructor (1000 + prio))) void