]> git.sesse.net Git - stockfish/blob - src/tt.cpp
4ac685d51e4c9ac464bb343fc4eaa21278a8b007
[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 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
28 #include "tt.h"
29
30
31 ////
32 //// Functions
33 ////
34
35 /// Constructor
36
37 TranspositionTable::TranspositionTable(unsigned mbSize) {
38
39   size = 0;
40   generation = 0;
41   writes = 0;
42   entries = 0;
43   set_size(mbSize);
44 }
45
46
47 /// Destructor
48
49 TranspositionTable::~TranspositionTable() {
50
51   delete [] entries;
52 }
53
54
55 /// TranspositionTable::set_size sets the size of the transposition table,
56 /// measured in megabytes.
57
58 void TranspositionTable::set_size(unsigned mbSize) {
59
60   assert(mbSize >= 4 && mbSize <= 1024);
61
62   unsigned newSize = 1024;
63
64   // We store a cluster of 4 TTEntry for each position and newSize is
65   // the maximum number of storable positions
66   for ( ; newSize * 4 * (sizeof(TTEntry)) <= (mbSize << 20); newSize *= 2);
67   newSize /= 2;
68   if (newSize != size)
69   {
70     size = newSize;
71     delete [] entries;
72     entries = new TTEntry[size * 4];
73     if (!entries)
74     {
75       std::cerr << "Failed to allocate " << mbSize
76                 << " MB for transposition table."
77                 << std::endl;
78       exit(EXIT_FAILURE);
79     }
80     clear();
81   }
82 }
83
84
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?
89
90 void TranspositionTable::clear() {
91
92   memset(entries, 0, size * 4 * sizeof(TTEntry));
93 }
94
95
96 /// TranspositionTable::store writes a new entry containing a position,
97 /// a value, a value type, a search depth, and a best move to the
98 /// transposition table.  The transposition table is organized in clusters
99 /// of four TTEntry objects, and when a new entry is written, it replaces
100 /// the least valuable of the four entries in a cluster.  A TTEntry t1 is
101 /// considered to be more valuable than a TTEntry t2 if t1 is from the
102 /// current search and t2 is from a previous search, or if the depth of t1
103 /// is bigger than the depth of t2.
104
105 void TranspositionTable::store(const Position &pos, Value v, Depth d,
106                                Move m, ValueType type) {
107   TTEntry *tte, *replace;
108
109   tte = replace = first_entry(pos);
110   for (int i = 0; i < 4; i++)
111   {
112     if (!(tte+i)->key()) // still empty
113     {
114         *(tte+i) = TTEntry(pos.get_key(), v, type, d, m, generation);
115         writes++;
116         return;
117     }
118     if ((tte+i)->key() == pos.get_key()) // overwrite old
119     {
120         if (m == MOVE_NONE)
121             m = (tte+i)->move();
122
123         *(tte+i) = TTEntry(pos.get_key(), v, type, d, m, generation);
124         return;
125     }
126     if (   i == 0  // already is (replace == tte+i), common case
127         || replace->generation() < (tte+i)->generation())
128         continue;
129
130     if (    replace->generation() > (tte+i)->generation()
131         || (tte+i)->depth() < replace->depth())
132         replace = tte+i;
133   }
134   *replace = TTEntry(pos.get_key(), v, type, d, m, generation);
135   writes++;
136 }
137
138
139 /// TranspositionTable::retrieve looks up the current position in the
140 /// transposition table. Returns a pointer to the TTEntry or NULL
141 /// if position is not found.
142
143 const TTEntry* TranspositionTable::retrieve(const Position &pos) const {
144
145   TTEntry *tte = first_entry(pos);
146
147   for (int i = 0; i < 4; i++, tte++)
148   {
149       if (tte->key() == pos.get_key())
150           return tte;
151   }
152   return NULL;
153 }
154
155
156 /// TranspositionTable::first_entry returns a pointer to the first
157 /// entry of a cluster given a position.
158
159 inline TTEntry* TranspositionTable::first_entry(const Position &pos) const {
160
161   return entries + (int(pos.get_key() & (size - 1)) << 2);
162 }
163
164 /// TranspositionTable::new_search() is called at the beginning of every new
165 /// search.  It increments the "generation" variable, which is used to
166 /// distinguish transposition table entries from previous searches from
167 /// entries from the current search.
168
169 void TranspositionTable::new_search() {
170
171   generation++;
172   writes = 0;
173 }
174
175
176 /// TranspositionTable::insert_pv() is called at the end of a search
177 /// iteration, and inserts the PV back into the PV.  This makes sure the
178 /// old PV moves are searched first, even if the old TT entries have been
179 /// overwritten.
180
181 void TranspositionTable::insert_pv(const Position &pos, Move pv[]) {
182
183   UndoInfo u;
184   Position p(pos);
185
186   for (int i = 0; pv[i] != MOVE_NONE; i++)
187   {
188     store(p, VALUE_NONE, Depth(0), pv[i], VALUE_TYPE_NONE);
189     p.do_move(pv[i], u);
190   }
191 }
192
193
194 /// TranspositionTable::full() returns the permill of all transposition table
195 /// entries which have received at least one write during the current search.
196 /// It is used to display the "info hashfull ..." information in UCI.
197
198 int TranspositionTable::full() {
199
200   double N = double(size) * 4.0;
201   return int(1000 * (1 - exp(writes * log(1.0 - 1.0/N))));
202 }
203
204
205 /// Constructors
206
207 TTEntry::TTEntry() {
208 }
209
210 TTEntry::TTEntry(Key k, Value v, ValueType t, Depth d, Move m,
211                  int generation) :
212   key_ (k), data((m & 0x7FFFF) | (t << 20) | (generation << 23)),
213   value_(v), depth_(int16_t(d)) {}
214
215
216