-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhash.c
316 lines (242 loc) · 7.97 KB
/
hash.c
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
MIT License
Copyright (c) 2000 Adrien M. Regimbald
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/**************************************************
* Faile version 1.4 *
* Author: Adrien Regimbald *
* E-mail: [email protected] *
* Web Page: http://www.ee.ualberta.ca/~adrien/ *
* *
* File: hash.c *
* Purpose: contains functions needed for hashing *
**************************************************/
#include "faile.h"
#include "extvars.h"
#include "protos.h"
#include "rand.h"
d_long compute_hash (void) {
/* compute and return the initial hash value for the position */
d_long this_pos = {0, 0};
int i, square;
/* loop through the board, adding up piece values: */
for (i = 1; i <= num_pieces; i++) {
square = pieces[i];
if (square)
xor (&this_pos, h_values[board[square]][square]);
}
/* add the ep hash value: */
xor (&this_pos, ep_h_values[ep_square]);
/* add the castling hash values: */
xor (&this_pos, wck_h_values[!moved[30] && !moved[33]]);
xor (&this_pos, wcq_h_values[!moved[30] && !moved[26]]);
xor (&this_pos, bck_h_values[!moved[114] && !moved[117]]);
xor (&this_pos, bcq_h_values[!moved[114] && !moved[110]]);
/* add the hash value for side to move: */
xor (&this_pos, color_h_values[white_to_move]);
return (this_pos);
}
void hash_to_pv (int depth) {
/* try to extract the PV from hash info */
hash_s *hash_p;
d_long temp_hash, hash;
int ep_temp;
move_s move, ign_me;
char str_move[6];
if (!depth)
return;
/* lookup our hash: */
hash_p = hash_table + (hash_mask & cur_pos.x1);
hash = hash_p->hash;
if (hash.x1 == cur_pos.x1 && hash.x2 == cur_pos.x2) {
move = hash_p->move;
comp_to_coord (move, str_move);
if (verify_coord (str_move, &ign_me)) {
pv[1][i_depth-depth+1] = move;
pv_length[1] = i_depth-depth+2;
temp_hash = cur_pos;
ep_temp = ep_square;
make (&move, 0);
ply++;
if (check_legal (&move, 0)) {
hash_to_pv (depth-1);
}
ply--;
unmake (&move, 0);
ep_square = ep_temp;
cur_pos = temp_hash;
}
}
}
void init_hash_tables (void) {
/* this function allocates space for the hash tables, as well as setting
some necessary values for storing hashes */
int i = 1;
unsigned long int max_hash = 1, element_size = sizeof (hash_s);
/* see if user specified a hash size, otherwise use default: */
if (!hash_max_mb)
hash_max_mb = HASH_MB;
/* compute the maximum hash element, based upon size desired: */
while (max_hash * element_size <= hash_max_mb<<19) {
max_hash = 1 << i++;
}
/* compute the hash mask, for computing hash table indices: */
hash_mask = max_hash-1;
/* allocate our hash table's memory, and report on the allocation: */
if ((hash_table = malloc (max_hash*element_size)) == NULL) {
fprintf (stderr, "Couldn't allocate memory for hash tables!\n");
shut_down (EXIT_FAILURE);
}
printf ("\nMemory Allocation:\n");
printf ("%lu hash entries * %lu bytes/entry = %lu kb of RAM\n",
max_hash, element_size, (max_hash*element_size)>>10);
/* clear our hash tables of possible errant values: */
memset (hash_table, 0, max_hash*element_size);
}
void init_hash_values (void) {
/* this function sets the hash value tables for the various pieces, as well
as values for castling rights, en passant, and side to move */
int i, j;
/* set frame and npiece values to 0: */
for (j = 0; j < 144; j++) {
h_values[0][j].x1 = 0;
h_values[0][j].x2 = 0;
h_values[13][j].x1 = 0;
h_values[13][j].x2 = 0;
}
/* set the hash values for the pieces: */
for (i = 1; i <= 12; i++) {
for (j = 0; j < 144; j++) {
h_values[i][j].x1 = rand_32 ();
h_values[i][j].x2 = rand_32 ();
}
}
/* set the ep hash values: */
ep_h_values[0].x1 = 0;
ep_h_values[0].x2 = 0;
for (j = 1; j < 144; j++) {
ep_h_values[j].x1 = rand_32 ();
ep_h_values[j].x2 = rand_32 ();
}
/* set the castling hash values: */
for (j = 0; j <= 1; j++) {
wck_h_values[j].x1 = rand_32 ();
wck_h_values[j].x2 = rand_32 ();
wcq_h_values[j].x1 = rand_32 ();
wcq_h_values[j].x2 = rand_32 ();
bck_h_values[j].x1 = rand_32 ();
bck_h_values[j].x2 = rand_32 ();
bcq_h_values[j].x1 = rand_32 ();
bcq_h_values[j].x2 = rand_32 ();
}
/* set the hash values for side to move: */
color_h_values[0].x1 = rand_32 ();
color_h_values[0].x2 = rand_32 ();
color_h_values[1].x1 = rand_32 ();
color_h_values[1].x2 = rand_32 ();
}
long int chk_hash (int alpha, int beta, int depth, int *type, move_s *move) {
/* see what info we can get from our hash tables for the current
position. This could be a value we can return, a suggested move, or
even just a warning not to use null search in this position */
hash_s *hash_p;
long int score;
s_int h_depth, flag;
d_long hash;
*type = no_info;
*move = dummy;
/* lookup our hash: */
hash_p = hash_table + (hash_mask & cur_pos.x1);
hash = hash_p->hash;
if (hash.x1 == cur_pos.x1 && hash.x2 == cur_pos.x2) {
/* get info from the hash: */
*move = hash_p->move;
score = hash_p->score;
h_depth = hash_p->depth;
flag = hash_p->flag;
/* adjust our score if it is a mate score: */
if (abs (score) > INF-100) {
if (score > 0)
score -= (ply);
else
score += (ply);
}
/* see if we should avoid null moves: */
if (h_depth >= depth-null_red && score < beta && flag == u_bound)
*type = avoid_null;
/* check what info we can get from our hash: */
if (h_depth >= depth) {
switch (flag) {
case u_bound:
if (score <= alpha) {
*type = u_bound;
return (alpha);
}
break;
case l_bound:
if (score >= beta) {
*type = l_bound;
return (beta);
}
break;
case exact:
*type = exact;
return (score);
break;
}
}
}
return (0);
}
void refresh_hash (void) {
/* reset the depths on the hash table to 0 for every game, so that old hash
entries don't keep new ones from being added in the next game: */
unsigned long int i;
for (i = 0; i <= hash_mask; i++) {
hash_table[i].depth = 0;
}
}
void store_hash (int alpha, int depth, int score, int flag, move_s move) {
/* store a position into the hash table: */
hash_s *hash_p;
/* look for the correct place to put the hash: */
hash_p = hash_table + (hash_mask & cur_pos.x1);
/* don't replace if the info in the hash table is more accurate than
what we have now: */
if (depth < hash_p->depth) {
return;
}
/* adjust for mate scores: */
if (abs (score) > INF-100) {
if (score > 0)
score += (ply);
else
score -= (ply);
}
/* store our hash info: */
hash_p->hash = cur_pos;
hash_p->depth = depth;
hash_p->score = score;
hash_p->move = move;
hash_p->flag = flag;
}
void xor (d_long *a, d_long b) {
/* xor two d_long values: */
a->x1 ^= b.x1;
a->x2 ^= b.x2;
}