Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/Stockfish
Path: blob/master/src/tt.cpp
376 views
1
/*
2
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3
Copyright (C) 2004-2025 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
else if (depth8 + DEPTH_ENTRY_OFFSET >= 5 && Bound(genBound8 & 0x3) != BOUND_EXACT)
114
depth8--;
115
}
116
117
118
uint8_t TTEntry::relative_age(const uint8_t generation8) const {
119
// Due to our packed storage format for generation and its cyclic
120
// nature we add GENERATION_CYCLE (256 is the modulus, plus what
121
// is needed to keep the unrelated lowest n bits from affecting
122
// the result) to calculate the entry age correctly even after
123
// generation8 overflows into the next cycle.
124
return (GENERATION_CYCLE + generation8 - genBound8) & GENERATION_MASK;
125
}
126
127
128
// TTWriter is but a very thin wrapper around the pointer
129
TTWriter::TTWriter(TTEntry* tte) :
130
entry(tte) {}
131
132
void TTWriter::write(
133
Key k, Value v, bool pv, Bound b, Depth d, Move m, Value ev, uint8_t generation8) {
134
entry->save(k, v, pv, b, d, m, ev, generation8);
135
}
136
137
138
// A TranspositionTable is an array of Cluster, of size clusterCount. Each cluster consists of ClusterSize number
139
// of TTEntry. Each non-empty TTEntry contains information on exactly one position. The size of a Cluster should
140
// divide the size of a cache line for best performance, as the cacheline is prefetched when possible.
141
142
static constexpr int ClusterSize = 3;
143
144
struct Cluster {
145
TTEntry entry[ClusterSize];
146
char padding[2]; // Pad to 32 bytes
147
};
148
149
static_assert(sizeof(Cluster) == 32, "Suboptimal Cluster size");
150
151
152
// Sets the size of the transposition table,
153
// measured in megabytes. Transposition table consists
154
// of clusters and each cluster consists of ClusterSize number of TTEntry.
155
void TranspositionTable::resize(size_t mbSize, ThreadPool& threads) {
156
aligned_large_pages_free(table);
157
158
clusterCount = mbSize * 1024 * 1024 / sizeof(Cluster);
159
160
table = static_cast<Cluster*>(aligned_large_pages_alloc(clusterCount * sizeof(Cluster)));
161
162
if (!table)
163
{
164
std::cerr << "Failed to allocate " << mbSize << "MB for transposition table." << std::endl;
165
exit(EXIT_FAILURE);
166
}
167
168
clear(threads);
169
}
170
171
172
// Initializes the entire transposition table to zero,
173
// in a multi-threaded way.
174
void TranspositionTable::clear(ThreadPool& threads) {
175
generation8 = 0;
176
const size_t threadCount = threads.num_threads();
177
178
for (size_t i = 0; i < threadCount; ++i)
179
{
180
threads.run_on_thread(i, [this, i, threadCount]() {
181
// Each thread will zero its part of the hash table
182
const size_t stride = clusterCount / threadCount;
183
const size_t start = stride * i;
184
const size_t len = i + 1 != threadCount ? stride : clusterCount - start;
185
186
std::memset(&table[start], 0, len * sizeof(Cluster));
187
});
188
}
189
190
for (size_t i = 0; i < threadCount; ++i)
191
threads.wait_on_thread(i);
192
}
193
194
195
// Returns an approximation of the hashtable
196
// occupation during a search. The hash is x permill full, as per UCI protocol.
197
// Only counts entries which match the current generation.
198
int TranspositionTable::hashfull(int maxAge) const {
199
int maxAgeInternal = maxAge << GENERATION_BITS;
200
int cnt = 0;
201
for (int i = 0; i < 1000; ++i)
202
for (int j = 0; j < ClusterSize; ++j)
203
cnt += table[i].entry[j].is_occupied()
204
&& table[i].entry[j].relative_age(generation8) <= maxAgeInternal;
205
206
return cnt / ClusterSize;
207
}
208
209
210
void TranspositionTable::new_search() {
211
// increment by delta to keep lower bits as is
212
generation8 += GENERATION_DELTA;
213
}
214
215
216
uint8_t TranspositionTable::generation() const { return generation8; }
217
218
219
// Looks up the current position in the transposition
220
// table. It returns true if the position is found.
221
// Otherwise, it returns false and a pointer to an empty or least valuable TTEntry
222
// to be replaced later. The replace value of an entry is calculated as its depth
223
// minus 8 times its relative age. TTEntry t1 is considered more valuable than
224
// TTEntry t2 if its replace value is greater than that of t2.
225
std::tuple<bool, TTData, TTWriter> TranspositionTable::probe(const Key key) const {
226
227
TTEntry* const tte = first_entry(key);
228
const uint16_t key16 = uint16_t(key); // Use the low 16 bits as key inside the cluster
229
230
for (int i = 0; i < ClusterSize; ++i)
231
if (tte[i].key16 == key16)
232
// This gap is the main place for read races.
233
// After `read()` completes that copy is final, but may be self-inconsistent.
234
return {tte[i].is_occupied(), tte[i].read(), TTWriter(&tte[i])};
235
236
// Find an entry to be replaced according to the replacement strategy
237
TTEntry* replace = tte;
238
for (int i = 1; i < ClusterSize; ++i)
239
if (replace->depth8 - replace->relative_age(generation8)
240
> tte[i].depth8 - tte[i].relative_age(generation8))
241
replace = &tte[i];
242
243
return {false,
244
TTData{Move::none(), VALUE_NONE, VALUE_NONE, DEPTH_ENTRY_OFFSET, BOUND_NONE, false},
245
TTWriter(replace)};
246
}
247
248
249
TTEntry* TranspositionTable::first_entry(const Key key) const {
250
return &table[mul_hi64(key, clusterCount)].entry[0];
251
}
252
253
} // namespace Stockfish
254
255