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