]> git.sesse.net Git - stockfish/blob - src/tt.cpp
Fix assignment of pv[0] when creating root move list
[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 TranspositionTable::TranspositionTable() {
37
38   size = writes = 0;
39   entries = 0;
40   generation = 0;
41 }
42
43 TranspositionTable::~TranspositionTable() {
44
45   delete [] entries;
46 }
47
48
49 /// TranspositionTable::set_size sets the size of the transposition table,
50 /// measured in megabytes.
51
52 void TranspositionTable::set_size(unsigned mbSize) {
53
54   assert(mbSize >= 4 && mbSize <= 4096);
55
56   unsigned newSize = 1024;
57
58   // We store a cluster of 4 TTEntry for each position and newSize is
59   // the maximum number of storable positions
60   while ((2 * newSize) * 4 * (sizeof(TTEntry)) <= (mbSize << 20))
61       newSize *= 2;
62
63   if (newSize != size)
64   {
65       size = newSize;
66       delete [] entries;
67       entries = new TTEntry[size * 4];
68       if (!entries)
69       {
70           std::cerr << "Failed to allocate " << mbSize
71                     << " MB for transposition table." << std::endl;
72           exit(EXIT_FAILURE);
73       }
74       clear();
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 * 4 * sizeof(TTEntry));
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 Position& p, Value v, ValueType t, Depth d, Move m) {
101
102   TTEntry *tte, *replace;
103
104   tte = replace = first_entry(p);
105   for (int i = 0; i < 4; i++, tte++)
106   {
107       if (!tte->key() || tte->key() == p.get_key()) // empty or overwrite old
108       {
109           // Do not overwrite when new type is VALUE_TYPE_EVAL
110           if (tte->key() && t == VALUE_TYPE_EVAL)
111               return;
112
113           if (m == MOVE_NONE)
114               m = tte->move();
115
116           *tte = TTEntry(p.get_key(), v, t, d, m, generation);
117           return;
118       }
119       else if (i == 0)  // replace would be a no-op in this common case
120           continue;
121
122       int c1 = (replace->generation() == generation ?  2 : 0);
123       int c2 = (tte->generation() == generation ? -2 : 0);
124       int c3 = (tte->depth() < replace->depth() ?  1 : 0);
125
126       if (c1 + c2 + c3 > 0)
127           replace = tte;
128   }
129   *replace = TTEntry(p.get_key(), v, t, d, m, generation);
130   writes++;
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 Position& pos) const {
139
140   TTEntry *tte = first_entry(pos);
141
142   for (int i = 0; i < 4; i++, tte++)
143       if (tte->key() == pos.get_key())
144           return tte;
145
146   return NULL;
147 }
148
149
150 /// TranspositionTable::first_entry returns a pointer to the first
151 /// entry of a cluster given a position.
152
153 inline TTEntry* TranspositionTable::first_entry(const Position& pos) const {
154
155   return entries + (int(pos.get_key() & (size - 1)) << 2);
156 }
157
158 /// TranspositionTable::new_search() is called at the beginning of every new
159 /// search. It increments the "generation" variable, which is used to
160 /// distinguish transposition table entries from previous searches from
161 /// entries from the current search.
162
163 void TranspositionTable::new_search() {
164
165   generation++;
166   writes = 0;
167 }
168
169
170 /// TranspositionTable::insert_pv() is called at the end of a search
171 /// iteration, and inserts the PV back into the PV. This makes sure
172 /// the old PV moves are searched first, even if the old TT entries
173 /// have been overwritten.
174
175 void TranspositionTable::insert_pv(const Position& pos, Move pv[]) {
176
177   StateInfo st;
178   Position p(pos);
179
180   for (int i = 0; pv[i] != MOVE_NONE; i++)
181   {
182       store(p, VALUE_NONE, VALUE_TYPE_NONE, Depth(-127*OnePly), pv[i]);
183       p.do_move(pv[i], st);
184   }
185 }
186
187
188 /// TranspositionTable::full() returns the permill of all transposition table
189 /// entries which have received at least one write during the current search.
190 /// It is used to display the "info hashfull ..." information in UCI.
191
192 int TranspositionTable::full() const {
193
194   double N = double(size) * 4.0;
195   return int(1000 * (1 - exp(writes * log(1.0 - 1.0/N))));
196 }