-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCTranspositionTable.cs
177 lines (158 loc) · 3.65 KB
/
CTranspositionTable.cs
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using Color = System.Int32;
using Move = System.Int32;
using Square = System.Int32;
using Bitboard = System.UInt64;
using Hash = System.UInt64;
namespace NSRapchess
{
public enum RecType : byte
{
invalid,
alpha,
beta,
exact
}
public struct CRec
{
public ulong hash;//8
public int value;//4
public int move;//4
public byte depth;//1
public byte age;//1
public RecType type;//1
public CRec(ulong hash, int depth, int move, int value, RecType type, byte age)
{
this.hash = hash;
this.depth = (byte)depth;
this.move = move;
this.value = value;
this.type = type;
this.age = age;
}
}
internal class CTranspositionTable
{
static byte generation = 0;
static ulong used = 0;
public const int DEFAULT_SIZE_MB = 20;
static ulong hashMask;
static CRec[] table;
static CRec empty = new CRec();
readonly static int clusterShift = 2;
public static int clusterSize = 1 << clusterShift;
readonly static int clusterMask = clusterSize - 1;
static CTranspositionTable()
{
Resize(DEFAULT_SIZE_MB);
}
public static void Resize(int hashSizeMBytes)
{
int sizeRec = System.Runtime.InteropServices.Marshal.SizeOf(typeof(CRec));
int mb = 1 << 20;
int length = 0x100;
while ((length * sizeRec) < (hashSizeMBytes * mb))
length <<= 1;
table = new CRec[length--];
hashMask = (ulong)(length & ~clusterMask);
used = 0;
}
public static void Clear()
{
used = 0;
generation = 0;
Array.Clear(table, 0, table.Length);
}
public static void NextGeneration()
{
if (++generation == 0)
Clear();
}
public static int Permill()
{
return (int)(used * 1000ul / (ulong)table.Length);
}
public static void GetRecExact(ulong hash, out Move move)
{
int iStart = (int)(hash & hashMask);
for (int n = 0; n < clusterSize; n++)
{
CRec rec = table[iStart + n];
if ((rec.hash == hash) && (rec.type == RecType.exact))
{
move = rec.move;
return;
}
if (rec.type == RecType.invalid) break;
}
move = 0;
}
public static void GetRec(ulong hash, int halfMove, int depth, out CRec rec)
{
int iStart = (int)(hash & hashMask);
for (int n = 0; n < clusterSize; n++)
{
rec = table[iStart + n];
if ((rec.hash == hash) && (rec.age >= halfMove) && (rec.depth >= depth))
return;
}
rec = empty;
}
public static void GetRec(ulong hash, int depth, out CRec rec)
{
int iStart = (int)(hash & hashMask);
for (int n = 0; n < clusterSize; n++)
{
rec = table[iStart + n];
if (rec.hash == hash)
{
if (rec.depth < depth)
rec = empty;
else
table[iStart + n].age = generation;
return;
}
}
rec = empty;
}
public static void GetRec(ulong hash, out CRec rec)
{
int iStart = (int)(hash & hashMask);
for (int n = 0; n < clusterSize; n++)
{
rec = table[iStart + n];
if (rec.hash == hash)
return;
}
rec = empty;
}
public static void SetRec(ulong hash, int depth, int bestMove, int bestValue, RecType rt)
{
CRec rec = new CRec(hash, depth, bestMove, bestValue, rt, generation);
int iStart = (int)(rec.hash & hashMask);
ref CRec rep = ref table[iStart];
for (int n = 0; n < clusterSize; n++)
{
ref CRec cur = ref table[iStart + n];
if (cur.type == RecType.invalid)
{
used++;
cur = rec;
return;
}
if (cur.hash == rec.hash)
{
if (rec.move == 0)
rec.move = cur.move;
cur = rec;
return;
}
if (((cur.age == generation || cur.type == RecType.exact) ? 1 : 0)
- ((rep.age == generation) ? 1 : 0)
- ((cur.depth < rep.depth) ? 1 : 0) < 0)
rep = cur;
}
rep = rec;
}
}
}