Skip to content

Commit

Permalink
Bench: 30658368
Browse files Browse the repository at this point in the history
  • Loading branch information
TerjeKir committed Nov 18, 2023
1 parent 737edda commit b45a151
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 46 deletions.
9 changes: 9 additions & 0 deletions src/move.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions src/move.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
29 changes: 26 additions & 3 deletions src/movegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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;
}
2 changes: 2 additions & 0 deletions src/movegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -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[]);
11 changes: 1 addition & 10 deletions src/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
9 changes: 4 additions & 5 deletions src/syzygy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down
7 changes: 1 addition & 6 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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++) {
Expand Down
23 changes: 2 additions & 21 deletions src/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <string.h>

#include "makemove.h"
#include "move.h"
#include "movegen.h"
#include "threads.h"

Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b45a151

Please sign in to comment.