Repository navigation
Expand file tree
/
Copy pathperft_example.cpp
More file actions
909 lines (685 loc) · 25.3 KB
/
Copy pathperft_example.cpp
File metadata and controls
909 lines (685 loc) · 25.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
#include "position.h"
#include "printers.h"
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <functional>
#include <iostream>
#include <limits>
#include <mutex>
#include <sstream>
#include <stdexcept>
#include <string>
#include <thread>
#include <vector>
using namespace chess;
/* ===================== Transposition Table ===================== */
struct TTEntry {
uint64_t key = 0;
uint64_t nodes = 0;
uint8_t depth = 0;
};
static bool valid_tt_bits(size_t bits) {
return bits < std::numeric_limits<size_t>::digits && bits < std::numeric_limits<unsigned long long>::digits;
}
class PerftTT {
public:
PerftTT() = default;
explicit PerftTT(size_t bits) { resize(bits); }
void resize(size_t bits) {
if (!valid_tt_bits(bits))
throw std::runtime_error("TT size is too large");
const size_t count = size_t(1) << bits;
table.clear();
table.resize(count);
mask = count - 1;
}
inline bool probe(uint64_t key, int depth, uint64_t &out) const {
if (table.empty())
return false;
const TTEntry &e = table[key & mask];
if (e.key == key && e.depth == static_cast<uint8_t>(depth)) {
out = e.nodes;
return true;
}
return false;
}
inline void store(uint64_t key, int depth, uint64_t nodes) {
if (table.empty())
return;
TTEntry &e = table[key & mask];
if (e.key == 0 || e.key == key || depth >= static_cast<int>(e.depth)) {
e.key = key;
e.depth = static_cast<uint8_t>(depth);
e.nodes = nodes;
}
}
void clear() {
for (auto &e : table)
e.key = 0;
}
private:
std::vector<TTEntry> table;
size_t mask = 0;
};
/*
* IMPORTANT:
*
* There is deliberately NO global TT.
*
* Each worker owns its own PerftTT. The previous implementation had
* multiple threads concurrently reading/writing the same TTEntry,
* which was a C++ data race and therefore undefined behavior.
*/
/* ===================== Perft core (runtime depth, no template bloat) ===================== */
// Two runtime-depth recursions (not 24 template instantiations): one without
// TT (hot benchmark path, zero TT branches) and one with TT.
inline uint64_t perft_no_tt(Position &pos, int depth) {
if (depth <= 1) {
if (depth <= 0)
return 1;
CountOnlyList moves;
pos.legals(moves);
return moves.size_;
}
Movelist moves;
pos.legals(moves);
uint64_t nodes = 0;
for (const Move &m : moves) {
pos.doMove<false>(m);
nodes += perft_no_tt(pos, depth - 1);
pos.undoMove();
}
return nodes;
}
inline uint64_t perft_with_tt(Position &pos, int depth, PerftTT *tt) {
if (depth <= 1) {
if (depth <= 0)
return 1;
CountOnlyList moves;
pos.legals(moves);
return moves.size_;
}
const uint64_t key = pos.hash();
uint64_t cached;
if (tt->probe(key, depth, cached))
return cached;
Movelist moves;
pos.legals(moves);
uint64_t nodes = 0;
for (const Move &m : moves) {
pos.doMove<false>(m);
nodes += perft_with_tt(pos, depth - 1, tt);
pos.undoMove();
}
tt->store(key, depth, nodes);
return nodes;
}
inline uint64_t perft_no_div(Position &pos, int depth, PerftTT *tt) {
if (depth < 0)
return 0;
if (depth == 0)
return 1;
if (tt == nullptr)
return perft_no_tt(pos, depth);
return perft_with_tt(pos, depth, tt);
}
/* ===================== Threaded perft ===================== */
struct ThreadResult {
Move move;
uint64_t nodes = 0;
};
struct PerftTask {
const Position *pos;
int depth;
bool use_tt;
Move move;
};
/*
* Each worker gets its OWN TT.
*
* No mutex is required for TT access.
* No atomics are required for TT entries.
*
* The only shared state is:
* - tasks : immutable
* - root pos : immutable
* - results : one unique slot per task
* - next_idx : atomic work distributor
* - print_mtx : stdout serialization
*/
static void thread_worker(const std::vector<PerftTask> &tasks,
std::vector<ThreadResult> &results,
std::atomic<size_t> &next_idx,
std::mutex &print_mtx,
bool print_div,
size_t tt_bits) {
PerftTT local_tt;
if (tt_bits != 0)
local_tt.resize(tt_bits);
for (;;) {
const size_t idx = next_idx.fetch_add(1, std::memory_order_relaxed);
if (idx >= tasks.size())
break;
const PerftTask &task = tasks[idx];
/*
* Each worker has its own Position.
*
* The root Position is never modified by workers.
*/
Position local = *task.pos;
local.doMove<false>(task.move);
uint64_t nodes;
if (task.use_tt) {
nodes = perft_no_div(local, task.depth - 1, &local_tt);
} else {
nodes = perft_no_div(local, task.depth - 1, nullptr);
}
/*
* Each task owns exactly one results[idx].
* Therefore this write does not race with another worker.
*/
results[idx].move = task.move;
results[idx].nodes = nodes;
if (print_div) {
std::lock_guard<std::mutex> lock(print_mtx);
std::cout << task.move << ": " << nodes << '\n';
}
}
}
static uint64_t run_threaded_perft(Position &pos, int depth, size_t tt_bits, int threads, bool print_div) {
if (depth < 0)
return 0;
if (depth == 0)
return 1;
Movelist moves;
pos.legals(moves);
const size_t n_moves = moves.size();
if (n_moves == 0)
return 0;
const bool use_tt = tt_bits != 0;
std::vector<PerftTask> tasks(n_moves);
for (size_t i = 0; i < n_moves; ++i) {
tasks[i].pos = &pos;
tasks[i].depth = depth;
tasks[i].use_tt = use_tt;
tasks[i].move = moves[i];
}
std::vector<ThreadResult> results(n_moves);
std::atomic<size_t> next_idx{ 0 };
std::mutex print_mtx;
std::mutex worker_exception_mtx;
std::exception_ptr worker_exception;
const int n_threads = std::max(1, std::min(threads, static_cast<int>(n_moves)));
// Single-thread fast path: no thread spawn, no Position copies, no atomics.
// Same node counts and same div output order as the 1-worker threaded path.
if (n_threads == 1) {
PerftTT local_tt;
if (use_tt)
local_tt.resize(tt_bits);
PerftTT *ttp = use_tt ? &local_tt : nullptr;
uint64_t total = 0;
for (size_t i = 0; i < n_moves; ++i) {
pos.doMove<false>(moves[i]);
const uint64_t sub = perft_no_div(pos, depth - 1, ttp);
total += sub;
if (print_div)
std::cout << moves[i] << ": " << sub << '\n';
pos.undoMove();
}
return total;
}
std::vector<std::thread> thread_pool;
thread_pool.reserve(n_threads);
try {
for (int t = 0; t < n_threads; ++t) {
thread_pool.emplace_back([&] {
try {
thread_worker(tasks, results, next_idx, print_mtx, print_div, tt_bits);
} catch (...) {
std::lock_guard<std::mutex> lock(worker_exception_mtx);
if (!worker_exception)
worker_exception = std::current_exception();
}
});
}
} catch (...) {
for (auto &t : thread_pool) {
if (t.joinable())
t.join();
}
throw;
}
for (auto &t : thread_pool)
t.join();
if (worker_exception)
std::rethrow_exception(worker_exception);
uint64_t total = 0;
for (const auto &r : results)
total += r.nodes;
return total;
}
/* ===================== Helpers ===================== */
static unsigned hardware_threads() {
const unsigned n = std::thread::hardware_concurrency();
return n ? n : 1;
}
static size_t mb_to_tt_bits(int mb) {
if (mb <= 0)
return 0;
/*
* TTEntry may contain padding, so sizeof(TTEntry) is deliberately
* used rather than assuming a particular entry size.
*/
const uint64_t bytes = static_cast<uint64_t>(mb) * 1024ULL * 1024ULL;
const uint64_t entries = bytes / sizeof(TTEntry);
if (entries < 1)
return 0;
size_t bits = 0;
/*
* Find the largest power-of-two table that fits in the requested
* memory.
*/
while (bits + 1 < sizeof(size_t) * 8) {
const uint64_t next = 1ULL << (bits + 1);
if (next > entries)
break;
++bits;
}
return bits;
}
static bool has_one_king_each_color(const Position &pos) {
return pos.count<KING, WHITE>() == 1 && pos.count<KING, BLACK>() == 1;
}
/* ===================== Perft command ===================== */
static void run_perft(Position &pos, int depth, size_t tt_bits, int threads) {
if (!valid_tt_bits(tt_bits)) {
std::cout << "info string TT size is too large\n";
std::cout.flush();
return;
}
if (depth < 0) {
std::cout << "info string Depth must be non-negative\n";
return;
}
try {
const auto start = std::chrono::steady_clock::now();
/*
* For benchmark/UCI use, division output is disabled here.
*
* Printing every root move can dominate the benchmark.
*/
const uint64_t nodes = run_threaded_perft(pos, depth, tt_bits, threads, false);
const auto end = std::chrono::steady_clock::now();
const std::chrono::duration<double> elapsed = end - start;
const uint64_t milliseconds = static_cast<uint64_t>(elapsed.count() * 1000.0);
const uint64_t nps = elapsed.count() > 0.0 ? static_cast<uint64_t>(nodes / elapsed.count()) : 0;
std::cout << "time " << milliseconds << '\n';
std::cout << "nps " << nps << '\n';
std::cout << "nodes searched: " << nodes << std::endl;
} catch (const std::exception &e) {
std::cout << "info string Perft error: " << e.what() << '\n';
std::cout.flush();
}
}
/* ===================== CLI usage ===================== */
static void print_usage(const char *prog) {
std::cerr << "Usage: " << prog << " [options]\n"
<< " -f,--fen FEN FEN string (default: startpos)\n"
<< " -d,--depth N Perft depth (default: 6)\n"
<< " -m,--hash MB Hash size in MB per worker (default: 0)\n"
<< " -z,--ttsize N TT size in bits, raw override (default: 0)\n"
<< " -t,--threads N Number of threads (default: 1)\n"
<< "\n"
<< "No arguments: enter UCI mode.\n"
<< "\n"
<< "UCI commands:\n"
<< " uci\n"
<< " isready\n"
<< " setoption name Threads value <N>\n"
<< " setoption name Hash value <MB> (per worker)\n"
<< " ucinewgame\n"
<< " position [startpos | fen <FEN>] [moves <moves>]\n"
<< " go perft <depth> [hash <MB>] [threads <N>]\n"
<< " quit\n";
}
/* ===================== Tokenization ===================== */
static std::vector<std::string> split_args(const std::string &cmd) {
std::vector<std::string> tokens;
std::istringstream iss(cmd);
std::string tok;
while (iss >> tok)
tokens.push_back(tok);
return tokens;
}
/* ===================== Standalone CLI ===================== */
static int uci_command_line(int argc, char **argv) {
std::string fen = Position::START_FEN;
std::string ttsize_value = "0";
size_t tt_bits = 0;
int depth = 6;
int threads = 1;
for (int i = 1; i < argc; ++i) {
if ((std::strcmp(argv[i], "-f") == 0 || std::strcmp(argv[i], "--fen") == 0) && i + 1 < argc) {
fen = argv[++i];
} else if ((std::strcmp(argv[i], "-d") == 0 || std::strcmp(argv[i], "--depth") == 0) && i + 1 < argc) {
const std::string value = argv[++i];
try {
size_t parsed_chars = 0;
const long parsed = std::stol(value, &parsed_chars);
if (parsed_chars != value.size() || parsed < 0 || parsed > std::numeric_limits<int>::max())
throw std::invalid_argument("invalid depth");
depth = static_cast<int>(parsed);
} catch (const std::exception &) {
std::cerr << "Invalid --depth value: " << value << '\n';
return EXIT_FAILURE;
}
} else if ((std::strcmp(argv[i], "-z") == 0 || std::strcmp(argv[i], "--ttsize") == 0) && i + 1 < argc) {
ttsize_value = argv[++i];
try {
size_t parsed_chars = 0;
const unsigned long parsed = std::stoul(ttsize_value, &parsed_chars);
if (parsed_chars != ttsize_value.size() || parsed > std::numeric_limits<size_t>::max())
throw std::invalid_argument("invalid TT size");
tt_bits = static_cast<size_t>(parsed);
} catch (const std::exception &) {
std::cerr << "Invalid --ttsize value: " << ttsize_value << '\n';
return EXIT_FAILURE;
}
} else if ((std::strcmp(argv[i], "-m") == 0 || std::strcmp(argv[i], "--hash") == 0) && i + 1 < argc) {
const std::string value = argv[++i];
try {
size_t parsed_chars = 0;
const long parsed = std::stol(value, &parsed_chars);
if (parsed_chars != value.size() || parsed < 0 || parsed > std::numeric_limits<int>::max())
throw std::invalid_argument("invalid hash size");
tt_bits = mb_to_tt_bits(static_cast<int>(parsed));
} catch (const std::exception &) {
std::cerr << "Invalid --hash value: " << value << '\n';
return EXIT_FAILURE;
}
} else if ((std::strcmp(argv[i], "-t") == 0 || std::strcmp(argv[i], "--threads") == 0) && i + 1 < argc) {
const std::string value = argv[++i];
try {
size_t parsed_chars = 0;
const long parsed = std::stol(value, &parsed_chars);
if (parsed_chars != value.size() || parsed < 1 || parsed > 1024)
throw std::invalid_argument("invalid thread count");
threads = static_cast<int>(parsed);
} catch (const std::exception &) {
std::cerr << "Invalid --threads value: " << value << '\n';
return EXIT_FAILURE;
}
} else if (std::strcmp(argv[i], "-h") == 0 || std::strcmp(argv[i], "--help") == 0) {
print_usage(argv[0]);
return EXIT_SUCCESS;
}
}
if (!valid_tt_bits(tt_bits)) {
std::cerr << "Unsupported --ttsize width: " << ttsize_value << '\n';
return EXIT_FAILURE;
}
Position pos;
try {
if (!pos.setFEN(fen))
throw std::runtime_error("invalid FEN");
if (!has_one_king_each_color(pos))
throw std::runtime_error("FEN must contain exactly one king per color");
} catch (const std::exception &e) {
std::cerr << "Invalid FEN: " << e.what() << '\n';
return EXIT_FAILURE;
}
std::cerr << "FEN: " << fen << '\n';
std::cerr << "Depth: " << depth << " | Threads: " << threads << " | TT: ";
if (tt_bits != 0)
std::cerr << (1ULL << tt_bits) << " entries";
else
std::cerr << "disabled";
std::cerr << '\n';
const auto start = std::chrono::steady_clock::now();
uint64_t nodes = 0;
try {
nodes = run_threaded_perft(pos, depth, tt_bits, threads, false);
} catch (const std::exception &e) {
std::cerr << "Perft error: " << e.what() << '\n';
return EXIT_FAILURE;
}
const auto end = std::chrono::steady_clock::now();
const std::chrono::duration<double> elapsed = end - start;
std::cout << "Perft(" << depth << ") = " << nodes << '\n';
std::cout << "Time = " << elapsed.count() << " s\n";
std::cout << "Speed = " << (elapsed.count() > 0.0 ? static_cast<uint64_t>(nodes / elapsed.count()) : 0) << " nps\n";
return EXIT_SUCCESS;
}
/* ===================== UCI ===================== */
static void uci_loop() {
Position pos;
pos.setFEN(Position::START_FEN);
/*
* Hash is disabled by default.
*
* This now agrees with the CLI and the actual initial state.
*/
size_t tt_bits = 0;
int threads = 1;
std::string line;
while (std::getline(std::cin, line)) {
const auto tokens = split_args(line);
if (tokens.empty())
continue;
const std::string &cmd = tokens[0];
/* ---------- quit ---------- */
if (cmd == "quit") {
break;
}
/* ---------- uci ---------- */
else if (cmd == "uci") {
std::cout << "id name chesslib-perft\n";
std::cout << "id author winapiadmin\n";
std::cout << "option name Threads "
<< "type spin "
<< "default 1 "
<< "min 1 "
<< "max 1024\n";
std::cout << "option name Hash "
<< "type spin "
<< "default 0 "
<< "min 0 "
<< "max 65536\n"
<< "info string Hash size is allocated per worker\n";
std::cout << "uciok\n";
std::cout.flush();
}
/* ---------- isready ---------- */
else if (cmd == "isready") {
std::cout << "readyok\n";
std::cout.flush();
}
/* ---------- setoption ---------- */
else if (cmd == "setoption") {
/*
* setoption name <name> value <value>
*/
size_t i = tokens.size() >= 2 && tokens[1] == "name" ? 2 : 1;
std::string opt_name;
while (i < tokens.size() && tokens[i] != "value") {
if (!opt_name.empty())
opt_name += ' ';
opt_name += tokens[i];
++i;
}
if (i < tokens.size() && tokens[i] == "value") {
++i;
}
std::string opt_val;
while (i < tokens.size()) {
if (!opt_val.empty())
opt_val += ' ';
opt_val += tokens[i];
++i;
}
try {
if (opt_name == "Threads") {
const int t = std::stoi(opt_val);
threads = std::max(1, std::min(t, 1024));
} else if (opt_name == "Hash") {
const int mb = std::stoi(opt_val);
tt_bits = mb_to_tt_bits(mb);
}
} catch (const std::exception &) {
std::cout << "info string Invalid setoption value\n";
std::cout.flush();
}
}
/* ---------- ucinewgame ---------- */
else if (cmd == "ucinewgame") {
pos.setFEN(Position::START_FEN);
/*
* There is no global TT anymore.
*
* Every perft worker creates a fresh TT for its run,
* so there is nothing to clear here.
*/
}
/* ---------- position ---------- */
else if (cmd == "position") {
size_t i = 1;
if (i < tokens.size() && tokens[i] == "startpos") {
pos.setFEN(Position::START_FEN);
++i;
} else if (i < tokens.size() && tokens[i] == "fen") {
++i;
std::string fen_str;
/*
* FEN consists of six space-separated fields.
* Stop when "moves" begins.
*/
while (i < tokens.size() && tokens[i] != "moves") {
if (!fen_str.empty())
fen_str += ' ';
fen_str += tokens[i];
++i;
}
if (fen_str.empty()) {
std::cout << "info string Missing FEN value\n";
std::cout.flush();
continue;
}
try {
Position candidate = pos;
if (!candidate.setFEN(fen_str))
throw std::runtime_error("invalid FEN");
if (!has_one_king_each_color(candidate))
throw std::runtime_error("FEN must contain exactly one king per color");
pos = std::move(candidate);
} catch (const std::exception &e) {
std::cout << "info string Invalid FEN: " << e.what() << '\n';
std::cout.flush();
continue;
}
} else {
std::cout << "info string Expected 'startpos' or 'fen'\n";
std::cout.flush();
continue;
}
/* ---------- position moves ---------- */
if (i < tokens.size() && tokens[i] == "moves") {
++i;
while (i < tokens.size()) {
try {
pos.push_uci(tokens[i]);
} catch (const std::exception &e) {
std::cout << "info string Invalid move " << tokens[i] << ": " << e.what() << '\n';
break;
}
++i;
}
}
}
/* ---------- go ---------- */
else if (cmd == "go") {
int depth = -1;
size_t local_tt = tt_bits;
int local_threads = threads;
bool perft_cmd = false;
std::string parsing_value;
std::string ttsize_value;
size_t i = 1;
try {
while (i < tokens.size()) {
if (tokens[i] == "perft" && i + 1 < tokens.size()) {
parsing_value = tokens[i + 1];
size_t parsed_chars = 0;
const long parsed_depth = std::stol(parsing_value, &parsed_chars);
if (parsed_chars != parsing_value.size() || parsed_depth < 0 ||
parsed_depth > std::numeric_limits<int>::max())
throw std::invalid_argument("invalid depth");
depth = static_cast<int>(parsed_depth);
perft_cmd = true;
i += 2;
} else if (tokens[i] == "threads" && i + 1 < tokens.size()) {
parsing_value = tokens[i + 1];
local_threads = std::stoi(parsing_value);
local_threads = std::max(1, std::min(local_threads, 1024));
i += 2;
} else if (tokens[i] == "hash" && i + 1 < tokens.size()) {
parsing_value = tokens[i + 1];
local_tt = mb_to_tt_bits(std::stoi(parsing_value));
i += 2;
} else if (tokens[i] == "ttsize" && i + 1 < tokens.size()) {
ttsize_value = tokens[i + 1];
parsing_value = ttsize_value;
size_t parsed_chars = 0;
const unsigned long parsed = std::stoul(parsing_value, &parsed_chars);
if (parsed_chars != parsing_value.size() || parsed > std::numeric_limits<size_t>::max())
throw std::invalid_argument("invalid TT size");
local_tt = static_cast<size_t>(parsed);
i += 2;
} else {
/*
* Ignore unsupported UCI go parameters.
*/
++i;
}
}
} catch (const std::exception &) {
std::cout << "info string Invalid go perft value: " << parsing_value << '\n';
std::cout.flush();
continue;
}
if (!valid_tt_bits(local_tt)) {
std::cout << "info string Unsupported go perft ttsize width: " << ttsize_value << '\n';
std::cout.flush();
continue;
}
if (!perft_cmd || depth < 0) {
std::cout << "info string Usage: "
<< "go perft <depth> "
<< "[threads <N>] "
<< "[hash <MB>] "
<< "[ttsize <bits>]\n";
std::cout.flush();
continue;
}
run_perft(pos, depth, local_tt, local_threads);
}
/* ---------- unknown ---------- */
else {
std::cout << "info string Unknown command: " << cmd << '\n';
std::cout.flush();
}
}
}
/* ===================== main ===================== */
int main(int argc, char **argv) {
if (argc > 1) {
return uci_command_line(argc, argv);
} else {
uci_loop();
}
return 0;
}