-
Notifications
You must be signed in to change notification settings - Fork 8
/
hash.h
151 lines (125 loc) · 3.23 KB
/
hash.h
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
Fire is a freeware UCI chess playing engine authored by Norman Schmidt.
Fire utilizes many state-of-the-art chess programming ideas and techniques
which have been documented in detail at https://www.chessprogramming.org/
and demonstrated via the very strong open-source chess engine Stockfish...
https://github.com/official-stockfish/Stockfish.
Fire is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or any later version.
You should have received a copy of the GNU General Public License with
this program: copying.txt. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "define.h"
#include "fire.h"
#include "thread.h"
enum hash_flags : uint8_t
{
no_limit,
threat_white = 1,
threat_black = 2,
in_use = 4,
south_border = 64,
north_border = 128,
exact_value = north_border | south_border
};
const uint8_t age_mask = 0x38;
const uint8_t flags_mask = 0xc7;
const uint8_t threat_mask = 0x03;
const uint8_t use_mask = 0xfb;
struct main_hash_entry
{
[[nodiscard]] uint32_t move() const
{
return static_cast<uint32_t>(move_);
}
[[nodiscard]] int value() const
{
return value_;
}
[[nodiscard]] int eval() const
{
return eval_;
}
[[nodiscard]] int depth() const
{
return depth_ * static_cast<int>(plies) + plies - 1;
}
[[nodiscard]] hash_flags bounds() const
{
return static_cast<hash_flags>(flags_ & exact_value);
}
[[nodiscard]] hash_flags threat() const
{
return static_cast<hash_flags>(flags_ & threat_mask);
}
void save(const uint64_t k, const int val, const uint8_t flags, const int d, const uint32_t z, const int eval, const uint8_t gen)
{
const auto dd = d / plies;
const uint16_t k16 = k >> 48;
if (z || k16 != key_)
move_ = static_cast<uint16_t>(z);
if (k16 != key_
|| dd > depth_ - 4
|| (flags & exact_value) == exact_value)
{
key_ = k16;
value_ = static_cast<int16_t>(val);
eval_ = static_cast<int16_t>(eval);
flags_ = static_cast<uint8_t>(gen + flags);
depth_ = static_cast<int8_t>(dd);
}
}
private:
friend class hash;
uint16_t key_;
int8_t depth_;
uint8_t flags_;
int16_t value_;
int16_t eval_;
uint16_t move_;
};
class hash
{
static constexpr int cache_line = 64;
static constexpr int bucket_size = 3;
struct bucket
{
main_hash_entry entry[bucket_size];
char padding[2];
};
static_assert(cache_line % sizeof(bucket) == 0, "Cluster size incorrect");
public:
~hash()
{
free(hash_mem_);
}
void new_age()
{
age_ = age_ + 8 & age_mask;
}
[[nodiscard]] uint8_t age() const
{
return age_;
}
[[nodiscard]] main_hash_entry* probe(uint64_t key) const;
[[nodiscard]] main_hash_entry* replace(uint64_t key) const;
[[nodiscard]] int hash_full() const;
void init(size_t mb_size);
void clear() const;
[[nodiscard]] inline main_hash_entry* entry(const uint64_t key) const
{
return reinterpret_cast<main_hash_entry*>(reinterpret_cast<char*>(hash_mem_) + (key & bucket_mask_));
}
inline void prefetch_entry(const uint64_t key) const
{
prefetch(entry(key));
}
private:
size_t buckets_ = 0;
size_t bucket_mask_ = 0;
bucket* hash_mem_ = nullptr;
uint8_t age_ = 0;
};
extern hash main_hash;