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-2009 Marco Costalba
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/>.
28 #include <xmmintrin.h>
34 /// This is the number of TTEntry slots for each position
35 static const int ClusterSize = 5;
42 TranspositionTable::TranspositionTable() {
49 TranspositionTable::~TranspositionTable() {
55 /// TranspositionTable::set_size sets the size of the transposition table,
56 /// measured in megabytes.
58 void TranspositionTable::set_size(unsigned mbSize) {
60 assert(mbSize >= 4 && mbSize <= 4096);
62 unsigned newSize = 1024;
64 // We store a cluster of ClusterSize number of TTEntry for each position
65 // and newSize is the maximum number of storable positions.
66 while ((2 * newSize) * ClusterSize * (sizeof(TTEntry)) <= (mbSize << 20))
73 entries = new TTEntry[size * ClusterSize];
76 std::cerr << "Failed to allocate " << mbSize
77 << " MB for transposition table." << std::endl;
78 Application::exit_with_failure();
85 /// TranspositionTable::clear overwrites the entire transposition table
86 /// with zeroes. It is called whenever the table is resized, or when the
87 /// user asks the program to clear the table (from the UCI interface).
88 /// Perhaps we should also clear it when the "ucinewgame" command is recieved?
90 void TranspositionTable::clear() {
92 memset(entries, 0, size * ClusterSize * sizeof(TTEntry));
96 /// TranspositionTable::first_entry returns a pointer to the first
97 /// entry of a cluster given a position. The low 32 bits of the key
98 /// are used to get the index in the table.
100 inline TTEntry* TranspositionTable::first_entry(const Key posKey) const {
102 return entries + ((uint32_t(posKey) & (size - 1)) * ClusterSize);
106 /// TranspositionTable::store writes a new entry containing a position,
107 /// a value, a value type, a search depth, and a best move to the
108 /// transposition table. Transposition table is organized in clusters of
109 /// four TTEntry objects, and when a new entry is written, it replaces
110 /// the least valuable of the four entries in a cluster. A TTEntry t1 is
111 /// considered to be more valuable than a TTEntry t2 if t1 is from the
112 /// current search and t2 is from a previous search, or if the depth of t1
113 /// is bigger than the depth of t2. A TTEntry of type VALUE_TYPE_EVAL
114 /// never replaces another entry for the same position.
116 void TranspositionTable::store(const Key posKey, Value v, ValueType t, Depth d, Move m) {
118 TTEntry *tte, *replace;
119 uint32_t posKey32 = posKey >> 32; // Use the high 32 bits as key
121 tte = replace = first_entry(posKey);
122 for (int i = 0; i < ClusterSize; i++, tte++)
124 if (!tte->key() || tte->key() == posKey32) // empty or overwrite old
126 // Do not overwrite when new type is VALUE_TYPE_EVAL
127 if (tte->key() && t == VALUE_TYPE_EVAL)
133 *tte = TTEntry(posKey32, v, t, d, m, generation);
136 else if (i == 0) // replace would be a no-op in this common case
139 int c1 = (replace->generation() == generation ? 2 : 0);
140 int c2 = (tte->generation() == generation ? -2 : 0);
141 int c3 = (tte->depth() < replace->depth() ? 1 : 0);
143 if (c1 + c2 + c3 > 0)
146 *replace = TTEntry(posKey32, v, t, d, m, generation);
151 /// TranspositionTable::retrieve looks up the current position in the
152 /// transposition table. Returns a pointer to the TTEntry or NULL
153 /// if position is not found.
155 TTEntry* TranspositionTable::retrieve(const Key posKey) const {
157 uint32_t posKey32 = posKey >> 32;
158 TTEntry* tte = first_entry(posKey);
160 for (int i = 0; i < ClusterSize; i++, tte++)
161 if (tte->key() == posKey32)
168 /// TranspositionTable::prefetch looks up the current position in the
169 /// transposition table and load it in L1/L2 cache. This is a non
170 /// blocking function and do not stalls the CPU waiting for data
171 /// to be loaded from RAM, that can be very slow. When we will
172 /// subsequently call retrieve() the TT data will be already
173 /// quickly accessible in L1/l2 CPU cache.
175 void TranspositionTable::prefetch(const Key posKey) const {
177 _mm_prefetch((char*)first_entry(posKey), _MM_HINT_T0);
181 /// TranspositionTable::new_search() is called at the beginning of every new
182 /// search. It increments the "generation" variable, which is used to
183 /// distinguish transposition table entries from previous searches from
184 /// entries from the current search.
186 void TranspositionTable::new_search() {
193 /// TranspositionTable::insert_pv() is called at the end of a search
194 /// iteration, and inserts the PV back into the PV. This makes sure
195 /// the old PV moves are searched first, even if the old TT entries
196 /// have been overwritten.
198 void TranspositionTable::insert_pv(const Position& pos, Move pv[]) {
203 for (int i = 0; pv[i] != MOVE_NONE; i++)
205 store(p.get_key(), VALUE_NONE, VALUE_TYPE_NONE, Depth(-127*OnePly), pv[i]);
206 p.do_move(pv[i], st);
211 /// TranspositionTable::extract_pv() extends a PV by adding moves from the
212 /// transposition table at the end. This should ensure that the PV is almost
213 /// always at least two plies long, which is important, because otherwise we
214 /// will often get single-move PVs when the search stops while failing high,
215 /// and a single-move PV means that we don't have a ponder move.
217 void TranspositionTable::extract_pv(const Position& pos, Move pv[]) {
223 for (ply = 0; pv[ply] != MOVE_NONE; ply++)
224 p.do_move(pv[ply], st[ply]);
228 for (stop = false, tte = retrieve(p.get_key());
229 tte && tte->move() != MOVE_NONE && !stop;
230 tte = retrieve(p.get_key()), ply++)
232 if (!move_is_legal(p, tte->move()))
234 pv[ply] = tte->move();
235 p.do_move(pv[ply], st[ply]);
236 for (int j = 0; j < ply; j++)
237 if (st[j].key == p.get_key()) stop = true;
243 /// TranspositionTable::full() returns the permill of all transposition table
244 /// entries which have received at least one write during the current search.
245 /// It is used to display the "info hashfull ..." information in UCI.
247 int TranspositionTable::full() const {
249 double N = double(size) * ClusterSize;
250 return int(1000 * (1 - exp(writes * log(1.0 - 1.0/N))));