-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtt.cpp
More file actions
53 lines (49 loc) · 1.41 KB
/
tt.cpp
File metadata and controls
53 lines (49 loc) · 1.41 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
#include "tt.hpp"
void TranspositionTable::newSearch()
{
time = 0;
}
void TranspositionTable::store(uint64_t hash, chess::Move best, int16_t score, int8_t depth, TTFlag flag)
{
// 2 entries per bucket
if (buckets == 0)
return;
uint64_t index = hash % buckets;
if (index >= buckets - 1)
index = buckets - 2; // Ensure we don't overflow
TTEntry &e0 = table[index], &e1 = table[index + 1];
// Store the entry
++time; // monotonically increasing stamp
for (TTEntry *e : {&e0, &e1})
{
if (!e->valid() || e->hash == hash || e->depth() < depth)
{
e->hash = hash;
e->set(depth, flag, score, time, true, best);
return;
}
}
// If we get here, we need to evict an entry
// Find the oldest entry
TTEntry *oldest = (e0.timestamp() < e1.timestamp()) ? &e0 : &e1;
// Evict it
oldest->hash = hash;
oldest->set(depth, flag, score, time, true, best);
}
TTEntry *TranspositionTable::lookup(uint64_t hash)
{
// 2 entries per bucket
if (buckets == 0)
return nullptr;
uint64_t index = hash % buckets;
if (index >= buckets - 1)
index = buckets - 2; // Ensure we don't overflow
TTEntry &e0 = table[index], &e1 = table[index + 1];
// Check the entries
for (TTEntry *e : {&e0, &e1})
{
if (e->valid() && e->hash == hash)
return e;
}
return nullptr;
}