From e8a5c64988d4c0d3d6c87e3ba3204ab4cf512bcb Mon Sep 17 00:00:00 2001 From: Joost VandeVondele Date: Sat, 15 Jul 2023 13:34:16 +0200 Subject: [PATCH] Consolidate to centipawns conversion avoid doing this calculations in multiple places. closes https://github.com/official-stockfish/Stockfish/pull/4686 No functional change --- src/evaluate.cpp | 6 ++---- src/nnue/evaluate_nnue.cpp | 8 ++++---- src/uci.cpp | 9 ++++++++- src/uci.h | 1 + 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/evaluate.cpp b/src/evaluate.cpp index d4d8daee..7f0ea4bc 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -58,8 +58,6 @@ namespace Eval { string currentEvalFileName = "None"; - static double to_cp(Value v) { return double(v) / UCI::NormalizeToPawnValue; } - /// NNUE::init() tries to load a NNUE network at startup time, or when the engine /// receives a UCI command "setoption name EvalFile value nn-[a-z0-9]{12}.nnue" /// The name of the NNUE network is always retrieved from the EvalFile option. @@ -194,11 +192,11 @@ std::string Eval::trace(Position& pos) { Value v; v = NNUE::evaluate(pos, false); v = pos.side_to_move() == WHITE ? v : -v; - ss << "NNUE evaluation " << to_cp(v) << " (white side)\n"; + ss << "NNUE evaluation " << 0.01 * UCI::to_cp(v) << " (white side)\n"; v = evaluate(pos); v = pos.side_to_move() == WHITE ? v : -v; - ss << "Final evaluation " << to_cp(v) << " (white side)"; + ss << "Final evaluation " << 0.01 * UCI::to_cp(v) << " (white side)"; ss << " [with scaled NNUE, ...]"; ss << "\n"; diff --git a/src/nnue/evaluate_nnue.cpp b/src/nnue/evaluate_nnue.cpp index a1a90023..d90f59a2 100644 --- a/src/nnue/evaluate_nnue.cpp +++ b/src/nnue/evaluate_nnue.cpp @@ -224,7 +224,7 @@ namespace Stockfish::Eval::NNUE { buffer[0] = (v < 0 ? '-' : v > 0 ? '+' : ' '); - int cp = std::abs(100 * v / UCI::NormalizeToPawnValue); + int cp = std::abs(UCI::to_cp(v)); if (cp >= 10000) { buffer[1] = '0' + cp / 10000; cp %= 10000; @@ -249,15 +249,15 @@ namespace Stockfish::Eval::NNUE { } - // format_cp_aligned_dot() converts a Value into (centi)pawns, always keeping two decimals. + // format_cp_aligned_dot() converts a Value into pawns, always keeping two decimals. static void format_cp_aligned_dot(Value v, std::stringstream &stream) { - const double cp = 1.0 * std::abs(int(v)) / UCI::NormalizeToPawnValue; + const double pawns = std::abs(0.01 * UCI::to_cp(v)); stream << (v < 0 ? '-' : v > 0 ? '+' : ' ') << std::setiosflags(std::ios::fixed) << std::setw(6) << std::setprecision(2) - << cp; + << pawns; } diff --git a/src/uci.cpp b/src/uci.cpp index ed16f24c..f893bd9c 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -303,6 +303,13 @@ void UCI::loop(int argc, char* argv[]) { } +/// Turns a Value to an integer centipawn number, +/// without treatment of mate and similar special scores. +int UCI::to_cp(Value v) { + + return 100 * v / UCI::NormalizeToPawnValue; +} + /// UCI::value() converts a Value to a string by adhering to the UCI protocol specification: /// /// cp The score from the engine's point of view in centipawns. @@ -316,7 +323,7 @@ string UCI::value(Value v) { stringstream ss; if (abs(v) < VALUE_TB_WIN_IN_MAX_PLY) - ss << "cp " << v * 100 / NormalizeToPawnValue; + ss << "cp " << UCI::to_cp(v); else if (abs(v) < VALUE_MATE_IN_MAX_PLY) { const int ply = VALUE_MATE_IN_MAX_PLY - 1 - std::abs(v); // recompute ss->ply diff --git a/src/uci.h b/src/uci.h index 8f1be00c..2e40c912 100644 --- a/src/uci.h +++ b/src/uci.h @@ -76,6 +76,7 @@ private: void init(OptionsMap&); void loop(int argc, char* argv[]); +int to_cp(Value v); std::string value(Value v); std::string square(Square s); std::string move(Move m, bool chess960); -- 2.39.2