+ else if (token == "perft") is >> limits.perft;
+ else if (token == "infinite") limits.infinite = 1;
+ else if (token == "ponder") ponderMode = true;
+
+ Threads.start_thinking(pos, states, limits, ponderMode);
+ }
+
+
+ // bench() is called when the engine receives the "bench" command.
+ // Firstly, a list of UCI commands is set up according to the bench
+ // parameters, then it is run one by one, printing a summary at the end.
+
+ void bench(Position& pos, istream& args, StateListPtr& states) {
+
+ string token;
+ uint64_t num, nodes = 0, cnt = 1;
+
+ vector<string> list = setup_bench(pos, args);
+ num = count_if(list.begin(), list.end(), [](const string& s) { return s.find("go ") == 0 || s.find("eval") == 0; });
+
+ TimePoint elapsed = now();
+
+ for (const auto& cmd : list)
+ {
+ istringstream is(cmd);
+ is >> skipws >> token;
+
+ if (token == "go" || token == "eval")
+ {
+ cerr << "\nPosition: " << cnt++ << '/' << num << " (" << pos.fen() << ")" << endl;
+ if (token == "go")
+ {
+ go(pos, is, states);
+ Threads.main()->wait_for_search_finished();
+ nodes += Threads.nodes_searched();
+ }
+ else
+ trace_eval(pos);
+ }
+ else if (token == "setoption") setoption(is);
+ else if (token == "position") position(pos, is, states);
+ else if (token == "ucinewgame") { Search::clear(); elapsed = now(); } // Search::clear() may take a while
+ }
+
+ elapsed = now() - elapsed + 1; // Ensure positivity to avoid a 'divide by zero'
+
+ dbg_print();
+
+ cerr << "\n==========================="
+ << "\nTotal time (ms) : " << elapsed
+ << "\nNodes searched : " << nodes
+ << "\nNodes/second : " << 1000 * nodes / elapsed << endl;
+ }
+
+ // The win rate model returns the probability of winning (in per mille units) given an
+ // eval and a game ply. It fits the LTC fishtest statistics rather accurately.
+ int win_rate_model(Value v, int ply) {
+
+ // The model only captures up to 240 plies, so limit the input and then rescale
+ double m = std::min(240, ply) / 64.0;
+
+ // The coefficients of a third-order polynomial fit is based on the fishtest data
+ // for two parameters that need to transform eval to the argument of a logistic
+ // function.
+ constexpr double as[] = { 0.33677609, -4.30175627, 33.08810557, 365.60223431};
+ constexpr double bs[] = { -2.50471102, 14.23235405, -14.33066859, 71.42705250 };
+
+ // Enforce that NormalizeToPawnValue corresponds to a 50% win rate at ply 64
+ static_assert(UCI::NormalizeToPawnValue == int(as[0] + as[1] + as[2] + as[3]));
+
+ double a = (((as[0] * m + as[1]) * m + as[2]) * m) + as[3];
+ double b = (((bs[0] * m + bs[1]) * m + bs[2]) * m) + bs[3];