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