]> git.sesse.net Git - stockfish/blob - src/tt.cpp
268c21f896ab3c89e962ae6dde36c9001030231b
[stockfish] / src / tt.cpp
1 /*
2   Glaurung, a UCI chess playing engine.
3   Copyright (C) 2004-2008 Tord Romstad
4
5   Glaurung is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, either version 3 of the License, or
8   (at your option) any later version.
9
10   Glaurung is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20 ////
21 //// Includes
22 ////
23
24 #include <cassert>
25 #include <cmath>
26
27 #include "tt.h"
28
29
30 ////
31 //// Functions
32 ////
33
34 /// Constructor
35
36 TranspositionTable::TranspositionTable(unsigned mbSize) {
37
38   size = 0;
39   generation = 0;
40   writes = 0;
41   entries = 0;
42   set_size(mbSize);
43 }
44
45
46 /// Destructor
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 <= 1024);
60
61   unsigned newSize = 1024;
62
63   // We store a cluster of 4 TTEntry for each position and newSize is
64   // the maximum number of storable positions
65   for ( ; newSize * 4 * (sizeof(TTEntry)) <= (mbSize << 20); newSize *= 2);
66   newSize /= 2;
67   if (newSize != size)
68   {
69     size = newSize;
70     delete [] entries;
71     entries = new TTEntry[size * 4];
72     if (!entries)
73     {
74       std::cerr << "Failed to allocate " << mbSize
75                 << " MB for transposition table."
76                 << std::endl;
77       exit(EXIT_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 * 4 * 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.  The transposition table is organized in clusters
98 /// of 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.
103
104 void TranspositionTable::store(const Position &pos, Value v, Depth d,
105                                Move m, ValueType type) {
106   TTEntry *tte, *replace;
107
108   tte = replace = first_entry(pos);
109   for (int i = 0; i < 4; i++)
110   {
111     if (!(tte+i)->key()) // still empty
112     {
113         *(tte+i) = TTEntry(pos.get_key(), v, type, d, m, generation);
114         writes++;
115         return;
116     }
117     if ((tte+i)->key() == pos.get_key()) // overwrite old
118     {
119         if (m == MOVE_NONE)
120             m = (tte+i)->move();
121
122         *(tte+i) = TTEntry(pos.get_key(), v, type, d, m, generation);
123         return;
124     }
125     if (   i == 0  // already is (replace == tte+i), common case
126         || replace->generation() < (tte+i)->generation())
127         continue;
128
129     if (    replace->generation() > (tte+i)->generation()
130         || (tte+i)->depth() < replace->depth())
131         replace = tte+i;
132   }
133   *replace = TTEntry(pos.get_key(), v, type, d, m, generation);
134   writes++;
135 }
136
137
138 /// TranspositionTable::retrieve looks up the current position in the
139 /// transposition table, and extracts the value, value type, depth and
140 /// best move if the position is found.  The return value is true if
141 /// the position is found, and false if it isn't.
142
143 bool TranspositionTable::retrieve(const Position &pos, Value *value,
144                                   Depth *d, Move *move,
145                                   ValueType *type) const {
146   TTEntry *tte = first_entry(pos);
147
148   for (int i = 0; i < 4; i++)
149   {
150       tte += i;
151       if (tte->key() == pos.get_key())
152       {
153           *value = tte->value();
154           *type = tte->type();
155           *d = tte->depth();
156           *move = tte->move();
157           return true;
158       }
159   }
160   *move = MOVE_NONE;
161   return false;
162 }
163
164
165 /// TranspositionTable::first_entry returns a pointer to the first
166 /// entry of a cluster given a position.
167
168 inline TTEntry* TranspositionTable::first_entry(const Position &pos) const {
169
170   return entries + (int(pos.get_key() & (size - 1)) << 2);
171 }
172
173 /// TranspositionTable::new_search() is called at the beginning of every new
174 /// search.  It increments the "generation" variable, which is used to
175 /// distinguish transposition table entries from previous searches from
176 /// entries from the current search.
177
178 void TranspositionTable::new_search() {
179
180   generation++;
181   writes = 0;
182 }
183
184
185 /// TranspositionTable::insert_pv() is called at the end of a search
186 /// iteration, and inserts the PV back into the PV.  This makes sure the
187 /// old PV moves are searched first, even if the old TT entries have been
188 /// overwritten.
189
190 void TranspositionTable::insert_pv(const Position &pos, Move pv[]) {
191
192   UndoInfo u;
193   Position p(pos);
194
195   for (int i = 0; pv[i] != MOVE_NONE; i++)
196   {
197     store(p, VALUE_NONE, Depth(0), pv[i], VALUE_TYPE_NONE);
198     p.do_move(pv[i], u);
199   }
200 }
201
202
203 /// TranspositionTable::full() returns the permill of all transposition table
204 /// entries which have received at least one write during the current search.
205 /// It is used to display the "info hashfull ..." information in UCI.
206
207 int TranspositionTable::full() {
208
209   double N = double(size) * 4.0;
210   return int(1000 * (1 - exp(writes * log(1.0 - 1.0/N))));
211 }
212
213
214 /// Constructors
215
216 TTEntry::TTEntry() {
217 }
218
219 TTEntry::TTEntry(Key k, Value v, ValueType t, Depth d, Move m,
220                  int generation) :
221   key_ (k), data((m & 0x7FFFF) | (t << 20) | (generation << 23)),
222   value_(v), depth_(int16_t(d)) {}
223
224
225 /// Functions for extracting data from TTEntry objects.
226
227 inline Key TTEntry::key() const {
228   return key_;
229 }
230
231 inline Depth TTEntry::depth() const {
232   return Depth(depth_);
233 }
234
235 inline Move TTEntry::move() const {
236   return Move(data & 0x7FFFF);
237 }
238
239 inline Value TTEntry::value() const {
240   return Value(value_);
241 }
242
243 inline ValueType TTEntry::type() const {
244   return ValueType((data >> 20) & 3);
245 }
246
247 inline int TTEntry::generation() const {
248   return (data >> 23);
249 }