From: Marco Costalba Date: Fri, 15 Apr 2011 16:14:12 +0000 (+0200) Subject: Code style in tt.cpp X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=c13b53a5144163a81d214fb72733b3400b2da002 Code style in tt.cpp No functional change. Signed-off-by: Marco Costalba --- diff --git a/src/tt.cpp b/src/tt.cpp index f598e3bf..d18b512c 100644 --- a/src/tt.cpp +++ b/src/tt.cpp @@ -17,29 +17,18 @@ along with this program. If not, see . */ - -//// -//// Includes -//// - #include #include #include #include "tt.h" -// The main transposition table -TranspositionTable TT; - -//// -//// Functions -//// +TranspositionTable TT; // Our global transposition table TranspositionTable::TranspositionTable() { - size = 0; - entries = 0; - generation = 0; + size = generation = 0; + entries = NULL; } 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) { @@ -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). -/// Perhaps we should also clear it when the "ucinewgame" command is received? 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, -/// 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; - 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); + 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; } - // 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); @@ -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 { diff --git a/src/tt.h b/src/tt.h index fe4cee62..daa66c33 100644 --- a/src/tt.h +++ b/src/tt.h @@ -25,40 +25,8 @@ #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 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 /// @@ -92,42 +60,38 @@ public: } 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; - 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; -/// 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]; }; -/// 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 { @@ -148,15 +112,15 @@ public: 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; -/// 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 { @@ -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(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 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)