diff --git a/src/bitboard.h b/src/bitboard.h index 3d629de1..a813ea12 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -166,7 +166,7 @@ template constexpr Bitboard shift(Bitboard b) { return D == NORTH ? b << 8 : D == SOUTH ? b >> 8 : D == EAST ? (b & ~FileHBB) << 1 : D == WEST ? (b & ~FileABB) >> 1 - : D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == NORTH_WEST ? (b & ~FileABB) << 7 + : D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == NORTH_WEST ? (b & ~FileABB) << 7 : D == SOUTH_EAST ? (b & ~FileHBB) >> 7 : D == SOUTH_WEST ? (b & ~FileABB) >> 9 : 0; } diff --git a/src/misc.h b/src/misc.h index 563a58a5..1112090b 100644 --- a/src/misc.h +++ b/src/misc.h @@ -41,6 +41,8 @@ void dbg_print(); typedef std::chrono::milliseconds::rep TimePoint; // A value in milliseconds +static_assert(sizeof(TimePoint) == sizeof(int64_t), "TimePoint should be 64 bits"); + inline TimePoint now() { return std::chrono::duration_cast (std::chrono::steady_clock::now().time_since_epoch()).count(); diff --git a/src/search.cpp b/src/search.cpp index 7a8d3253..1a516ff8 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -1495,7 +1495,7 @@ void MainThread::check_time() { static TimePoint lastInfoTime = now(); - int elapsed = Time.elapsed(); + TimePoint elapsed = Time.elapsed(); TimePoint tick = Limits.startTime + elapsed; if (tick - lastInfoTime >= 1000) @@ -1521,7 +1521,7 @@ void MainThread::check_time() { string UCI::pv(const Position& pos, Depth depth, Value alpha, Value beta) { std::stringstream ss; - int elapsed = Time.elapsed() + 1; + TimePoint elapsed = Time.elapsed() + 1; const RootMoves& rootMoves = pos.this_thread()->rootMoves; size_t PVIdx = pos.this_thread()->PVIdx; size_t multiPV = std::min((size_t)Options["MultiPV"], rootMoves.size()); diff --git a/src/search.h b/src/search.h index 22f9f0d7..671e1277 100644 --- a/src/search.h +++ b/src/search.h @@ -90,10 +90,9 @@ struct LimitsType { } std::vector searchmoves; - int time[COLOR_NB], inc[COLOR_NB], npmsec, movestogo, depth, - movetime, mate, perft, infinite; + TimePoint time[COLOR_NB], inc[COLOR_NB], npmsec, movetime, startTime; + int movestogo, depth, mate, perft, infinite; int64_t nodes; - TimePoint startTime; }; extern LimitsType Limits; diff --git a/src/timeman.cpp b/src/timeman.cpp index e63454eb..ade25c49 100644 --- a/src/timeman.cpp +++ b/src/timeman.cpp @@ -52,13 +52,13 @@ namespace { } template - int remaining(int myTime, int movesToGo, int ply, int slowMover) { + TimePoint remaining(TimePoint myTime, int movesToGo, int ply, TimePoint slowMover) { - constexpr double TMaxRatio = (T == OptimumTime ? 1 : MaxRatio); - constexpr double TStealRatio = (T == OptimumTime ? 0 : StealRatio); + constexpr double TMaxRatio = (T == OptimumTime ? 1.0 : MaxRatio); + constexpr double TStealRatio = (T == OptimumTime ? 0.0 : StealRatio); - double moveImportance = (move_importance(ply) * slowMover) / 100; - double otherMovesImportance = 0; + double moveImportance = (move_importance(ply) * slowMover) / 100.0; + double otherMovesImportance = 0.0; for (int i = 1; i < movesToGo; ++i) otherMovesImportance += move_importance(ply + 2 * i); @@ -66,7 +66,7 @@ namespace { double ratio1 = (TMaxRatio * moveImportance) / (TMaxRatio * moveImportance + otherMovesImportance); double ratio2 = (moveImportance + TStealRatio * otherMovesImportance) / (moveImportance + otherMovesImportance); - return int(myTime * std::min(ratio1, ratio2)); // Intel C++ asks for an explicit cast + return TimePoint(myTime * std::min(ratio1, ratio2)); // Intel C++ asks for an explicit cast } } // namespace @@ -83,22 +83,23 @@ namespace { void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) { - int minThinkingTime = Options["Minimum Thinking Time"]; - int moveOverhead = Options["Move Overhead"]; - int slowMover = Options["Slow Mover"]; - int npmsec = Options["nodestime"]; + TimePoint minThinkingTime = Options["Minimum Thinking Time"]; + TimePoint moveOverhead = Options["Move Overhead"]; + TimePoint slowMover = Options["Slow Mover"]; + TimePoint npmsec = Options["nodestime"]; + TimePoint hypMyTime; // If we have to play in 'nodes as time' mode, then convert from time // to nodes, and use resulting values in time management formulas. - // WARNING: Given npms (nodes per millisecond) must be much lower then - // the real engine speed to avoid time losses. + // WARNING: to avoid time losses, the given npmsec (nodes per millisecond) + // must be much lower than the real engine speed. if (npmsec) { if (!availableNodes) // Only once at game start availableNodes = npmsec * limits.time[us]; // Time is in msec - // Convert from millisecs to nodes - limits.time[us] = (int)availableNodes; + // Convert from milliseconds to nodes + limits.time[us] = TimePoint(availableNodes); limits.inc[us] *= npmsec; limits.npmsec = npmsec; } @@ -108,20 +109,20 @@ void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) { const int maxMTG = limits.movestogo ? std::min(limits.movestogo, MoveHorizon) : MoveHorizon; - // We calculate optimum time usage for different hypothetical "moves to go"-values + // We calculate optimum time usage for different hypothetical "moves to go" values // and choose the minimum of calculated search time values. Usually the greatest // hypMTG gives the minimum values. for (int hypMTG = 1; hypMTG <= maxMTG; ++hypMTG) { // Calculate thinking time for hypothetical "moves to go"-value - int hypMyTime = limits.time[us] - + limits.inc[us] * (hypMTG - 1) - - moveOverhead * (2 + std::min(hypMTG, 40)); + hypMyTime = limits.time[us] + + limits.inc[us] * (hypMTG - 1) + - moveOverhead * (2 + std::min(hypMTG, 40)); - hypMyTime = std::max(hypMyTime, 0); + hypMyTime = std::max(hypMyTime, TimePoint(0)); - int t1 = minThinkingTime + remaining(hypMyTime, hypMTG, ply, slowMover); - int t2 = minThinkingTime + remaining(hypMyTime, hypMTG, ply, slowMover); + TimePoint t1 = minThinkingTime + remaining(hypMyTime, hypMTG, ply, slowMover); + TimePoint t2 = minThinkingTime + remaining(hypMyTime, hypMTG, ply, slowMover); optimumTime = std::min(t1, optimumTime); maximumTime = std::min(t2, maximumTime); diff --git a/src/timeman.h b/src/timeman.h index f4e3a959..92854862 100644 --- a/src/timeman.h +++ b/src/timeman.h @@ -31,16 +31,16 @@ class TimeManagement { public: void init(Search::LimitsType& limits, Color us, int ply); - int optimum() const { return optimumTime; } - int maximum() const { return maximumTime; } - int elapsed() const { return int(Search::Limits.npmsec ? Threads.nodes_searched() : now() - startTime); } + TimePoint optimum() const { return optimumTime; } + TimePoint maximum() const { return maximumTime; } + TimePoint elapsed() const { return int(Search::Limits.npmsec ? Threads.nodes_searched() : now() - startTime); } int64_t availableNodes; // When in 'nodes as time' mode private: TimePoint startTime; - int optimumTime; - int maximumTime; + TimePoint optimumTime; + TimePoint maximumTime; }; extern TimeManagement Time;