]> git.sesse.net Git - stockfish/commitdiff
Consolidate to centipawns conversion
authorJoost VandeVondele <Joost.VandeVondele@gmail.com>
Sat, 15 Jul 2023 11:34:16 +0000 (13:34 +0200)
committerJoost VandeVondele <Joost.VandeVondele@gmail.com>
Sun, 16 Jul 2023 13:14:50 +0000 (15:14 +0200)
avoid doing this calculations in multiple places.

closes https://github.com/official-stockfish/Stockfish/pull/4686

No functional change

src/evaluate.cpp
src/nnue/evaluate_nnue.cpp
src/uci.cpp
src/uci.h

index d4d8daee4da730c072ffa8321f39f414aa085288..7f0ea4bc6040c4d38d0958fe0b3e61eeafe5a5de 100644 (file)
@@ -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";
 
index a1a90023909f98f25826691d738ac838a33e551d..d90f59a222b014f2d969cd4fea7ef6996b9a555f 100644 (file)
@@ -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;
   }
 
 
index ed16f24c382ec5d11555f9ab0fab4fa29b42dc47..f893bd9cece7becbc36f38c15ef4155386f07a4e 100644 (file)
@@ -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 <x>    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
index 8f1be00c7cbd514f614637168dd9719a313ac763..2e40c9125d167a3bbbeec28eb47b8f873f84e749 100644 (file)
--- 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);