]> git.sesse.net Git - stockfish/commitdiff
Use calloc() in TranspositionTable::set_size()
authorMarco Costalba <mcostalba@gmail.com>
Sat, 29 Jun 2013 08:23:43 +0000 (10:23 +0200)
committerMarco Costalba <mcostalba@gmail.com>
Sat, 29 Jun 2013 09:23:07 +0000 (11:23 +0200)
Function calloc() already initializes memory to
zero, so avoid calling clear() afterwards.

Also some renaming while there (inspired by DiscoCheck).

No functional change.

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

index f4f411c56d18f266f110d69ba5d4aa0b8c9707ad..15981755b432fd3329760ba6af7b01065b5fd33f 100644 (file)
@@ -570,9 +570,9 @@ namespace {
         && tte
         && tte->depth() >= depth
         && ttValue != VALUE_NONE // Only in case of TT access race
         && tte
         && tte->depth() >= depth
         && ttValue != VALUE_NONE // Only in case of TT access race
-        && (           PvNode ?  tte->type() == BOUND_EXACT
-            : ttValue >= beta ? (tte->type() & BOUND_LOWER)
-                              : (tte->type() & BOUND_UPPER)))
+        && (           PvNode ?  tte->bound() == BOUND_EXACT
+            : ttValue >= beta ? (tte->bound() &  BOUND_LOWER)
+                              : (tte->bound() &  BOUND_UPPER)))
     {
         TT.refresh(tte);
         ss->currentMove = ttMove; // Can be MOVE_NONE
     {
         TT.refresh(tte);
         ss->currentMove = ttMove; // Can be MOVE_NONE
@@ -601,8 +601,8 @@ namespace {
 
         // Can ttValue be used as a better position evaluation?
         if (ttValue != VALUE_NONE)
 
         // Can ttValue be used as a better position evaluation?
         if (ttValue != VALUE_NONE)
-            if (   ((tte->type() & BOUND_LOWER) && ttValue > eval)
-                || ((tte->type() & BOUND_UPPER) && ttValue < eval))
+            if (   ((tte->bound() & BOUND_LOWER) && ttValue > eval)
+                || ((tte->bound() & BOUND_UPPER) && ttValue < eval))
                 eval = ttValue;
     }
     else
                 eval = ttValue;
     }
     else
@@ -775,7 +775,7 @@ split_point_start: // At split points actual search starts from here
                            &&  depth >= (PvNode ? 6 * ONE_PLY : 8 * ONE_PLY)
                            &&  ttMove != MOVE_NONE
                            && !excludedMove // Recursive singular search is not allowed
                            &&  depth >= (PvNode ? 6 * ONE_PLY : 8 * ONE_PLY)
                            &&  ttMove != MOVE_NONE
                            && !excludedMove // Recursive singular search is not allowed
-                           && (tte->type() & BOUND_LOWER)
+                           && (tte->bound() & BOUND_LOWER)
                            &&  tte->depth() >= depth - 3 * ONE_PLY;
 
     // Step 11. Loop through moves
                            &&  tte->depth() >= depth - 3 * ONE_PLY;
 
     // Step 11. Loop through moves
@@ -1172,9 +1172,9 @@ split_point_start: // At split points actual search starts from here
     if (   tte
         && tte->depth() >= ttDepth
         && ttValue != VALUE_NONE // Only in case of TT access race
     if (   tte
         && tte->depth() >= ttDepth
         && ttValue != VALUE_NONE // Only in case of TT access race
-        && (           PvNode ?  tte->type() == BOUND_EXACT
-            : ttValue >= beta ? (tte->type() & BOUND_LOWER)
-                              : (tte->type() & BOUND_UPPER)))
+        && (           PvNode ?  tte->bound() == BOUND_EXACT
+            : ttValue >= beta ? (tte->bound() &  BOUND_LOWER)
+                              : (tte->bound() &  BOUND_UPPER)))
     {
         ss->currentMove = ttMove; // Can be MOVE_NONE
         return ttValue;
     {
         ss->currentMove = ttMove; // Can be MOVE_NONE
         return ttValue;
@@ -1584,7 +1584,7 @@ split_point_start: // At split points actual search starts from here
 void RootMove::extract_pv_from_tt(Position& pos) {
 
   StateInfo state[MAX_PLY_PLUS_2], *st = state;
 void RootMove::extract_pv_from_tt(Position& pos) {
 
   StateInfo state[MAX_PLY_PLUS_2], *st = state;
-  TTEntry* tte;
+  const TTEntry* tte;
   int ply = 0;
   Move m = pv[0];
 
   int ply = 0;
   Move m = pv[0];
 
@@ -1617,7 +1617,7 @@ void RootMove::extract_pv_from_tt(Position& pos) {
 void RootMove::insert_pv_in_tt(Position& pos) {
 
   StateInfo state[MAX_PLY_PLUS_2], *st = state;
 void RootMove::insert_pv_in_tt(Position& pos) {
 
   StateInfo state[MAX_PLY_PLUS_2], *st = state;
-  TTEntry* tte;
+  const TTEntry* tte;
   int ply = 0;
 
   do {
   int ply = 0;
 
   do {
index c6b294a00cad8fd5aeb1eae1bf673b53b3f4026a..0d85d03006b26bd1cedc54c881b0f890dc2a252f 100644 (file)
@@ -41,7 +41,7 @@ void TranspositionTable::set_size(size_t mbSize) {
 
   hashMask = size - ClusterSize;
   free(mem);
 
   hashMask = size - ClusterSize;
   free(mem);
-  mem = malloc(size * sizeof(TTEntry) + CACHE_LINE_SIZE - 1);
+  mem = calloc(size * sizeof(TTEntry) + CACHE_LINE_SIZE - 1, 1);
 
   if (!mem)
   {
 
   if (!mem)
   {
@@ -51,7 +51,6 @@ void TranspositionTable::set_size(size_t mbSize) {
   }
 
   table = (TTEntry*)((uintptr_t(mem) + CACHE_LINE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1));
   }
 
   table = (TTEntry*)((uintptr_t(mem) + CACHE_LINE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1));
-  clear(); // Operator new is not guaranteed to initialize memory to zero
 }
 
 
 }
 
 
@@ -65,6 +64,23 @@ void TranspositionTable::clear() {
 }
 
 
 }
 
 
+/// TranspositionTable::probe() looks up the current position in the
+/// transposition table. Returns a pointer to the TTEntry or NULL if
+/// position is not found.
+
+const TTEntry* TranspositionTable::probe(const Key key) const {
+
+  const TTEntry* tte = first_entry(key);
+  uint32_t key32 = key >> 32;
+
+  for (unsigned i = 0; i < ClusterSize; i++, tte++)
+      if (tte->key() == key32)
+          return tte;
+
+  return NULL;
+}
+
+
 /// TranspositionTable::store() writes a new entry containing position key and
 /// valuable information of current position. The lowest order bits of position
 /// key are used to decide on which cluster the position will be placed.
 /// TranspositionTable::store() writes a new entry containing position key and
 /// valuable information of current position. The lowest order bits of position
 /// key are used to decide on which cluster the position will be placed.
@@ -85,38 +101,21 @@ void TranspositionTable::store(const Key key, Value v, Bound t, Depth d, Move m,
   {
       if (!tte->key() || tte->key() == key32) // Empty or overwrite old
       {
   {
       if (!tte->key() || tte->key() == key32) // Empty or overwrite old
       {
-          // Preserve any existing ttMove
-          if (m == MOVE_NONE)
-              m = tte->move();
+          if (!m)
+              m = tte->move(); // Preserve any existing ttMove
 
 
-          tte->save(key32, v, t, d, m, generation, statV, kingD);
-          return;
+          replace = tte;
+          break;
       }
 
       // Implement replace strategy
       c1 = (replace->generation() == generation ?  2 : 0);
       }
 
       // Implement replace strategy
       c1 = (replace->generation() == generation ?  2 : 0);
-      c2 = (tte->generation() == generation || tte->type() == BOUND_EXACT ? -2 : 0);
+      c2 = (tte->generation() == generation || tte->bound() == BOUND_EXACT ? -2 : 0);
       c3 = (tte->depth() < replace->depth() ?  1 : 0);
 
       if (c1 + c2 + c3 > 0)
           replace = tte;
   }
       c3 = (tte->depth() < replace->depth() ?  1 : 0);
 
       if (c1 + c2 + c3 > 0)
           replace = tte;
   }
-  replace->save(key32, v, t, d, m, generation, statV, kingD);
-}
-
-
-/// TranspositionTable::probe() looks up the current position in the
-/// transposition table. Returns a pointer to the TTEntry or NULL if
-/// position is not found.
-
-TTEntry* TranspositionTable::probe(const Key key) const {
 
 
-  TTEntry* tte = first_entry(key);
-  uint32_t key32 = key >> 32;
-
-  for (unsigned i = 0; i < ClusterSize; i++, tte++)
-      if (tte->key() == key32)
-          return tte;
-
-  return NULL;
+  replace->save(key32, v, t, d, m, generation, statV, kingD);
 }
 }
index 5f34957c95e5e4414ecc8e7490f9ed249815ef53..aee530b05485d6149ec3441e44db51cdcfbf88d7 100644 (file)
--- a/src/tt.h
+++ b/src/tt.h
 /// static value: 16 bit
 /// static margin: 16 bit
 
 /// static value: 16 bit
 /// static margin: 16 bit
 
-class TTEntry {
+struct TTEntry {
 
 
-public:
   void save(uint32_t k, Value v, Bound b, Depth d, Move m, int g, Value ev, Value em) {
 
     key32        = (uint32_t)k;
     move16       = (uint16_t)m;
   void save(uint32_t k, Value v, Bound b, Depth d, Move m, int g, Value ev, Value em) {
 
     key32        = (uint32_t)k;
     move16       = (uint16_t)m;
-    bound        = (uint8_t)b;
+    bound8       = (uint8_t)b;
     generation8  = (uint8_t)g;
     value16      = (int16_t)v;
     depth16      = (int16_t)d;
     evalValue    = (int16_t)ev;
     evalMargin   = (int16_t)em;
   }
     generation8  = (uint8_t)g;
     value16      = (int16_t)v;
     depth16      = (int16_t)d;
     evalValue    = (int16_t)ev;
     evalMargin   = (int16_t)em;
   }
-  void set_generation(int g) { generation8 = (uint8_t)g; }
+  void set_generation(uint8_t g) { generation8 = g; }
 
   uint32_t key() const      { return key32; }
   Depth depth() const       { return (Depth)depth16; }
   Move move() const         { return (Move)move16; }
   Value value() const       { return (Value)value16; }
 
   uint32_t key() const      { return key32; }
   Depth depth() const       { return (Depth)depth16; }
   Move move() const         { return (Move)move16; }
   Value value() const       { return (Value)value16; }
-  Bound type() const        { return (Bound)bound; }
+  Bound bound() const       { return (Bound)bound8; }
   int generation() const    { return (int)generation8; }
   Value eval_value() const  { return (Value)evalValue; }
   Value eval_margin() const { return (Value)evalMargin; }
   int generation() const    { return (int)generation8; }
   Value eval_value() const  { return (Value)evalValue; }
   Value eval_margin() const { return (Value)evalMargin; }
@@ -62,7 +61,7 @@ public:
 private:
   uint32_t key32;
   uint16_t move16;
 private:
   uint32_t key32;
   uint16_t move16;
-  uint8_t bound, generation8;
+  uint8_t bound8, generation8;
   int16_t value16, depth16, evalValue, evalMargin;
 };
 
   int16_t value16, depth16, evalValue, evalMargin;
 };
 
@@ -81,7 +80,7 @@ public:
  ~TranspositionTable() { free(mem); }
   void new_search() { generation++; }
 
  ~TranspositionTable() { free(mem); }
   void new_search() { generation++; }
 
-  TTEntry* probe(const Key key) const;
+  const TTEntry* probe(const Key key) const;
   TTEntry* first_entry(const Key key) const;
   void refresh(const TTEntry* tte) const;
   void set_size(size_t mbSize);
   TTEntry* first_entry(const Key key) const;
   void refresh(const TTEntry* tte) const;
   void set_size(size_t mbSize);
@@ -92,7 +91,7 @@ private:
   uint32_t hashMask;
   TTEntry* table;
   void* mem;
   uint32_t hashMask;
   TTEntry* table;
   void* mem;
-  uint8_t generation; // Size must be not bigger then TTEntry::generation8
+  uint8_t generation; // Size must be not bigger than TTEntry::generation8
 };
 
 extern TranspositionTable TT;
 };
 
 extern TranspositionTable TT;