Warnings termination fest
[glaurung_clone.git] / src / tt.cpp
blob1959693258c3d8e0e6dbad995103ed0ee766cbe2
1 /*
2 Glaurung, a UCI chess playing engine.
3 Copyright (C) 2004-2008 Tord Romstad
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.
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.
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/>.
20 ////
21 //// Includes
22 ////
24 #include <cassert>
25 #include <cmath>
27 #include "tt.h"
30 ////
31 //// Functions
32 ////
34 /// Constructor
36 TranspositionTable::TranspositionTable(unsigned mbSize) {
38 size = 0;
39 generation = 0;
40 writes = 0;
41 entries = 0;
42 set_size(mbSize);
46 /// Destructor
48 TranspositionTable::~TranspositionTable() {
50 delete [] entries;
54 /// TranspositionTable::set_size sets the size of the transposition table,
55 /// measured in megabytes.
57 void TranspositionTable::set_size(unsigned mbSize) {
59 assert(mbSize >= 4 && mbSize <= 1024);
61 unsigned newSize = 1024;
63 // We store a cluster of 4 TTEntry for each position and newSize is
64 // the maximum number of storable positions
65 for ( ; newSize * 4 * (sizeof(TTEntry)) <= (mbSize << 20); newSize *= 2);
66 newSize /= 2;
67 if (newSize != size)
69 size = newSize;
70 delete [] entries;
71 entries = new TTEntry[size * 4];
72 if (!entries)
74 std::cerr << "Failed to allocate " << mbSize
75 << " MB for transposition table."
76 << std::endl;
77 exit(EXIT_FAILURE);
79 clear();
84 /// TranspositionTable::clear overwrites the entire transposition table
85 /// with zeroes. It is called whenever the table is resized, or when the
86 /// user asks the program to clear the table (from the UCI interface).
87 /// Perhaps we should also clear it when the "ucinewgame" command is recieved?
89 void TranspositionTable::clear() {
91 memset(entries, 0, size * 4 * sizeof(TTEntry));
95 /// TranspositionTable::store writes a new entry containing a position,
96 /// a value, a value type, a search depth, and a best move to the
97 /// transposition table. The transposition table is organized in clusters
98 /// of four TTEntry objects, and when a new entry is written, it replaces
99 /// the least valuable of the four entries in a cluster. A TTEntry t1 is
100 /// considered to be more valuable than a TTEntry t2 if t1 is from the
101 /// current search and t2 is from a previous search, or if the depth of t1
102 /// is bigger than the depth of t2.
104 void TranspositionTable::store(const Position &pos, Value v, Depth d,
105 Move m, ValueType type) {
106 TTEntry *tte, *replace;
108 tte = replace = first_entry(pos);
109 for (int i = 0; i < 4; i++)
111 if (!(tte+i)->key()) // still empty
113 *(tte+i) = TTEntry(pos.get_key(), v, type, d, m, generation);
114 writes++;
115 return;
117 if ((tte+i)->key() == pos.get_key()) // overwrite old
119 if (m == MOVE_NONE)
120 m = (tte+i)->move();
122 *(tte+i) = TTEntry(pos.get_key(), v, type, d, m, generation);
123 return;
125 if ( i == 0 // already is (replace == tte+i), common case
126 || replace->generation() < (tte+i)->generation())
127 continue;
129 if ( replace->generation() > (tte+i)->generation()
130 || (tte+i)->depth() < replace->depth())
131 replace = tte+i;
133 *replace = TTEntry(pos.get_key(), v, type, d, m, generation);
134 writes++;
138 /// TranspositionTable::retrieve looks up the current position in the
139 /// transposition table. Returns a pointer to the TTEntry or NULL
140 /// if position is not found.
142 const TTEntry* TranspositionTable::retrieve(const Position &pos) const {
144 TTEntry *tte = first_entry(pos);
146 for (int i = 0; i < 4; i++)
148 tte += i;
149 if (tte->key() == pos.get_key())
150 return tte;
152 return NULL;
156 /// TranspositionTable::first_entry returns a pointer to the first
157 /// entry of a cluster given a position.
159 inline TTEntry* TranspositionTable::first_entry(const Position &pos) const {
161 return entries + (int(pos.get_key() & (size - 1)) << 2);
164 /// TranspositionTable::new_search() is called at the beginning of every new
165 /// search. It increments the "generation" variable, which is used to
166 /// distinguish transposition table entries from previous searches from
167 /// entries from the current search.
169 void TranspositionTable::new_search() {
171 generation++;
172 writes = 0;
176 /// TranspositionTable::insert_pv() is called at the end of a search
177 /// iteration, and inserts the PV back into the PV. This makes sure the
178 /// old PV moves are searched first, even if the old TT entries have been
179 /// overwritten.
181 void TranspositionTable::insert_pv(const Position &pos, Move pv[]) {
183 UndoInfo u;
184 Position p(pos);
186 for (int i = 0; pv[i] != MOVE_NONE; i++)
188 store(p, VALUE_NONE, Depth(0), pv[i], VALUE_TYPE_NONE);
189 p.do_move(pv[i], u);
194 /// TranspositionTable::full() returns the permill of all transposition table
195 /// entries which have received at least one write during the current search.
196 /// It is used to display the "info hashfull ..." information in UCI.
198 int TranspositionTable::full() {
200 double N = double(size) * 4.0;
201 return int(1000 * (1 - exp(writes * log(1.0 - 1.0/N))));
205 /// Constructors
207 TTEntry::TTEntry() {
210 TTEntry::TTEntry(Key k, Value v, ValueType t, Depth d, Move m,
211 int generation) :
212 key_ (k), data((m & 0x7FFFF) | (t << 20) | (generation << 23)),
213 value_(v), depth_(int16_t(d)) {}