]> git.sesse.net Git - stockfish/commitdiff
Coding style in TT code
authorMarco Costalba <mcostalba@gmail.com>
Sat, 13 Dec 2014 08:27:39 +0000 (09:27 +0100)
committerJoona Kiiski <joona.kiiski@gmail.com>
Sun, 14 Dec 2014 23:49:00 +0000 (23:49 +0000)
In particular seems more natural to return
bool and TTEntry on the same line, actually
we should pass and return them as a pair,
but due to limitations of C++ and not wanting
to use std::pair this can be an acceptable
compromise.

No functional change.

Resolves #157

src/search.cpp
src/tt.cpp
src/tt.h

index c774f08ab5e862013355b6bd82e0ee7998e498bf..272634d14a8154590065feb5028347564dafa362 100644 (file)
@@ -458,13 +458,12 @@ namespace {
     Move pv[MAX_PLY+1], quietsSearched[64];
     StateInfo st;
     TTEntry* tte;
-    bool ttHit;
     SplitPoint* splitPoint;
     Key posKey;
     Move ttMove, move, excludedMove, bestMove;
     Depth extension, newDepth, predictedDepth;
     Value bestValue, value, ttValue, eval, nullValue, futilityValue;
-    bool inCheck, givesCheck, singularExtensionNode, improving;
+    bool ttHit, inCheck, givesCheck, singularExtensionNode, improving;
     bool captureOrPromotion, dangerous, doFullDepthSearch;
     int moveCount, quietCount;
 
@@ -568,7 +567,7 @@ namespace {
 
                 tte->save(posKey, value_to_tt(value, ss->ply), BOUND_EXACT,
                           std::min(DEPTH_MAX - ONE_PLY, depth + 6 * ONE_PLY),
-                          MOVE_NONE, VALUE_NONE, TT.get_generation());
+                          MOVE_NONE, VALUE_NONE, TT.generation());
 
                 return value;
             }
@@ -598,7 +597,7 @@ namespace {
         eval = ss->staticEval =
         (ss-1)->currentMove != MOVE_NULL ? evaluate(pos) : -(ss-1)->staticEval + 2 * Eval::Tempo;
 
-        tte->save(posKey, VALUE_NONE, BOUND_NONE, DEPTH_NONE, MOVE_NONE, ss->staticEval, TT.get_generation());
+        tte->save(posKey, VALUE_NONE, BOUND_NONE, DEPTH_NONE, MOVE_NONE, ss->staticEval, TT.generation());
     }
 
     if (ss->skipEarlyPruning)
@@ -1084,7 +1083,7 @@ moves_loop: // When in check and at SpNode search starts from here
     tte->save(posKey, value_to_tt(bestValue, ss->ply),
               bestValue >= beta ? BOUND_LOWER :
               PvNode && bestMove ? BOUND_EXACT : BOUND_UPPER,
-              depth, bestMove, ss->staticEval, TT.get_generation());
+              depth, bestMove, ss->staticEval, TT.generation());
 
     assert(bestValue > -VALUE_INFINITE && bestValue < VALUE_INFINITE);
 
@@ -1110,11 +1109,10 @@ moves_loop: // When in check and at SpNode search starts from here
     Move pv[MAX_PLY+1];
     StateInfo st;
     TTEntry* tte;
-    bool ttHit;
     Key posKey;
     Move ttMove, move, bestMove;
     Value bestValue, value, ttValue, futilityValue, futilityBase, oldAlpha;
-    bool givesCheck, evasionPrunable;
+    bool ttHit, givesCheck, evasionPrunable;
     Depth ttDepth;
 
     if (PvNode)
@@ -1184,7 +1182,7 @@ moves_loop: // When in check and at SpNode search starts from here
         {
             if (!ttHit)
                 tte->save(pos.key(), value_to_tt(bestValue, ss->ply), BOUND_LOWER,
-                          DEPTH_NONE, MOVE_NONE, ss->staticEval, TT.get_generation());
+                          DEPTH_NONE, MOVE_NONE, ss->staticEval, TT.generation());
 
             return bestValue;
         }
@@ -1283,7 +1281,7 @@ moves_loop: // When in check and at SpNode search starts from here
               else // Fail high
               {
                   tte->save(posKey, value_to_tt(value, ss->ply), BOUND_LOWER,
-                            ttDepth, move, ss->staticEval, TT.get_generation());
+                            ttDepth, move, ss->staticEval, TT.generation());
 
                   return value;
               }
@@ -1298,7 +1296,7 @@ moves_loop: // When in check and at SpNode search starts from here
 
     tte->save(posKey, value_to_tt(bestValue, ss->ply),
               PvNode && bestValue > oldAlpha ? BOUND_EXACT : BOUND_UPPER,
-              ttDepth, bestMove, ss->staticEval, TT.get_generation());
+              ttDepth, bestMove, ss->staticEval, TT.generation());
 
     assert(bestValue > -VALUE_INFINITE && bestValue < VALUE_INFINITE);
 
@@ -1480,7 +1478,7 @@ void RootMove::insert_pv_in_tt(Position& pos) {
       TTEntry* tte = TT.probe(pos.key(), ttHit);
 
       if (!ttHit || tte->move() != pv[idx]) // Don't overwrite correct entries
-          tte->save(pos.key(), VALUE_NONE, BOUND_NONE, DEPTH_NONE, pv[idx], VALUE_NONE, TT.get_generation());
+          tte->save(pos.key(), VALUE_NONE, BOUND_NONE, DEPTH_NONE, pv[idx], VALUE_NONE, TT.generation());
 
       assert(MoveList<LEGAL>(pos).contains(pv[idx]));
 
index 2979c104aac3020c3798996597b182174b03ad4b..19f21de22f21ee12f077538f4a812f46ef40a64c 100644 (file)
@@ -63,12 +63,12 @@ void TranspositionTable::clear() {
 }
 
 
-/// TranspositionTable::probe() looks up the current position in the
-/// transposition table. It returns true and a pointer to the TTEntry if
-/// the position is found. Otherwise, it returns false and a pointer to an empty or
-/// least valuable TTEntry to be replaced later. A TTEntry t1 is considered
-/// to be more valuable than a TTEntry t2 if t1 is from the current search and t2
-/// is from a previous search, or if the depth of t1 is bigger than the depth of t2.
+/// TranspositionTable::probe() looks up the current position in the transposition
+/// table. It returns true and a pointer to the TTEntry if the position is found.
+/// Otherwise, it returns false and a pointer to an empty or least valuable TTEntry
+/// to be replaced later. A TTEntry t1 is considered to be more valuable than a
+/// TTEntry t2 if t1 is from the current search and t2 is from a previous search,
+/// or if the depth of t1 is bigger than the depth of t2.
 
 TTEntry* TranspositionTable::probe(const Key key, bool& found) const {
 
@@ -79,20 +79,18 @@ TTEntry* TranspositionTable::probe(const Key key, bool& found) const {
       if (!tte[i].key16 || tte[i].key16 == key16)
       {
           if (tte[i].key16)
-              tte[i].genBound8 = uint8_t(generation | tte[i].bound()); // Refresh
+              tte[i].genBound8 = uint8_t(generation8 | tte[i].bound()); // Refresh
 
-          found = (bool)tte[i].key16;
-          return &tte[i];
+          return found = (bool)tte[i].key16, &tte[i];
       }
 
   // Find an entry to be replaced according to the replacement strategy
   TTEntry* replace = tte;
   for (unsigned i = 1; i < TTClusterSize; ++i)
-      if (  ((  tte[i].genBound8 & 0xFC) == generation || tte[i].bound() == BOUND_EXACT)
-          - ((replace->genBound8 & 0xFC) == generation)
+      if (  ((  tte[i].genBound8 & 0xFC) == generation8 || tte[i].bound() == BOUND_EXACT)
+          - ((replace->genBound8 & 0xFC) == generation8)
           - (tte[i].depth8 < replace->depth8) < 0)
           replace = &tte[i];
 
-  found = false;
-  return replace;
+  return found = false, replace;
 }
index 0c324c73f21151455ecd6583ef5478a1fe0e56f0..89c95d550468e59f8aec727948193ebd7d5e7865 100644 (file)
--- a/src/tt.h
+++ b/src/tt.h
@@ -43,10 +43,10 @@ struct TTEntry {
 
   void save(Key k, Value v, Bound b, Depth d, Move m, Value ev, uint8_t g) {
 
-    k >>= 48;
-    if (m || k != key16) // preserve any existing ttMove
+    if (m || (k >> 48) != key16) // Preserve any existing move for the same position
         move16 = (uint16_t)m;
-    key16      = (uint16_t)k;
+
+    key16      = (uint16_t)(k >> 48);
     value16    = (int16_t)v;
     evalValue  = (int16_t)ev;
     genBound8  = (uint8_t)(g | b);
@@ -86,8 +86,8 @@ class TranspositionTable {
 
 public:
  ~TranspositionTable() { free(mem); }
-  void new_search() { generation += 4; } // Lower 2 bits are used by Bound
-  uint8_t get_generation() const { return generation; }
+  void new_search() { generation8 += 4; } // Lower 2 bits are used by Bound
+  uint8_t generation() const { return generation8; }
   TTEntry* probe(const Key key, bool& found) const;
   TTEntry* first_entry(const Key key) const;
   void resize(size_t mbSize);
@@ -97,7 +97,7 @@ private:
   size_t clusterCount;
   TTCluster* table;
   void* mem;
-  uint8_t generation; // Size must be not bigger than TTEntry::genBound8
+  uint8_t generation8; // Size must be not bigger than TTEntry::genBound8
 };
 
 extern TranspositionTable TT;