2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4 Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
6 Stockfish is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Stockfish is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #if !defined(TT_H_INCLUDED)
38 /// A simple fixed size hash table used to store pawns and material
39 /// configurations. It is basically just an array of Entry objects.
40 /// Without cluster concept or overwrite policy.
42 template<class Entry, int HashSize>
45 SimpleHash(const SimpleHash&);
46 SimpleHash& operator=(const SimpleHash&);
51 entries = new Entry[HashSize];
54 std::cerr << "Failed to allocate " << HashSize * sizeof(Entry)
55 << " bytes for material hash table." << std::endl;
58 memset(entries, 0, HashSize * sizeof(Entry));
61 ~SimpleHash() { delete [] entries; }
63 Entry* find(Key key) const { return entries + ((uint32_t)key & (HashSize - 1)); }
70 /// The TTEntry class is the class of transposition table entries
72 /// A TTEntry needs 128 bits to be stored
78 /// bit 96-111: static value
79 /// bit 112-127: margin of static value
81 /// the 32 bits of the data field are so defined
84 /// bit 16-20: not used
85 /// bit 21-22: value type
86 /// bit 23-31: generation
91 void save(uint32_t k, Value v, ValueType t, Depth d, Move m, int g, Value statV, Value kd) {
94 data = (m & 0xFFFF) | (t << 21) | (g << 23);
97 staticValue = (int16_t)statV;
98 staticValueMargin = (int16_t)kd;
100 void set_generation(int g) { data = move() | (type() << 21) | (g << 23); }
102 uint32_t key() const { return key32; }
103 Depth depth() const { return Depth(depth16); }
104 Move move() const { return Move(data & 0xFFFF); }
105 Value value() const { return Value(value16); }
106 ValueType type() const { return ValueType((data >> 21) & 3); }
107 int generation() const { return data >> 23; }
108 Value static_value() const { return Value(staticValue); }
109 Value static_value_margin() const { return Value(staticValueMargin); }
117 int16_t staticValueMargin;
121 /// This is the number of TTEntry slots for each cluster
122 const int ClusterSize = 4;
124 /// TTCluster consists of ClusterSize number of TTEntries.
125 /// Size of TTCluster must not be bigger than a cache line size.
126 /// In case it is less, it should be padded to guarantee always aligned accesses.
129 TTEntry data[ClusterSize];
133 /// The transposition table class. This is basically just a huge array
134 /// containing TTCluster objects, and a few methods for writing new entries
135 /// and reading new ones.
137 class TranspositionTable {
139 TranspositionTable(const TranspositionTable&);
140 TranspositionTable& operator=(const TranspositionTable&);
143 TranspositionTable();
144 ~TranspositionTable();
145 void set_size(size_t mbSize);
147 void store(const Key posKey, Value v, ValueType type, Depth d, Move m, Value statV, Value kingD);
148 TTEntry* retrieve(const Key posKey) const;
150 TTEntry* first_entry(const Key posKey) const;
151 void refresh(const TTEntry* tte) const;
156 uint8_t generation; // To properly compare, size must be smaller then TT stored value
159 extern TranspositionTable TT;
162 /// TranspositionTable::first_entry returns a pointer to the first
163 /// entry of a cluster given a position. The lowest order bits of the key
164 /// are used to get the index of the cluster.
166 inline TTEntry* TranspositionTable::first_entry(const Key posKey) const {
168 return entries[((uint32_t)posKey) & (size - 1)].data;
172 /// TranspositionTable::refresh updates the 'generation' value of the TTEntry
173 /// to avoid aging. Normally called after a TT hit, before to return.
175 inline void TranspositionTable::refresh(const TTEntry* tte) const {
177 const_cast<TTEntry*>(tte)->set_generation(generation);
180 #endif // !defined(TT_H_INCLUDED)