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