-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtt.cpp
More file actions
58 lines (53 loc) · 1.67 KB
/
Copy pathtt.cpp
File metadata and controls
58 lines (53 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "tt.hpp"
const int8_t depthMargin=2;
TranspositionTable::TranspositionTable(size_t sizePow2)
: sizeMask{(1ULL << sizePow2) - 1},
entryCount(1ULL << sizePow2),
currentTime(0)
{
table = (TTEntry*)malloc(entryCount * 2 * sizeof(TTEntry)); // 2 entries per bucket
clear();
}
TranspositionTable::~TranspositionTable() {
free(this->table);
}
void TranspositionTable::clear() {
std::memset(table, 0, sizeof(TTEntry) * entryCount*2);
}
void TranspositionTable::newSearch() {
currentTime++;
}
void TranspositionTable::store(uint64_t hash, const chess::Move& bestMove, int16_t score, uint8_t depth, TTFlag flag) {
size_t index = (hash & sizeMask) * 2; // Two entries per bucket
TTEntry& e0 = table[index];
TTEntry& e1 = table[index + 1];
// Replace exact match or shallower
for (TTEntry* e : {&e0, &e1}) {
if (!e->valid() || e->hash == hash || e->depth()-depthMargin < depth) {
e->hash = hash;
e->bestMove = bestMove;
e->set(depth, flag, score, currentTime, true);
return;
}
}
// No match: pick the one with oldest timestamp
TTEntry* victim = (e0.timestamp() <= e1.timestamp()) ? &e0 : &e1;
victim->hash = hash;
victim->bestMove = bestMove;
victim->set(depth, flag, score, currentTime, true);
}
TTEntry* TranspositionTable::probe(uint64_t hash) {
size_t index = (hash & sizeMask) * 2;
TTEntry& e0 = table[index];
TTEntry& e1 = table[index + 1];
if (e0.valid() && e0.hash == hash) {
++tthits;
return &e0;
}
if (e1.valid() && e1.hash == hash) {
++tthits;
return &e1;
}
++ttmiss;
return nullptr;
}