Repository navigation
Expand file tree
/
Copy pathtransposition.cpp
More file actions
88 lines (67 loc) · 2 KB
/
Copy pathtransposition.cpp
File metadata and controls
88 lines (67 loc) · 2 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// Created by Ashish Kumar Nanda on 8/11/26.
//
#include "transposition.h"
#include <vector>
#include <algorithm>
#include "eval.h"
constexpr size_t TT_ENTRIES = 1ULL << 22; // 64 MB
static std::vector<TTEntry> table;
void tt_init() {
table.assign(TT_ENTRIES, TTEntry{});
}
void tt_clear() {
std::ranges::fill(table, TTEntry{});
}
static uint64_t slot(const uint64_t hash) {
return hash & (TT_ENTRIES - 1);
}
bool tt_probe(const uint64_t hash, const int depth, const int ply, const int alpha, const int beta, int &out_score, Move &out_move) {
const TTEntry entry = table[slot(hash)];
out_move = NO_MOVE;
if (entry.key != hash) {
return false; //this position isn't stored or relevant
}
int s = entry.score;
if (s >= MATE_BOUND) s -= ply;
else if (s <= -MATE_BOUND) s += ply;
out_move = entry.best_move; //position is stored, so we have a best_move no matter what
if (entry.depth < depth) {
//insufficient depth, exit with just best_move (ordering reccommendation)
return false;
}
if (entry.bound == BOUND_EXACT) {
out_score = s;
return true;
}
if (entry.bound == BOUND_LOWER && entry.score >= beta) {
out_score = s;
return true;
}
if (entry.bound == BOUND_UPPER && entry.score <= alpha) {
out_score = s;
return true;
}
return false;
}
void tt_store(const uint64_t hash, const int depth, const int ply, int score, const Move best_move, const int alpha_original, const int beta) {
Bound bound;
if (score >= beta) {
bound = BOUND_LOWER;
}
else if (score <= alpha_original) {
bound = BOUND_UPPER;
}
else {
bound = BOUND_EXACT;
}
if (score >= MATE_BOUND) score += ply;
else if (score <= -MATE_BOUND) score -= ply;
TTEntry entry = table[slot(hash)];
entry.key = hash;
entry.bound = bound;
entry.depth = depth;
entry.score = score;
entry.best_move = best_move;
table[slot(hash)] = entry;
}