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);
68 void save(uint64_t key, int16_t score, int16_t sEval, uint8_t depth,
69 Bound bound, uint16_t move, uint8_t age) {
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) |
78 __atomic_store_n(&
signature, key ^ newData, __ATOMIC_RELAXED);
79 __atomic_store_n(&
data, newData, __ATOMIC_RELAXED);
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); }
88 static uint8_t
getAge(uint64_t d) {
return static_cast<uint8_t
>(d & 0x1F); }
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);
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);
136static_assert(
sizeof(
Cluster) == 64,
"Cluster must be exactly 64 bytes");
192 bool probe(uint64_t key,
int depth,
int &score,
Move &bestMove,
193 int &entryDepth,
Bound &entryBound)
const;
203 std::optional<int>
probeScore(uint64_t key)
const;
208 bool probeEval(uint64_t key, int16_t &score)
const;
213 void storeEval(uint64_t key, int16_t score);
220 void store(uint64_t key,
int depth,
int score,
Bound bound,
Move bestMove,
224 void dumpToFile(
const std::string &filename)
const;
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