]> git.sesse.net Git - stockfish/commitdiff
Assorted tweaks from DON
authorMarco Costalba <mcostalba@gmail.com>
Sun, 9 Feb 2014 16:31:45 +0000 (17:31 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Sun, 9 Feb 2014 16:31:45 +0000 (17:31 +0100)
Mainly renames and some little code style improvment,
inspired by looking at DON sources:

https://github.com/erashid/DON

No functional change.

16 files changed:
src/benchmark.cpp
src/bitboard.cpp
src/main.cpp
src/misc.cpp
src/misc.h
src/position.h
src/rkiss.h
src/search.cpp
src/search.h
src/thread.cpp
src/thread.h
src/tt.cpp
src/tt.h
src/uci.cpp
src/ucioption.cpp
src/ucioption.h

index 7ca4b59e0ff13e7112228cd27d236c42bdc0d9cb..63a9d84c7eaf5369d873df9d0dbded8f18df49e0 100644 (file)
@@ -126,7 +126,7 @@ void benchmark(const Position& current, istream& is) {
       file.close();
   }
 
-  int64_t nodes = 0;
+  uint64_t nodes = 0;
   Search::StateStackPtr st;
   Time::point elapsed = Time::now();
 
@@ -138,13 +138,13 @@ void benchmark(const Position& current, istream& is) {
 
       if (limitType == "perft")
       {
-          size_t cnt = Search::perft(pos, limits.depth * ONE_PLY);
+          uint64_t cnt = Search::perft(pos, limits.depth * ONE_PLY);
           cerr << "\nPerft " << limits.depth  << " leaf nodes: " << cnt << endl;
           nodes += cnt;
       }
       else
       {
-          Threads.start_thinking(pos, limits, vector<Move>(), st);
+          Threads.start_thinking(pos, limits, st);
           Threads.wait_for_think_finished();
           nodes += Search::RootPos.nodes_searched();
       }
index 9adf1407bd5471436e360b79aa4af7eb30f4065a..6784effd3d6b546c64e4192f10984b651ec0b8d2 100644 (file)
@@ -325,11 +325,11 @@ namespace {
                 if (attack && attack != reference[i])
                     break;
 
-                assert(reference[i] != 0);
+                assert(reference[i]);
 
                 attack = reference[i];
             }
-        } while (i != size);
+        } while (i < size);
     }
   }
 }
index 98f6f3875fd613ea10b0d769444fc2291c19d6e1..dd0b7ff8a88b0b2c4250105ea10bf93dd3dcb9f4 100644 (file)
@@ -40,7 +40,7 @@ int main(int argc, char* argv[]) {
   Pawns::init();
   Eval::init();
   Threads.init();
-  TT.set_size(Options["Hash"]);
+  TT.resize(Options["Hash"]);
 
   std::string args;
 
index b650b96094a66d4a0fbaa0c4ce20a86b3d143e42..99c00ebe4397e5a47677289c01936715224ebf84 100644 (file)
@@ -144,10 +144,10 @@ std::ostream& operator<<(std::ostream& os, SyncCout sc) {
 
   static Mutex m;
 
-  if (sc == io_lock)
+  if (sc == IO_LOCK)
       m.lock();
 
-  if (sc == io_unlock)
+  if (sc == IO_UNLOCK)
       m.unlock();
 
   return os;
index d7addd845351f2a5f03fc9552ea6255c17bcb314..c4232a48ce3c653e5e17c9d16e58d133c5a4315f 100644 (file)
@@ -59,10 +59,10 @@ private:
 };
 
 
-enum SyncCout { io_lock, io_unlock };
+enum SyncCout { IO_LOCK, IO_UNLOCK };
 std::ostream& operator<<(std::ostream&, SyncCout);
 
-#define sync_cout std::cout << io_lock
-#define sync_endl std::endl << io_unlock
+#define sync_cout std::cout << IO_LOCK
+#define sync_endl std::endl << IO_UNLOCK
 
 #endif // #ifndef MISC_H_INCLUDED
index 8fc62c9894d614af730d9e2d405a9da991362d0d..5d3048ae5cfdf305c34ca78683de2ab6683d5b4b 100644 (file)
@@ -159,8 +159,8 @@ public:
   int game_ply() const;
   bool is_chess960() const;
   Thread* this_thread() const;
-  int64_t nodes_searched() const;
-  void set_nodes_searched(int64_t n);
+  uint64_t nodes_searched() const;
+  void set_nodes_searched(uint64_t n);
   bool is_draw() const;
 
   // Position consistency check, for debugging
@@ -201,7 +201,7 @@ private:
   Square castlingRookSquare[COLOR_NB][CASTLING_SIDE_NB];
   Bitboard castlingPath[COLOR_NB][CASTLING_SIDE_NB];
   StateInfo startState;
-  int64_t nodes;
+  uint64_t nodes;
   int gamePly;
   Color sideToMove;
   Thread* thisThread;
@@ -209,11 +209,11 @@ private:
   int chess960;
 };
 
-inline int64_t Position::nodes_searched() const {
+inline uint64_t Position::nodes_searched() const {
   return nodes;
 }
 
-inline void Position::set_nodes_searched(int64_t n) {
+inline void Position::set_nodes_searched(uint64_t n) {
   nodes = n;
 }
 
index 23bd614e5ced789619dcaa1023c84f7fa4556ce7..b9b82cef4fc5b1ad4b434f61d7225747bc7aa9b8 100644 (file)
@@ -45,15 +45,15 @@ class RKISS {
 
   uint64_t a, b, c, d;
 
-  uint64_t rotate(uint64_t x, uint64_t k) const {
+  uint64_t rotate_L(uint64_t x, unsigned k) const {
     return (x << k) | (x >> (64 - k));
   }
 
   uint64_t rand64() {
 
-    const uint64_t e = a - rotate(b,  7);
-    a = b ^ rotate(c, 13);
-    b = c + rotate(d, 37);
+    const uint64_t e = a - rotate_L(b,  7);
+    a = b ^ rotate_L(c, 13);
+    b = c + rotate_L(d, 37);
     c = d + e;
     return d = e + a;
   }
index 0b57c1fe3e7b35747a48bf659dd6d6f25365d8a0..097faf90f7c2fc4d15f5845335c306fa792ae99f 100644 (file)
@@ -154,10 +154,10 @@ void Search::init() {
 /// Search::perft() is our utility to verify move generation. All the leaf nodes
 /// up to the given depth are generated and counted and the sum returned.
 
-static size_t perft(Position& pos, Depth depth) {
+static uint64_t perft(Position& pos, Depth depth) {
 
   StateInfo st;
-  size_t cnt = 0;
+  uint64_t cnt = 0;
   CheckInfo ci(pos);
   const bool leaf = depth == 2 * ONE_PLY;
 
@@ -170,7 +170,7 @@ static size_t perft(Position& pos, Depth depth) {
   return cnt;
 }
 
-size_t Search::perft(Position& pos, Depth depth) {
+uint64_t Search::perft(Position& pos, Depth depth) {
   return depth > ONE_PLY ? ::perft(pos, depth) : MoveList<LEGAL>(pos).size();
 }
 
index 25e71a845c36c201b54fbd405e7fc2587d76a656..2fd8d47d9860efcea1ac414ca13ece6bf63b2e4c 100644 (file)
@@ -81,6 +81,7 @@ struct LimitsType {
   LimitsType() { std::memset(this, 0, sizeof(LimitsType)); }
   bool use_time_management() const { return !(mate | movetime | depth | nodes | infinite); }
 
+  std::vector<Move> searchmoves;
   int time[COLOR_NB], inc[COLOR_NB], movestogo, depth, nodes, movetime, mate, infinite, ponder;
 };
 
@@ -89,7 +90,7 @@ struct LimitsType {
 /// typically in an async fashion e.g. to stop the search by the GUI.
 
 struct SignalsType {
-  bool stopOnPonderhit, firstRootMove, stop, failedLowAtRoot;
+  bool stop, stopOnPonderhit, firstRootMove, failedLowAtRoot;
 };
 
 typedef std::auto_ptr<std::stack<StateInfo> > StateStackPtr;
@@ -103,7 +104,7 @@ extern Time::point SearchTime, IterationTime;
 extern StateStackPtr SetupStates;
 
 extern void init();
-extern size_t perft(Position& pos, Depth depth);
+extern uint64_t perft(Position& pos, Depth depth);
 extern void think();
 
 } // namespace Search
index fdb99e62ac5e5c3ea79e00af7b8fec038235b157..e5bcc64d43072193b613d162ec47151f1833051a 100644 (file)
@@ -29,6 +29,8 @@ using namespace Search;
 
 ThreadPool Threads; // Global object
 
+extern void check_time();
+
 namespace {
 
  // start_routine() is the C function which is called when a new thread
@@ -90,9 +92,43 @@ Thread::Thread() /* : splitPoints() */ { // Value-initialization bug in MSVC
 }
 
 
+// Thread::cutoff_occurred() checks whether a beta cutoff has occurred in the
+// current active split point, or in some ancestor of the split point.
+
+bool Thread::cutoff_occurred() const {
+
+  for (SplitPoint* sp = activeSplitPoint; sp; sp = sp->parentSplitPoint)
+      if (sp->cutoff)
+          return true;
+
+  return false;
+}
+
+
+// Thread::available_to() checks whether the thread is available to help the
+// thread 'master' at a split point. An obvious requirement is that thread must
+// be idle. With more than two threads, this is not sufficient: If the thread is
+// the master of some split point, it is only available as a slave to the slaves
+// which are busy searching the split point at the top of slave's split point
+// stack (the "helpful master concept" in YBWC terminology).
+
+bool Thread::available_to(const Thread* master) const {
+
+  if (searching)
+      return false;
+
+  // Make a local copy to be sure it doesn't become zero under our feet while
+  // testing next condition and so leading to an out of bounds access.
+  int size = splitPointsSize;
+
+  // No split points means that the thread is available as a slave for any
+  // other thread otherwise apply the "helpful master" concept if possible.
+  return !size || (splitPoints[size - 1].slavesMask & (1ULL << master->idx));
+}
+
+
 // TimerThread::idle_loop() is where the timer thread waits msec milliseconds
 // and then calls check_time(). If msec is 0 thread sleeps until it's woken up.
-extern void check_time();
 
 void TimerThread::idle_loop() {
 
@@ -144,41 +180,6 @@ void MainThread::idle_loop() {
 }
 
 
-// Thread::cutoff_occurred() checks whether a beta cutoff has occurred in the
-// current active split point, or in some ancestor of the split point.
-
-bool Thread::cutoff_occurred() const {
-
-  for (SplitPoint* sp = activeSplitPoint; sp; sp = sp->parentSplitPoint)
-      if (sp->cutoff)
-          return true;
-
-  return false;
-}
-
-
-// Thread::available_to() checks whether the thread is available to help the
-// thread 'master' at a split point. An obvious requirement is that thread must
-// be idle. With more than two threads, this is not sufficient: If the thread is
-// the master of some split point, it is only available as a slave to the slaves
-// which are busy searching the split point at the top of slave's split point
-// stack (the "helpful master concept" in YBWC terminology).
-
-bool Thread::available_to(const Thread* master) const {
-
-  if (searching)
-      return false;
-
-  // Make a local copy to be sure it doesn't become zero under our feet while
-  // testing next condition and so leading to an out of bounds access.
-  int size = splitPointsSize;
-
-  // No split points means that the thread is available as a slave for any
-  // other thread otherwise apply the "helpful master" concept if possible.
-  return !size || (splitPoints[size - 1].slavesMask & (1ULL << master->idx));
-}
-
-
 // init() is called at startup to create and launch requested threads, that will
 // go immediately to sleep due to 'sleepWhileIdle' set to true. We cannot use
 // a c'tor because Threads is a static object and we need a fully initialized
@@ -264,8 +265,7 @@ void Thread::split(Position& pos, const Stack* ss, Value alpha, Value beta, Valu
                    MovePicker* movePicker, int nodeType, bool cutNode) {
 
   assert(pos.pos_is_ok());
-  assert(*bestValue <= alpha && alpha < beta && beta <= VALUE_INFINITE);
-  assert(*bestValue > -VALUE_INFINITE);
+  assert(-VALUE_INFINITE < *bestValue && *bestValue <= alpha && alpha < beta && beta <= VALUE_INFINITE);
   assert(depth >= Threads.minimumSplitDepth);
   assert(searching);
   assert(splitPointsSize < MAX_SPLITPOINTS_PER_THREAD);
@@ -367,8 +367,8 @@ void ThreadPool::wait_for_think_finished() {
 // start_thinking() wakes up the main thread sleeping in MainThread::idle_loop()
 // so to start a new search, then returns immediately.
 
-void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits,
-                                const std::vector<Move>& searchMoves, StateStackPtr& states) {
+void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits, StateStackPtr& states) {
+
   wait_for_think_finished();
 
   SearchTime = Time::now(); // As early as possible
@@ -386,8 +386,8 @@ void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits,
   }
 
   for (MoveList<LEGAL> it(pos); *it; ++it)
-      if (   searchMoves.empty()
-          || std::count(searchMoves.begin(), searchMoves.end(), *it))
+      if (   limits.searchmoves.empty()
+          || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), *it))
           RootMoves.push_back(RootMove(*it));
 
   main()->thinking = true;
index c5690a02634885f5abdf69171660317a45721bbf..c9152b229243620bb00b451f85f91e4c7e9b85e6 100644 (file)
@@ -76,7 +76,7 @@ struct SplitPoint {
   // Shared data
   Mutex mutex;
   volatile uint64_t slavesMask;
-  volatile int64_t nodes;
+  volatile uint64_t nodes;
   volatile Value alpha;
   volatile Value bestValue;
   volatile Move bestMove;
@@ -162,8 +162,7 @@ struct ThreadPool : public std::vector<Thread*> {
   void read_uci_options();
   Thread* available_slave(const Thread* master) const;
   void wait_for_think_finished();
-  void start_thinking(const Position&, const Search::LimitsType&,
-                      const std::vector<Move>&, Search::StateStackPtr&);
+  void start_thinking(const Position&, const Search::LimitsType&, Search::StateStackPtr&);
 
   bool sleepWhileIdle;
   Depth minimumSplitDepth;
index 24e94c8008bd691c4045719f065359dd1ff3ee5a..04673901545381a0f00956ed571a10d7023eee66 100644 (file)
 TranspositionTable TT; // Our global transposition table
 
 
-/// TranspositionTable::set_size() sets the size of the transposition table,
+/// TranspositionTable::resize() sets the size of the transposition table,
 /// measured in megabytes. Transposition table consists of a power of 2 number
 /// of clusters and each cluster consists of ClusterSize number of TTEntry.
 
-void TranspositionTable::set_size(uint64_t mbSize) {
+void TranspositionTable::resize(uint64_t mbSize) {
 
   assert(msb((mbSize << 20) / sizeof(TTEntry)) < 32);
 
index a926d4ada6887b9d63ae9a77a91ff787d00b1cdd..9e8c0481eb0cc7055c715a12b8063586720220d6 100644 (file)
--- a/src/tt.h
+++ b/src/tt.h
@@ -81,7 +81,7 @@ public:
   const TTEntry* probe(const Key key) const;
   TTEntry* first_entry(const Key key) const;
   void refresh(const TTEntry* tte) const;
-  void set_size(uint64_t mbSize);
+  void resize(uint64_t mbSize);
   void clear();
   void store(const Key key, Value v, Bound type, Depth d, Move m, Value statV);
 
index 9f9d84cca96a680717bf17c89b091de67b0a04c6..9b7ab033c12ca5ab21fdc7e7d79829401253cbea 100644 (file)
@@ -194,14 +194,13 @@ namespace {
   void go(const Position& pos, istringstream& is) {
 
     Search::LimitsType limits;
-    vector<Move> searchMoves;
     string token;
 
     while (is >> token)
     {
         if (token == "searchmoves")
             while (is >> token)
-                searchMoves.push_back(move_from_uci(pos, token));
+                limits.searchmoves.push_back(move_from_uci(pos, token));
 
         else if (token == "wtime")     is >> limits.time[WHITE];
         else if (token == "btime")     is >> limits.time[BLACK];
@@ -216,6 +215,6 @@ namespace {
         else if (token == "ponder")    limits.ponder = true;
     }
 
-    Threads.start_thinking(pos, limits, searchMoves, SetupStates);
+    Threads.start_thinking(pos, limits, SetupStates);
   }
 }
index 417765a54b6c3a487803df7481752cf286771876..22a335f5883e519ddb095d4e247cf0a701d03005 100644 (file)
@@ -38,7 +38,7 @@ namespace UCI {
 void on_logger(const Option& o) { start_logger(o); }
 void on_eval(const Option&) { Eval::init(); }
 void on_threads(const Option&) { Threads.read_uci_options(); }
-void on_hash_size(const Option& o) { TT.set_size(o); }
+void on_hash_size(const Option& o) { TT.resize(o); }
 void on_clear_hash(const Option&) { TT.clear(); }
 
 
@@ -115,16 +115,16 @@ std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
 
 /// Option class constructors and conversion operators
 
-Option::Option(const char* v, Fn* f) : type("string"), min(0), max(0), idx(Options.size()), on_change(f)
+Option::Option(const char* v, OnChange f) : type("string"), min(0), max(0), idx(Options.size()), on_change(f)
 { defaultValue = currentValue = v; }
 
-Option::Option(bool v, Fn* f) : type("check"), min(0), max(0), idx(Options.size()), on_change(f)
+Option::Option(bool v, OnChange f) : type("check"), min(0), max(0), idx(Options.size()), on_change(f)
 { defaultValue = currentValue = (v ? "true" : "false"); }
 
-Option::Option(Fn* f) : type("button"), min(0), max(0), idx(Options.size()), on_change(f)
+Option::Option(OnChange f) : type("button"), min(0), max(0), idx(Options.size()), on_change(f)
 {}
 
-Option::Option(int v, int minv, int maxv, Fn* f) : type("spin"), min(minv), max(maxv), idx(Options.size()), on_change(f)
+Option::Option(int v, int minv, int maxv, OnChange f) : type("spin"), min(minv), max(maxv), idx(Options.size()), on_change(f)
 { std::ostringstream ss; ss << v; defaultValue = currentValue = ss.str(); }
 
 
@@ -156,7 +156,7 @@ Option& Option::operator=(const string& v) {
       currentValue = v;
 
   if (on_change)
-      (*on_change)(*this);
+      on_change(*this);
 
   return *this;
 }
index 5f04184da0584b46dddc531f732c85b6c51a0b49..2eb937a8b7714eda0b2fcf250373afe9edfac046 100644 (file)
@@ -38,13 +38,13 @@ typedef std::map<std::string, Option, CaseInsensitiveLess> OptionsMap;
 /// Option class implements an option as defined by UCI protocol
 class Option {
 
-  typedef void (Fn)(const Option&);
+  typedef void (*OnChange)(const Option&);
 
 public:
-  Option(Fn* = NULL);
-  Option(bool v, Fn* = NULL);
-  Option(const char* v, Fn* = NULL);
-  Option(int v, int min, int max, Fn* = NULL);
+  Option(OnChange = NULL);
+  Option(bool v, OnChange = NULL);
+  Option(const char* v, OnChange = NULL);
+  Option(int v, int min, int max, OnChange = NULL);
 
   Option& operator=(const std::string& v);
   operator int() const;
@@ -56,7 +56,7 @@ private:
   std::string defaultValue, currentValue, type;
   int min, max;
   size_t idx;
-  Fn* on_change;
+  OnChange on_change;
 };
 
 void init(OptionsMap&);