]> git.sesse.net Git - stockfish/blob - src/tt.cpp
Initial import of Glaurung 2.1
[stockfish] / src / tt.cpp
1 /*
2   Glaurung, a UCI chess playing engine.
3   Copyright (C) 2004-2008 Tord Romstad
4
5   Glaurung is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, either version 3 of the License, or
8   (at your option) any later version.
9   
10   Glaurung is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14   
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20 ////
21 //// Includes
22 ////
23
24 #include <cassert>
25 #include <cmath>
26
27 #include "tt.h"
28
29
30 ////
31 //// Functions
32 ////
33
34 /// Constructor
35
36 TranspositionTable::TranspositionTable(unsigned mbSize) {
37   size = 0;
38   generation = 0;
39   writes = 0;
40   entries = 0;
41   this->set_size(mbSize);
42 }
43
44
45 /// Destructor
46
47 TranspositionTable::~TranspositionTable() {
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(unsigned mbSize) {
56   unsigned newSize;
57
58   assert(mbSize >= 4 && mbSize <= 1024);
59
60   for(newSize = 1024; newSize * 4 * (sizeof(TTEntry)) <= (mbSize << 20);
61       newSize *= 2);
62   newSize /= 2;
63
64   if(newSize != size) {
65     size = newSize;
66     delete [] entries;
67     entries = new TTEntry[size * 4];
68     if(entries == NULL) {
69       std::cerr << "Failed to allocate " << mbSize
70                 << " MB for transposition table."
71                 << std::endl;
72       exit(EXIT_FAILURE);
73     }
74     this->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   memset(entries, 0, size * 4 * sizeof(TTEntry));
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.  The transposition table is organized in clusters
92 /// of 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.
97
98 void TranspositionTable::store(const Position &pos, Value v, Depth d,
99                                Move m, ValueType type) {
100   TTEntry *tte, *replace;
101
102   tte = replace = entries + int(pos.get_key() & (size - 1)) * 4;
103   for(int i = 0; i < 4; i++) {
104     if((tte+i)->key() == pos.get_key()) {
105       if(m == MOVE_NONE)
106         m = (tte+i)->move();
107       *(tte+i) = TTEntry(pos.get_key(), v, type, d, m, generation);
108       return;
109     }
110     if(replace->generation() == generation) {
111       if((tte+i)->generation() != generation ||
112          (tte+i)->depth() < replace->depth())
113         replace = tte+i;
114     }
115     else if((tte+i)->generation() != generation &&
116             (tte+i)->depth() < replace->depth())
117       replace = tte+i;
118   }
119   *replace = TTEntry(pos.get_key(), v, type, d, m, generation);
120   writes++;
121 }
122
123
124 /// TranspositionTable::retrieve looks up the current position in the
125 /// transposition table, and extracts the value, value type, depth and
126 /// best move if the position is found.  The return value is true if
127 /// the position is found, and false if it isn't.
128
129 bool TranspositionTable::retrieve(const Position &pos, Value *value,
130                                   Depth *d, Move *move,
131                                   ValueType *type) const {
132   TTEntry *tte;
133   bool found = false;
134
135   tte = entries + int(pos.get_key() & (size - 1)) * 4;
136   for(int i = 0; i < 4 && !found ; i++)
137     if((tte+i)->key() == pos.get_key()) {
138       tte = tte + i;
139       found = true;
140     }
141   if(!found) {
142     *move = MOVE_NONE;
143     return false;
144   }
145
146   *value = tte->value();
147   *type = tte->type();
148   *d = tte->depth();
149   *move = tte->move();
150
151   return true;
152 }
153
154
155 /// TranspositionTable::new_search() is called at the beginning of every new
156 /// search.  It increments the "generation" variable, which is used to
157 /// distinguish transposition table entries from previous searches from
158 /// entries from the current search.
159
160 void TranspositionTable::new_search() {
161   generation++;
162   writes = 0;
163 }
164
165
166 /// TranspositionTable::insert_pv() is called at the end of a search 
167 /// iteration, and inserts the PV back into the PV.  This makes sure the
168 /// old PV moves are searched first, even if the old TT entries have been
169 /// overwritten.
170
171 void TranspositionTable::insert_pv(const Position &pos, Move pv[]) {
172   UndoInfo u;
173   Position p(pos);
174
175   for(int i = 0; pv[i] != MOVE_NONE; i++) {
176     this->store(p, VALUE_NONE, Depth(0), pv[i], VALUE_TYPE_NONE);
177     p.do_move(pv[i], u);
178   }
179 }
180
181
182 /// TranspositionTable::full() returns the permill of all transposition table
183 /// entries which have received at least one write during the current search.
184 /// It is used to display the "info hashfull ..." information in UCI.
185
186 int TranspositionTable::full() {
187   double N = double(size) * 4.0;
188   return int(1000 * (1 - exp(writes * log(1.0 - 1.0/N))));
189 }
190
191
192 /// Constructors
193
194 TTEntry::TTEntry() {
195 }
196
197 TTEntry::TTEntry(Key k, Value v, ValueType t, Depth d, Move m,
198                  int generation) {
199   key_ = k;
200   data = (m & 0x7FFFF) | (t << 20) | (generation << 23);
201   value_ = v;
202   depth_ = int16_t(d);
203 }
204
205
206 /// Functions for extracting data from TTEntry objects.
207
208 Key TTEntry::key() const {
209   return key_;
210 }
211
212 Depth TTEntry::depth() const {
213   return Depth(depth_);
214 }
215
216 Move TTEntry::move() const {
217   return Move(data & 0x7FFFF);
218 }
219
220 Value TTEntry::value() const {
221   return Value(value_);
222 }
223
224 ValueType TTEntry::type() const {
225   return ValueType((data >> 20) & 3);
226 }
227
228 int TTEntry::generation() const {
229   return (data >> 23);
230 }