]> git.sesse.net Git - stockfish/commitdiff
Code style in tt.cpp
authorMarco Costalba <mcostalba@gmail.com>
Fri, 15 Apr 2011 16:14:12 +0000 (18:14 +0200)
committerMarco Costalba <mcostalba@gmail.com>
Sat, 16 Apr 2011 09:49:35 +0000 (10:49 +0100)
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
src/tt.cpp
src/tt.h

index f598e3bf1318d4a57e7368d5e9b18a284f86e56b..d18b512cd429f8a161c583de962e1a68ef4964b3 100644 (file)
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-
-////
-//// Includes
-////
-
 #include <cassert>
 #include <cstring>
 #include <iostream>
 
 #include "tt.h"
 
 #include <cassert>
 #include <cstring>
 #include <iostream>
 
 #include "tt.h"
 
-// The main transposition table
-TranspositionTable TT;
-
-////
-//// Functions
-////
+TranspositionTable TT; // Our global transposition table
 
 TranspositionTable::TranspositionTable() {
 
 
 TranspositionTable::TranspositionTable() {
 
-  size = 0;
-  entries = 0;
-  generation = 0;
+  size = generation = 0;
+  entries = NULL;
 }
 
 TranspositionTable::~TranspositionTable() {
 }
 
 TranspositionTable::~TranspositionTable() {
@@ -48,7 +37,7 @@ TranspositionTable::~TranspositionTable() {
 }
 
 
 }
 
 
-/// TranspositionTable::set_size sets the size of the transposition table,
+/// TranspositionTable::set_size() sets the size of the transposition table,
 /// measured in megabytes.
 
 void TranspositionTable::set_size(size_t mbSize) {
 /// measured in megabytes.
 
 void TranspositionTable::set_size(size_t mbSize) {
@@ -78,10 +67,9 @@ void TranspositionTable::set_size(size_t mbSize) {
 }
 
 
 }
 
 
-/// TranspositionTable::clear overwrites the entire transposition table
+/// TranspositionTable::clear() overwrites the entire transposition table
 /// with zeroes. It is called whenever the table is resized, or when the
 /// user asks the program to clear the table (from the UCI interface).
 /// with zeroes. It is called whenever the table is resized, or when the
 /// user asks the program to clear the table (from the UCI interface).
-/// Perhaps we should also clear it when the "ucinewgame" command is received?
 
 void TranspositionTable::clear() {
 
 
 void TranspositionTable::clear() {
 
@@ -89,23 +77,22 @@ void TranspositionTable::clear() {
 }
 
 
 }
 
 
-/// 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.
 /// When a new entry is written and there are no empty entries available in cluster,
 /// When a new entry is written and there are no empty entries available in cluster,
-/// it replaces the least valuable of entries.
-/// 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.
+/// it replaces the least valuable of entries. 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.
 
 void TranspositionTable::store(const Key posKey, Value v, ValueType t, Depth d, Move m, Value statV, Value kingD) {
 
   int c1, c2, c3;
   TTEntry *tte, *replace;
 
 void TranspositionTable::store(const Key posKey, Value v, ValueType t, Depth d, Move m, Value statV, Value kingD) {
 
   int c1, c2, c3;
   TTEntry *tte, *replace;
-  uint32_t posKey32 = posKey >> 32; // Use the high 32 bits as key
+  uint32_t posKey32 = posKey >> 32; // Use the high 32 bits as key inside the cluster
 
   tte = replace = first_entry(posKey);
 
   tte = replace = first_entry(posKey);
+
   for (int i = 0; i < ClusterSize; i++, tte++)
   {
       if (!tte->key() || tte->key() == posKey32) // Empty or overwrite old
   for (int i = 0; i < ClusterSize; i++, tte++)
   {
       if (!tte->key() || tte->key() == posKey32) // Empty or overwrite old
@@ -118,10 +105,7 @@ void TranspositionTable::store(const Key posKey, Value v, ValueType t, Depth d,
           return;
       }
 
           return;
       }
 
-      // Replacing first entry is default and already set before entering for-loop
-      if (i == 0)
-          continue;
-
+      // Implement replace strategy
       c1 = (replace->generation() == generation ?  2 : 0);
       c2 = (tte->generation() == generation ? -2 : 0);
       c3 = (tte->depth() < replace->depth() ?  1 : 0);
       c1 = (replace->generation() == generation ?  2 : 0);
       c2 = (tte->generation() == generation ? -2 : 0);
       c3 = (tte->depth() < replace->depth() ?  1 : 0);
@@ -133,9 +117,9 @@ void TranspositionTable::store(const Key posKey, Value v, ValueType t, Depth d,
 }
 
 
 }
 
 
-/// TranspositionTable::retrieve looks up the current position in the
-/// transposition table. Returns a pointer to the TTEntry or NULL
-/// if position is not found.
+/// TranspositionTable::retrieve() looks up the current position in the
+/// transposition table. Returns a pointer to the TTEntry or NULL if
+/// position is not found.
 
 TTEntry* TranspositionTable::retrieve(const Key posKey) const {
 
 
 TTEntry* TranspositionTable::retrieve(const Key posKey) const {
 
index fe4cee62ae39a83ec94ad3d232289daa94a7d395..daa66c333e3488c8428a406680e9f73438a58f10 100644 (file)
--- a/src/tt.h
+++ b/src/tt.h
 #include "move.h"
 #include "types.h"
 
 #include "move.h"
 #include "types.h"
 
-/// A simple fixed size hash table used to store pawns and material
-/// configurations. It is basically just an array of Entry objects.
-/// Without cluster concept or overwrite policy.
-
-template<class Entry, int HashSize>
-class SimpleHash {
-
-  SimpleHash(const SimpleHash&);
-  SimpleHash& operator=(const SimpleHash&);
 
 
-public:
-  SimpleHash() {
-
-    entries = new (std::nothrow) Entry[HashSize];
-    if (!entries)
-    {
-        std::cerr << "Failed to allocate " << HashSize * sizeof(Entry)
-                  << " bytes for material hash table." << std::endl;
-        exit(EXIT_FAILURE);
-    }
-    memset(entries, 0, HashSize * sizeof(Entry));
-  }
-
-  virtual ~SimpleHash() { delete [] entries; }
-
-  Entry* find(Key key) const { return entries + ((uint32_t)key & (HashSize - 1)); }
-  void prefetch(Key key) const { ::prefetch((char*)find(key)); }
-
-protected:
-  Entry* entries;
-};
-
-
-/// The TTEntry class is the class of transposition table entries
+/// The TTEntry is the class of transposition table entries
 ///
 /// A TTEntry needs 128 bits to be stored
 ///
 ///
 /// A TTEntry needs 128 bits to be stored
 ///
@@ -92,42 +60,38 @@ public:
   }
   void set_generation(int g) { generation8 = (uint8_t)g; }
 
   }
   void set_generation(int g) { generation8 = (uint8_t)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); }
-  ValueType type() const { return ValueType(valueType); }
-  int generation() const { return generation8; }
-  Value static_value() const { return Value(staticValue); }
-  Value static_value_margin() const { return Value(staticMargin); }
+  uint32_t key() const              { return key32; }
+  Depth depth() const               { return (Depth)depth16; }
+  Move move() const                 { return (Move)move16; }
+  Value value() const               { return (Value)value16; }
+  ValueType type() const            { return (ValueType)valueType; }
+  int generation() const            { return (int)generation8; }
+  Value static_value() const        { return (Value)staticValue; }
+  Value static_value_margin() const { return (Value)staticMargin; }
 
 private:
   uint32_t key32;
   uint16_t move16;
 
 private:
   uint32_t key32;
   uint16_t move16;
-  uint8_t valueType;
-  uint8_t generation8;
-  int16_t value16;
-  int16_t depth16;
-  int16_t staticValue;
-  int16_t staticMargin;
+  uint8_t valueType, generation8;
+  int16_t value16, depth16, staticValue, staticMargin;
 };
 
 
 /// This is the number of TTEntry slots for each cluster
 const int ClusterSize = 4;
 
 };
 
 
 /// This is the number of TTEntry slots for each cluster
 const int ClusterSize = 4;
 
-/// TTCluster consists of ClusterSize number of TTEntries.
-/// Size of TTCluster must not be bigger than a cache line size.
-/// In case it is less, it should be padded to guarantee always aligned accesses.
+
+/// TTCluster consists of ClusterSize number of TTEntries. Size of TTCluster
+/// must not be bigger than a cache line size. In case it is less, it should
+/// be padded to guarantee always aligned accesses.
 
 struct TTCluster {
   TTEntry data[ClusterSize];
 };
 
 
 
 struct TTCluster {
   TTEntry data[ClusterSize];
 };
 
 
-/// The transposition table class. This is basically just a huge array
-/// containing TTCluster objects, and a few methods for writing new entries
-/// and reading new ones.
+/// The transposition table class. This is basically just a huge array containing
+/// TTCluster objects, and a few methods for writing and reading entries.
 
 class TranspositionTable {
 
 
 class TranspositionTable {
 
@@ -148,15 +112,15 @@ public:
 private:
   size_t size;
   TTCluster* entries;
 private:
   size_t size;
   TTCluster* entries;
-  uint8_t generation; // To properly compare, size must be smaller then TT stored value
+  uint8_t generation; // Size must be not bigger then TTEntry::generation8
 };
 
 extern TranspositionTable TT;
 
 
 };
 
 extern TranspositionTable TT;
 
 
-/// TranspositionTable::first_entry returns a pointer to the first
-/// entry of a cluster given a position. The lowest order bits of the key
-/// are used to get the index of the cluster.
+/// TranspositionTable::first_entry() returns a pointer to the first entry of
+/// a cluster given a position. The lowest order bits of the key are used to
+/// get the index of the cluster.
 
 inline TTEntry* TranspositionTable::first_entry(const Key posKey) const {
 
 
 inline TTEntry* TranspositionTable::first_entry(const Key posKey) const {
 
@@ -164,12 +128,45 @@ inline TTEntry* TranspositionTable::first_entry(const Key posKey) const {
 }
 
 
 }
 
 
-/// TranspositionTable::refresh updates the 'generation' value of the TTEntry
-/// to avoid aging. Normally called after a TT hit, before to return.
+/// TranspositionTable::refresh() updates the 'generation' value of the TTEntry
+/// to avoid aging. Normally called after a TT hit.
 
 inline void TranspositionTable::refresh(const TTEntry* tte) const {
 
   const_cast<TTEntry*>(tte)->set_generation(generation);
 }
 
 
 inline void TranspositionTable::refresh(const TTEntry* tte) const {
 
   const_cast<TTEntry*>(tte)->set_generation(generation);
 }
 
+
+/// A simple fixed size hash table used to store pawns and material
+/// configurations. It is basically just an array of Entry objects.
+/// Without cluster concept or overwrite policy.
+
+template<class Entry, int HashSize>
+class SimpleHash {
+
+  SimpleHash(const SimpleHash&);
+  SimpleHash& operator=(const SimpleHash&);
+
+public:
+  SimpleHash() {
+
+    entries = new (std::nothrow) Entry[HashSize];
+    if (!entries)
+    {
+        std::cerr << "Failed to allocate " << HashSize * sizeof(Entry)
+                  << " bytes for material hash table." << std::endl;
+        exit(EXIT_FAILURE);
+    }
+    memset(entries, 0, HashSize * sizeof(Entry));
+  }
+
+  virtual ~SimpleHash() { delete [] entries; }
+
+  Entry* find(Key key) const { return entries + ((uint32_t)key & (HashSize - 1)); }
+  void prefetch(Key key) const { ::prefetch((char*)find(key)); }
+
+protected:
+  Entry* entries;
+};
+
 #endif // !defined(TT_H_INCLUDED)
 #endif // !defined(TT_H_INCLUDED)