]> git.sesse.net Git - stockfish/blob - src/tt.cpp
Use 32 bit key in TT
[stockfish] / src / tt.cpp
1 /*
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
5
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.
10
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.
15
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/>.
18 */
19
20
21 ////
22 //// Includes
23 ////
24
25 #include <cassert>
26 #include <cmath>
27 #include <cstring>
28
29 #include "movegen.h"
30 #include "tt.h"
31
32
33 /// This is the number of TTEntry slots for each position
34 static const int ClusterSize = 4;
35
36
37 ////
38 //// Functions
39 ////
40
41 TranspositionTable::TranspositionTable() {
42
43   size = writes = 0;
44   entries = 0;
45   generation = 0;
46 }
47
48 TranspositionTable::~TranspositionTable() {
49
50   delete [] entries;
51 }
52
53
54 /// TranspositionTable::set_size sets the size of the transposition table,
55 /// measured in megabytes.
56
57 void TranspositionTable::set_size(unsigned mbSize) {
58
59   assert(mbSize >= 4 && mbSize <= 4096);
60
61   unsigned newSize = 1024;
62
63   // We store a cluster of ClusterSize number of TTEntry for each position
64   // and newSize is the maximum number of storable positions.
65   while ((2 * newSize) * ClusterSize * (sizeof(TTEntry)) <= (mbSize << 20))
66       newSize *= 2;
67
68   if (newSize != size)
69   {
70       size = newSize;
71       delete [] entries;
72       entries = new TTEntry[size * ClusterSize];
73       if (!entries)
74       {
75           std::cerr << "Failed to allocate " << mbSize
76                     << " MB for transposition table." << std::endl;
77           Application::exit_with_failure();
78       }
79       clear();
80   }
81 }
82
83
84 /// TranspositionTable::clear overwrites the entire transposition table
85 /// with zeroes. It is called whenever the table is resized, or when the
86 /// user asks the program to clear the table (from the UCI interface).
87 /// Perhaps we should also clear it when the "ucinewgame" command is recieved?
88
89 void TranspositionTable::clear() {
90
91   memset(entries, 0, size * ClusterSize * sizeof(TTEntry));
92 }
93
94
95 /// TranspositionTable::store writes a new entry containing a position,
96 /// a value, a value type, a search depth, and a best move to the
97 /// transposition table. Transposition table is organized in clusters of
98 /// four TTEntry objects, and when a new entry is written, it replaces
99 /// the least valuable of the four entries in a cluster. A TTEntry t1 is
100 /// considered to be more valuable than a TTEntry t2 if t1 is from the
101 /// current search and t2 is from a previous search, or if the depth of t1
102 /// is bigger than the depth of t2. A TTEntry of type VALUE_TYPE_EVAL
103 /// never replaces another entry for the same position.
104
105 void TranspositionTable::store(const Key posKey, Value v, ValueType t, Depth d, Move m) {
106
107   TTEntry *tte, *replace;
108   uint32_t posKey32 = posKey >> 32; // Use the high 32 bits as key
109
110   tte = replace = first_entry(posKey);
111   for (int i = 0; i < ClusterSize; i++, tte++)
112   {
113       if (!tte->key() || tte->key() == posKey32) // empty or overwrite old
114       {
115           // Do not overwrite when new type is VALUE_TYPE_EVAL
116           if (tte->key() && t == VALUE_TYPE_EVAL)
117               return;
118
119           if (m == MOVE_NONE)
120               m = tte->move();
121
122           *tte = TTEntry(posKey32, v, t, d, m, generation);
123           return;
124       }
125       else if (i == 0)  // replace would be a no-op in this common case
126           continue;
127
128       int c1 = (replace->generation() == generation ?  2 : 0);
129       int c2 = (tte->generation() == generation ? -2 : 0);
130       int c3 = (tte->depth() < replace->depth() ?  1 : 0);
131
132       if (c1 + c2 + c3 > 0)
133           replace = tte;
134   }
135   *replace = TTEntry(posKey32, v, t, d, m, generation);
136   writes++;
137 }
138
139
140 /// TranspositionTable::retrieve looks up the current position in the
141 /// transposition table. Returns a pointer to the TTEntry or NULL
142 /// if position is not found.
143
144 TTEntry* TranspositionTable::retrieve(const Key posKey) const {
145
146   uint32_t posKey32 = posKey >> 32;
147   TTEntry *tte = first_entry(posKey);
148
149   for (int i = 0; i < ClusterSize; i++, tte++)
150       if (tte->key() == posKey32)
151           return tte;
152
153   return NULL;
154 }
155
156
157 /// TranspositionTable::first_entry returns a pointer to the first
158 /// entry of a cluster given a position. The low 32 bits of the key
159 /// are used to get the index in the table.
160
161 inline TTEntry* TranspositionTable::first_entry(const Key posKey) const {
162
163   return entries + ((uint32_t(posKey) & (size - 1)) * ClusterSize);
164 }
165
166 /// TranspositionTable::new_search() is called at the beginning of every new
167 /// search. It increments the "generation" variable, which is used to
168 /// distinguish transposition table entries from previous searches from
169 /// entries from the current search.
170
171 void TranspositionTable::new_search() {
172
173   generation++;
174   writes = 0;
175 }
176
177
178 /// TranspositionTable::insert_pv() is called at the end of a search
179 /// iteration, and inserts the PV back into the PV. This makes sure
180 /// the old PV moves are searched first, even if the old TT entries
181 /// have been overwritten.
182
183 void TranspositionTable::insert_pv(const Position& pos, Move pv[]) {
184
185   StateInfo st;
186   Position p(pos);
187
188   for (int i = 0; pv[i] != MOVE_NONE; i++)
189   {
190       store(p.get_key(), VALUE_NONE, VALUE_TYPE_NONE, Depth(-127*OnePly), pv[i]);
191       p.do_move(pv[i], st);
192   }
193 }
194
195
196 /// TranspositionTable::extract_pv() extends a PV by adding moves from the
197 /// transposition table at the end. This should ensure that the PV is almost
198 /// always at least two plies long, which is important, because otherwise we
199 /// will often get single-move PVs when the search stops while failing high,
200 /// and a single-move PV means that we don't have a ponder move.
201
202 void TranspositionTable::extract_pv(const Position& pos, Move pv[]) {
203
204   int ply;
205   Position p(pos);
206   StateInfo st[100];
207
208   for (ply = 0; pv[ply] != MOVE_NONE; ply++)
209       p.do_move(pv[ply], st[ply]);
210
211   bool stop;
212   const TTEntry* tte;
213   for (stop = false, tte = retrieve(p.get_key());
214        tte && tte->move() != MOVE_NONE && !stop;
215        tte = retrieve(p.get_key()), ply++)
216   {
217       if (!move_is_legal(p, tte->move()))
218           break;
219       pv[ply] = tte->move();
220       p.do_move(pv[ply], st[ply]);
221       for (int j = 0; j < ply; j++)
222           if (st[j].key == p.get_key()) stop = true;
223   }
224   pv[ply] = MOVE_NONE;
225 }
226
227
228 /// TranspositionTable::full() returns the permill of all transposition table
229 /// entries which have received at least one write during the current search.
230 /// It is used to display the "info hashfull ..." information in UCI.
231
232 int TranspositionTable::full() const {
233
234   double N = double(size) * ClusterSize;
235   return int(1000 * (1 - exp(writes * log(1.0 - 1.0/N))));
236 }