]> git.sesse.net Git - stockfish/blob - src/tt.cpp
Rename constants to use *_NONE scheme
[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 "tt.h"
29
30 // The main transposition table
31 TranspositionTable TT;
32
33 ////
34 //// Functions
35 ////
36
37 TranspositionTable::TranspositionTable() {
38
39   size = 0;
40   entries = 0;
41   generation = 0;
42 }
43
44 TranspositionTable::~TranspositionTable() {
45
46   delete [] entries;
47 }
48
49
50 /// TranspositionTable::set_size sets the size of the transposition table,
51 /// measured in megabytes.
52
53 void TranspositionTable::set_size(size_t mbSize) {
54
55   size_t newSize = 1024;
56
57   // Transposition table consists of clusters and
58   // each cluster consists of ClusterSize number of TTEntries.
59   // Each non-empty entry contains information of exactly one position.
60   // newSize is the number of clusters we are going to allocate.
61   while ((2 * newSize) * sizeof(TTCluster) <= (mbSize << 20))
62       newSize *= 2;
63
64   if (newSize != size)
65   {
66       size = newSize;
67       delete [] entries;
68       entries = new TTCluster[size];
69       if (!entries)
70       {
71           std::cerr << "Failed to allocate " << mbSize
72                     << " MB for transposition table." << std::endl;
73           Application::exit_with_failure();
74       }
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 received?
83
84 void TranspositionTable::clear() {
85
86   memset(entries, 0, size * sizeof(TTCluster));
87 }
88
89
90 /// TranspositionTable::store writes a new entry containing position key and
91 /// valuable information of current position.
92 /// The Lowest order bits of position key are used to decide on which cluster
93 /// the position will be placed.
94 /// When a new entry is written and there are no empty entries available in cluster,
95 /// it replaces the least valuable of entries.
96 /// A TTEntry t1 is considered to be more valuable than a TTEntry t2 if t1 is from the
97 /// current search and t2 is from a previous search, or if the depth of t1
98 /// is bigger than the depth of t2.
99
100 void TranspositionTable::store(const Key posKey, Value v, ValueType t, Depth d, Move m, Value statV, Value kingD) {
101
102   int c1, c2, c3;
103   TTEntry *tte, *replace;
104   uint32_t posKey32 = posKey >> 32; // Use the high 32 bits as key
105
106   tte = replace = first_entry(posKey);
107   for (int i = 0; i < ClusterSize; i++, tte++)
108   {
109       if (!tte->key() || tte->key() == posKey32) // empty or overwrite old
110       {
111           // Preserve any existing ttMove
112           if (m == MOVE_NONE)
113               m = tte->move();
114
115           tte->save(posKey32, v, t, d, m, generation, statV, kingD);
116           return;
117       }
118
119       if (i == 0)  // Replacing first entry is default and already set before entering for-loop
120           continue;
121
122       c1 = (replace->generation() == generation ?  2 : 0);
123       c2 = (tte->generation() == generation ? -2 : 0);
124       c3 = (tte->depth() < replace->depth() ?  1 : 0);
125
126       if (c1 + c2 + c3 > 0)
127           replace = tte;
128   }
129   replace->save(posKey32, v, t, d, m, generation, statV, kingD);
130 }
131
132
133 /// TranspositionTable::retrieve looks up the current position in the
134 /// transposition table. Returns a pointer to the TTEntry or NULL
135 /// if position is not found.
136
137 TTEntry* TranspositionTable::retrieve(const Key posKey) const {
138
139   uint32_t posKey32 = posKey >> 32;
140   TTEntry* tte = first_entry(posKey);
141
142   for (int i = 0; i < ClusterSize; i++, tte++)
143       if (tte->key() == posKey32)
144           return tte;
145
146   return NULL;
147 }
148
149
150 /// TranspositionTable::new_search() is called at the beginning of every new
151 /// search. It increments the "generation" variable, which is used to
152 /// distinguish transposition table entries from previous searches from
153 /// entries from the current search.
154
155 void TranspositionTable::new_search() {
156   generation++;
157 }