io-chess
UCI chess engine
Loading...
Searching...
No Matches
TT.h
Go to the documentation of this file.
1#pragma once
2
11
12#include "../Types.h"
13#include <atomic>
14#include <cstdint>
15#include <optional>
16
21enum class Bound : uint8_t {
22 NONE = 0,
23 UPPER = 1,
24 LOWER = 2,
25 EXACT = 3,
27};
28
36struct TTEntry {
37 uint64_t data;
38 uint64_t signature;
39
47 bool probe(uint64_t key, uint64_t &out_data) const {
48 uint64_t d = __atomic_load_n(&data, __ATOMIC_RELAXED);
49 uint64_t s = __atomic_load_n(&signature, __ATOMIC_RELAXED);
50 if ((s ^ d) == key) {
51 out_data = d;
52 return true;
53 }
54 return false;
55 }
56
68 void save(uint64_t key, int16_t score, int16_t sEval, uint8_t depth,
69 Bound bound, uint16_t move, uint8_t age) {
70 uint64_t newData =
71 (static_cast<uint64_t>(move) << 48) |
72 (static_cast<uint64_t>(static_cast<uint16_t>(score)) << 32) |
73 (static_cast<uint64_t>(static_cast<uint16_t>(sEval)) << 16) |
74 (static_cast<uint64_t>(depth) << 8) |
75 (static_cast<uint64_t>(static_cast<uint8_t>(bound) & 0x7) << 5) |
76 (age & 0x1F);
77
78 __atomic_store_n(&signature, key ^ newData, __ATOMIC_RELAXED);
79 __atomic_store_n(&data, newData, __ATOMIC_RELAXED);
80 }
81
82 // Helpers for extracting fields
83 static uint16_t getMove(uint64_t d) { return static_cast<uint16_t>(d >> 48); }
84 static int16_t getScore(uint64_t d) { return static_cast<int16_t>((d >> 32) & 0xFFFF); }
85 static int16_t getStaticEval(uint64_t d) { return static_cast<int16_t>((d >> 16) & 0xFFFF); }
86 static uint8_t getDepth(uint64_t d) { return static_cast<uint8_t>((d >> 8) & 0xFF); }
87 static Bound getBound(uint64_t d) { return static_cast<Bound>((d >> 5) & 0x7); }
88 static uint8_t getAge(uint64_t d) { return static_cast<uint8_t>(d & 0x1F); }
89};
90
98struct alignas(16) EvalEntry {
99 std::atomic<uint64_t> payload;
100 std::atomic<uint64_t> signature;
101
105 void store(uint64_t key, int16_t score) {
106 uint64_t p = static_cast<uint64_t>(static_cast<uint16_t>(score));
107 uint64_t s = key ^ p;
108 signature.store(s, std::memory_order_relaxed);
109 payload.store(p, std::memory_order_relaxed);
110 }
111
115 bool probe(uint64_t key, int16_t &score) const {
116 uint64_t p = payload.load(std::memory_order_relaxed);
117 uint64_t s = signature.load(std::memory_order_relaxed);
118 if ((s ^ p) == key) {
119 score = static_cast<int16_t>(p & 0xFFFF);
120 return true;
121 }
122 return false;
123 }
124};
125
132struct alignas(64) Cluster {
135};
136static_assert(sizeof(Cluster) == 64, "Cluster must be exactly 64 bytes");
137
146private:
147 Cluster *table_ = nullptr;
148 size_t clusterCount_ = 0;
149 size_t indexMask_ = 0;
150 uint8_t currentAge_ = 0;
151 bool disableTT_ = false;
152 int numThreads_ = 1;
153 bool usingHugePages_ = false;
154
155public:
158
162 void resize(size_t mb);
163
167 void clear();
168
172 void newSearch() { currentAge_ = (currentAge_ + 1) & 0x1F; }
173
174 void setDisabled(bool disabled) { disableTT_ = disabled; }
175 void setNumThreads(int n) { numThreads_ = n; }
176
180 int hashfull() const;
181
185 void prefetch(uint64_t key) const;
186
192 bool probe(uint64_t key, int depth, int &score, Move &bestMove,
193 int &entryDepth, Bound &entryBound) const;
194
198 Move probeMove(uint64_t key) const;
199
203 std::optional<int> probeScore(uint64_t key) const;
204
208 bool probeEval(uint64_t key, int16_t &score) const;
209
213 void storeEval(uint64_t key, int16_t score);
214
220 void store(uint64_t key, int depth, int score, Bound bound, Move bestMove,
221 int staticEval = 0);
222
223 // Persistence and debugging
224 void dumpToFile(const std::string &filename) const;
225 bool saveFullToFile(const std::string &filename) const;
226 bool loadFullFromFile(const std::string &filename);
227
228private:
229 size_t index(uint64_t key) const { return key & indexMask_; }
230 int ageDiff(uint8_t entryAge) const {
231 return (currentAge_ >= entryAge) ? (currentAge_ - entryAge)
232 : (32 - entryAge + currentAge_);
233 }
234};
Bound
Represents the type of score stored in the Transposition Table.
Definition TT.h:21
@ LAZY_MASK
Flag indicating the entry was populated via lazy evaluation.
Definition TT.h:26
@ UPPER
Score is an upper bound (failed low).
Definition TT.h:23
@ LOWER
Score is a lower bound (failed high).
Definition TT.h:24
@ NONE
Invalid or empty bound.
Definition TT.h:22
@ EXACT
Score is exact (from a PV node).
Definition TT.h:25
Common type definitions and constants for the chess engine.
chess::Move Move
Alias for chess::Move.
Definition Types.h:15
size_t clusterCount_
Number of available clusters.
Definition TT.h:148
int hashfull() const
Calculates the utilization of the Transposition Table (per-mille).
Definition TT.cpp:358
bool loadFullFromFile(const std::string &filename)
Definition TT.cpp:449
std::optional< int > probeScore(uint64_t key) const
Lightly probes the TT to extract only the score (for pruning heuristics).
Definition TT.cpp:219
size_t index(uint64_t key) const
Definition TT.h:229
void store(uint64_t key, int depth, int score, Bound bound, Move bestMove, int staticEval=0)
Stores search results into the best slot in the cluster.
Definition TT.cpp:283
void clear()
Clears the contents of the Transposition Table.
Definition TT.cpp:119
Move probeMove(uint64_t key) const
Lightly probes the TT just to extract the best move (for move ordering).
Definition TT.cpp:189
uint8_t currentAge_
Current generation age (used to age out old entries).
Definition TT.h:150
bool usingHugePages_
True if the memory was allocated via HugePages.
Definition TT.h:153
void prefetch(uint64_t key) const
Issues a memory prefetch instruction for the cluster corresponding to the key.
Definition TT.cpp:126
~TranspositionTable()
Definition TT.cpp:42
void setDisabled(bool disabled)
Definition TT.h:174
bool probeEval(uint64_t key, int16_t &score) const
Probes the dedicated static evaluation slot.
Definition TT.cpp:239
void dumpToFile(const std::string &filename) const
Definition TT.cpp:379
void storeEval(uint64_t key, int16_t score)
Stores a static evaluation in the dedicated slot.
Definition TT.cpp:276
void resize(size_t mb)
Resizes the Transposition Table to the specified capacity in Megabytes.
Definition TT.cpp:56
void setNumThreads(int n)
Definition TT.h:175
bool probe(uint64_t key, int depth, int &score, Move &bestMove, int &entryDepth, Bound &entryBound) const
Fully probes the TT for a search entry matching the key.
Definition TT.cpp:132
void newSearch()
Increments the internal age generation to age out older entries.
Definition TT.h:172
bool saveFullToFile(const std::string &filename) const
Definition TT.cpp:409
int numThreads_
Number of threads sharing the table.
Definition TT.h:152
bool disableTT_
Flag to disable the TT entirely.
Definition TT.h:151
Cluster * table_
Pointer to the heap-allocated clusters.
Definition TT.h:147
int ageDiff(uint8_t entryAge) const
Definition TT.h:230
size_t indexMask_
Bitmask for fast modulo addressing.
Definition TT.h:149
TranspositionTable()=default
64-byte cache-line aligned structure holding TT entries.
Definition TT.h:132
EvalEntry eval
Dedicated static evaluation entry.
Definition TT.h:134
TTEntry entries[3]
Search entries.
Definition TT.h:133
16-byte entry dedicated exclusively to caching static evaluations.
Definition TT.h:98
std::atomic< uint64_t > signature
Checksum signature.
Definition TT.h:100
void store(uint64_t key, int16_t score)
Stores the static evaluation atomically.
Definition TT.h:105
bool probe(uint64_t key, int16_t &score) const
Probes for a cached static evaluation.
Definition TT.h:115
std::atomic< uint64_t > payload
Packed static evaluation score.
Definition TT.h:99
16-byte Transposition Table entry for search results.
Definition TT.h:36
static int16_t getStaticEval(uint64_t d)
Definition TT.h:85
static Bound getBound(uint64_t d)
Definition TT.h:87
static uint8_t getDepth(uint64_t d)
Definition TT.h:86
static int16_t getScore(uint64_t d)
Definition TT.h:84
static uint8_t getAge(uint64_t d)
Definition TT.h:88
uint64_t signature
Checksum signature (Key ^ Data).
Definition TT.h:38
uint64_t data
Packed search data.
Definition TT.h:37
void save(uint64_t key, int16_t score, int16_t sEval, uint8_t depth, Bound bound, uint16_t move, uint8_t age)
Atomically stores search data into the entry.
Definition TT.h:68
static uint16_t getMove(uint64_t d)
Definition TT.h:83
bool probe(uint64_t key, uint64_t &out_data) const
Probes the entry to safely retrieve data if the key matches.
Definition TT.h:47