2 Commits

Author SHA1 Message Date
96744cea20 perft-test: option to run perft/perft2/both 2024-03-29 10:00:01 +01:00
24207583d1 perft2: is_in_check() before recursion 2024-03-29 09:59:14 +01:00
2 changed files with 48 additions and 31 deletions

View File

@@ -100,8 +100,6 @@ u64 perft2(pos_t *pos, int depth, int ply)
move_t move; move_t move;
state_t state; state_t state;
if (is_in_check(pos, OPPONENT(pos->turn)))
return 0;
if (depth == 0) if (depth == 0)
return 1; return 1;
pos->checkers = pos_checkers(pos, pos->turn); pos->checkers = pos_checkers(pos, pos->turn);
@@ -113,13 +111,13 @@ u64 perft2(pos_t *pos, int depth, int ply)
for (nmove = 0; nmove < pseudo.nmoves; ++nmove ) { for (nmove = 0; nmove < pseudo.nmoves; ++nmove ) {
move = pseudo.move[nmove]; move = pseudo.move[nmove];
move_do(pos, move); move_do(pos, move);
if (!is_in_check(pos, OPPONENT(pos->turn))) {
subnodes = perft2(pos, depth - 1, ply + 1); subnodes = perft2(pos, depth - 1, ply + 1);
nodes += subnodes;
nodes += subnodes; if (ply == 1) {
if (ply == 1) { char movestr[8];
char movestr[8]; printf("%s: %d\n", move_str(movestr, move, 0), subnodes);
printf("%s: %d\n", move_str(movestr, move, 0), subnodes); }
} }
move_undo(pos, move); move_undo(pos, move);
pos->state = state; pos->state = state;

View File

@@ -233,11 +233,17 @@ int main(int __unused ac, __unused char**av)
//move_t move; //move_t move;
FILE *outfd; FILE *outfd;
int depth = 6; int depth = 6;
s64 ms1 = 0, ms1_total = 0, ms2 = 0, ms2_total = 0;
int run = 3;
if (ac > 1) if (ac > 1)
depth = atoi(av[1]); depth = atoi(av[1]);
printf("depth = %d\n", depth); if (ac > 2)
run = atoi(av[2]);
printf("depth = %d run=%d\n", depth, run);
if (!run)
exit(0);
setlocale(LC_NUMERIC, ""); setlocale(LC_NUMERIC, "");
setlinebuf(stdout); /* line-buffered stdout */ setlinebuf(stdout); /* line-buffered stdout */
outfd = open_stockfish(); outfd = open_stockfish();
@@ -245,6 +251,8 @@ int main(int __unused ac, __unused char**av)
bitboard_init(); bitboard_init();
hyperbola_init(); hyperbola_init();
CLOCK_DEFINE(clock, CLOCK_PROCESS);
while ((fen = next_fen(PERFT | MOVEDO))) { while ((fen = next_fen(PERFT | MOVEDO))) {
test_line = cur_line(); test_line = cur_line();
if (!(pos = fen2pos(NULL, fen))) { if (!(pos = fen2pos(NULL, fen))) {
@@ -256,35 +264,46 @@ int main(int __unused ac, __unused char**av)
savepos = pos_dup(pos); savepos = pos_dup(pos);
//int j = 0; //int j = 0;
s64 μs; if (run & 1) {
CLOCK_DEFINE(clock, CLOCK_SYSTEM); clock_start(&clock);
my_count = perft(pos, depth, 1);
ms1 = clock_elapsed_ms(&clock);
ms1_total += ms1;
clock_start(&clock); if (sf_count == my_count) {
my_count = perft(pos, depth, 1); printf("pt1 OK : line=%03d perft=%lu %'ldms lps=%'lu \"%s\"\n",
μs = clock_elapsed_μs(&clock); test_line, my_count, ms1,
ms1? my_count*1000l/ms1: 0,
if (sf_count == my_count) { fen);
printf("pt1 OK : line=%03d perft=%lu μs=%'ldms lps=%'lu \"%s\"\n", } else {
test_line, my_count, μs, my_count*1000000l/μs, fen); printf("pt1 ERR: line=%03d sf=%lu me=%lu \"%s\"\n",
} else { test_line, sf_count, my_count, fen);
printf("pt1 ERR: line=%03d sf=%lu me=%lu \"%s\"\n", }
test_line, sf_count, my_count, fen);
} }
clock_start(&clock); if (run & 2) {
my_count = perft2(pos, depth, 1); clock_start(&clock);
μs = clock_elapsed_μs(&clock); my_count = perft2(pos, depth, 1);
ms2 = clock_elapsed_ms(&clock);
ms2_total += ms2;
if (sf_count == my_count) { if (sf_count == my_count) {
printf("pt2 OK : line=%03d perft=%lu μs=%'ldms lps=%'lu \"%s\"\n\n", printf("pt2 OK : line=%03d perft=%lu %'ldms lps=%'lu \"%s\"\n\n",
test_line, my_count, μs, my_count*1000000l/μs, fen); test_line, my_count, ms2,
} else { ms2? my_count*1000l/ms2: 0,
printf("pt2 ERR: line=%03d sf=%lu me=%lu \"%s\"\n\n", fen);
test_line, sf_count, my_count, fen); } else {
printf("pt2 ERR: line=%03d sf=%lu me=%lu \"%s\"\n\n",
test_line, sf_count, my_count, fen);
}
} }
pos_del(savepos); pos_del(savepos);
pos_del(pos); pos_del(pos);
i++; i++;
} }
if (run & 1)
printf("total perft %'ldms\n", ms1_total);
if (run & 2)
printf("total perft2 %'ldms\n", ms2_total);
return 0; return 0;
} }