Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/Stockfish
Path: blob/master/src/tt.cpp
627 views
1
/*
2
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3
Copyright (C) 2004-2026 The Stockfish developers (see AUTHORS file)
4
5
Stockfish 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
Stockfish 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
#include "tt.h"
20
21
#include <cassert>
22
#include <cstdint>
23
#include <cstdlib>
24
#include <cstring>
25
#include <iostream>
26
27
#include "memory.h"
28
#include "misc.h"
29
#include "syzygy/tbprobe.h"
30
#include "thread.h"
31
32
namespace Stockfish {
33
34
35
// TTEntry struct is the 10 bytes transposition table entry, defined as below:
36
//
37
// key 16 bit
38
// depth 8 bit
39
// generation 5 bit
40
// pv node 1 bit
41
// bound type 2 bit
42
// move 16 bit
43
// value 16 bit
44
// evaluation 16 bit
45
//
46
// These fields are in the same order as accessed by TT::probe(), since memory is fastest sequentially.
47
// Equally, the store order in save() matches this order.
48
49
struct TTEntry {
50
51
// Convert internal bitfields to external types
52
TTData read() const {
53
return TTData{Move(move16), Value(value16),
54
Value(eval16), Depth(depth8 + DEPTH_ENTRY_OFFSET),
55
Bound(genBound8 & 0x3), bool(genBound8 & 0x4)};
56
}
57
58
bool is_occupied() const;
59
void save(Key k, Value v, bool pv, Bound b, Depth d, Move m, Value ev, uint8_t generation8);
60
// The returned age is a multiple of TranspositionTable::GENERATION_DELTA
61
uint8_t relative_age(const uint8_t generation8) const;
62
63
private:
64
friend class TranspositionTable;
65
66
uint16_t key16;
67
uint8_t depth8;
68
uint8_t genBound8;
69
Move move16;
70
int16_t value16;
71
int16_t eval16;
72
};
73
74
// `genBound8` is where most of the details are. We use the following constants to manipulate 5 leading generation bits
75
// and 3 trailing miscellaneous bits.
76
77
// These bits are reserved for other things.
78
static constexpr unsigned GENERATION_BITS = 3;
79
// increment for generation field
80
static constexpr int GENERATION_DELTA = (1 << GENERATION_BITS);
81
// cycle length
82
static constexpr int GENERATION_CYCLE = 255 + GENERATION_DELTA;
83
// mask to pull out generation number
84
static constexpr int GENERATION_MASK = (0xFF << GENERATION_BITS) & 0xFF;
85
86
// DEPTH_ENTRY_OFFSET exists because 1) we use `bool(depth8)` as the occupancy check, but
87
// 2) we need to store negative depths for QS. (`depth8` is the only field with "spare bits":
88
// we sacrifice the ability to store depths greater than 1<<8 less the offset, as asserted in `save`.)
89
bool TTEntry::is_occupied() const { return bool(depth8); }
90
91
// Populates the TTEntry with a new node's data, possibly
92
// overwriting an old position. The update is not atomic and can be racy.
93
void TTEntry::save(
94
Key k, Value v, bool pv, Bound b, Depth d, Move m, Value ev, uint8_t generation8) {
95
96
// Preserve the old ttmove if we don't have a new one
97
if (m || uint16_t(k) != key16)
98
move16 = m;
99
100
// Overwrite less valuable entries (cheapest checks first)
101
if (b == BOUND_EXACT || uint16_t(k) != key16 || d - DEPTH_ENTRY_OFFSET + 2 * pv > depth8 - 4
102
|| relative_age(generation8))
103
{
104
assert(d > DEPTH_ENTRY_OFFSET);
105
assert(d < 256 + DEPTH_ENTRY_OFFSET);
106
107
key16 = uint16_t(k);
108
depth8 = uint8_t(d - DEPTH_ENTRY_OFFSET);
109
genBound8 = uint8_t(generation8 | uint8_t(pv) << 2 | b);
110
value16 = int16_t(v);
111
eval16 = int16_t(ev);
112
}
113
}
114
115
116
uint8_t TTEntry::relative_age(const uint8_t generation8) const {
117
// Due to our packed storage format for generation and its cyclic
118
// nature we add GENERATION_CYCLE (256 is the modulus, plus what
119
// is needed to keep the unrelated lowest n bits from affecting
120
// the result) to calculate the entry age correctly even after
121
// generation8 overflows into the next cycle.
122
return (GENERATION_CYCLE + generation8 - genBound8) & GENERATION_MASK;
123
}
124
125
126
// TTWriter is but a very thin wrapper around the pointer
127
TTWriter::TTWriter(TTEntry* tte) :
128
entry(tte) {}
129
130
void TTWriter::write(
131
Key k, Value v, bool pv, Bound b, Depth d, Move m, Value ev, uint8_t generation8) {
132
entry->save(k, v, pv, b, d, m, ev, generation8);
133
}
134
135
136
// A TranspositionTable is an array of Cluster, of size clusterCount. Each cluster consists of ClusterSize number
137
// of TTEntry. Each non-empty TTEntry contains information on exactly one position. The size of a Cluster should
138
// divide the size of a cache line for best performance, as the cacheline is prefetched when possible.
139
140
static constexpr int ClusterSize = 3;
141
142
struct Cluster {
143
TTEntry entry[ClusterSize];
144
char padding[2]; // Pad to 32 bytes
145
};
146
147
static_assert(sizeof(Cluster) == 32, "Suboptimal Cluster size");
148
149
150
// Sets the size of the transposition table,
151
// measured in megabytes. Transposition table consists
152
// of clusters and each cluster consists of ClusterSize number of TTEntry.
153
void TranspositionTable::resize(size_t mbSize, ThreadPool& threads) {
154
aligned_large_pages_free(table);
155
156
clusterCount = mbSize * 1024 * 1024 / sizeof(Cluster);
157
158
table = static_cast<Cluster*>(aligned_large_pages_alloc(clusterCount * sizeof(Cluster)));
159
160
if (!table)
161
{
162
std::cerr << "Failed to allocate " << mbSize << "MB for transposition table." << std::endl;
163
exit(EXIT_FAILURE);
164
}
165
166
clear(threads);
167
}
168
169
170
// Initializes the entire transposition table to zero,
171
// in a multi-threaded way.
172
void TranspositionTable::clear(ThreadPool& threads) {
173
generation8 = 0;
174
const size_t threadCount = threads.num_threads();
175
176
for (size_t i = 0; i < threadCount; ++i)
177
{
178
threads.run_on_thread(i, [this, i, threadCount]() {
179
// Each thread will zero its part of the hash table
180
const size_t stride = clusterCount / threadCount;
181
const size_t start = stride * i;
182
const size_t len = i + 1 != threadCount ? stride : clusterCount - start;
183
184
std::memset(&table[start], 0, len * sizeof(Cluster));
185
});
186
}
187
188
for (size_t i = 0; i < threadCount; ++i)
189
threads.wait_on_thread(i);
190
}
191
192
193
// Returns an approximation of the hashtable
194
// occupation during a search. The hash is x permill full, as per UCI protocol.
195
// Only counts entries which match the current generation.
196
int TranspositionTable::hashfull(int maxAge) const {
197
int maxAgeInternal = maxAge << GENERATION_BITS;
198
int cnt = 0;
199
for (int i = 0; i < 1000; ++i)
200
for (int j = 0; j < ClusterSize; ++j)
201
cnt += table[i].entry[j].is_occupied()
202
&& table[i].entry[j].relative_age(generation8) <= maxAgeInternal;
203
204
return cnt / ClusterSize;
205
}
206
207
208
void TranspositionTable::new_search() {
209
// increment by delta to keep lower bits as is
210
generation8 += GENERATION_DELTA;
211
}
212
213
214
uint8_t TranspositionTable::generation() const { return generation8; }
215
216
217
// Looks up the current position in the transposition
218
// table. It returns true if the position is found.
219
// Otherwise, it returns false and a pointer to an empty or least valuable TTEntry
220
// to be replaced later. The replace value of an entry is calculated as its depth
221
// minus 8 times its relative age. TTEntry t1 is considered more valuable than
222
// TTEntry t2 if its replace value is greater than that of t2.
223
std::tuple<bool, TTData, TTWriter> TranspositionTable::probe(const Key key) const {
224
225
TTEntry* const tte = first_entry(key);
226
const uint16_t key16 = uint16_t(key); // Use the low 16 bits as key inside the cluster
227
228
for (int i = 0; i < ClusterSize; ++i)
229
if (tte[i].key16 == key16)
230
// This gap is the main place for read races.
231
// After `read()` completes that copy is final, but may be self-inconsistent.
232
return {tte[i].is_occupied(), tte[i].read(), TTWriter(&tte[i])};
233
234
// Find an entry to be replaced according to the replacement strategy
235
TTEntry* replace = tte;
236
for (int i = 1; i < ClusterSize; ++i)
237
if (replace->depth8 - replace->relative_age(generation8)
238
> tte[i].depth8 - tte[i].relative_age(generation8))
239
replace = &tte[i];
240
241
return {false,
242
TTData{Move::none(), VALUE_NONE, VALUE_NONE, DEPTH_ENTRY_OFFSET, BOUND_NONE, false},
243
TTWriter(replace)};
244
}
245
246
247
TTEntry* TranspositionTable::first_entry(const Key key) const {
248
return &table[mul_hi64(key, clusterCount)].entry[0];
249
}
250
251
} // namespace Stockfish
252
253