/
tbprobe.c
432 lines (346 loc) · 12.8 KB
/
tbprobe.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
This Software is distributed with the following X11 License,
sometimes also known as MIT license.
Copyright (c) 2010 Miguel A. Ballicora
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.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "gtb-probe.h"
/* local prototypes */
static void dtm_print (unsigned stm, int tb_available, unsigned info, unsigned pliestomate);
static void wdl_print (unsigned stm, int tb_available, unsigned info);
/*
| paths to TBs, generally provided by the user
| Two styles are accepted: One path at a time, or
| multiple paths separated by ';'
| The example in this file uses both styles simultaneoulsy
*/
const char *path1 = "gtb/gtb4";
const char *path2 = "gtb/gtb3;gtb/gtb2";
const char *path3 = "gtb/gtb1";
int main (int argc, char *argv[])
{
/*--------------------------------------*\
| Probing info to be provided
\*--------------------------------------*/
unsigned int stm; /* side to move */
unsigned int epsquare; /* target square for an en passant capture */
unsigned int castling; /* castling availability, 0 => no castles */
unsigned int ws[17]; /* list of squares for white */
unsigned int bs[17]; /* list of squares for black */
unsigned char wp[17]; /* what white pieces are on those squares */
unsigned char bp[17]; /* what black pieces are on those squares */
/*--------------------------------------*\
| Probing info to be requested
\*--------------------------------------*/
char *initinfo; /* NULL if verbosity=0, initialization info if verbosity=1*/
int tb_available; /* 0 => FALSE, 1 => TRUE */
unsigned info = tb_UNKNOWN; /* default, no tbvalue */
unsigned pliestomate;
/*--------------------------------------*\
| Initialization info to be provided
\*--------------------------------------*/
int verbosity = 1; /* initialization 0 = non-verbose, 1 = verbose */
int scheme = tb_CP4; /* compression scheme to be used */
const char ** paths; /* paths where files will be searched */
size_t cache_size = 32*1024*1024; /* 32 MiB in this example */
/* wdl_fraction:
fraction, over 128, that will be dedicated to wdl information.
In other words, 96 means 3/4 of the cache will be dedicated to
win-draw-loss info, and 1/4 dedicated to distance to mate
information.
*/
int wdl_fraction = 96;
/*----------------------------------*\
| Return version of this demo
\*----------------------------------*/
#include "version.h"
#include "progname.h"
if (argc > 1 && 0==strcmp(argv[1],"-v")) {
printf ("%s %s\n",PROGRAM_NAME,VERSION);
return 0;
}
/*--------------------------------------*\
| Initialization:
| Include something like this at
| the beginning of the program.
\*--------------------------------------*/
/* the number of paths that can be added is only limited by memory */
paths = tbpaths_init();
if (NULL == paths) printf ("Error here... %d\n",__LINE__);
paths = tbpaths_add (paths, path1);
if (NULL == paths) printf ("Error here... %d\n",__LINE__);
paths = tbpaths_add (paths, path2);
if (NULL == paths) printf ("Error here... %d\n",__LINE__);
paths = tbpaths_add (paths, path3);
if (NULL == paths) printf ("Error here... %d\n",__LINE__);
/* init probing code, indexes, paths, etc. */
initinfo = tb_init (verbosity, scheme, paths);
/* init cache */
tbcache_init(cache_size, wdl_fraction);
tbstats_reset();
/* information to be output for the user, or to be saved in logs etc.*/
if (initinfo != NULL)
printf ("%s",initinfo);
/*--------------------------------------*\
|
| ASSIGNING POSITIONAL VALUES for
| one probing example
|
\*--------------------------------------*/
#if 1
/* needs 3-pc installed */
/* FEN: 8/8/8/4k3/8/8/8/KR6 w - - 0 1 */
stm = tb_WHITE_TO_MOVE;/* 0 = white to move, 1 = black to move */
epsquare = tb_NOSQUARE; /* no ep available */
castling = tb_NOCASTLE; /* no castling available, otherwise combine all
the castling possibilities with '|', for instance
white could castle both sides, black can't:
castling = tb_WOO | tb_WOOO;
both could castle on the king side:
castling = tb_WOO | tb_WOO;
etc.
*/
ws[0] = tb_A1;
ws[1] = tb_B1;
ws[2] = tb_NOSQUARE; /* it marks the end of list */
wp[0] = tb_KING;
wp[1] = tb_ROOK;
wp[2] = tb_NOPIECE; /* it marks the end of list */
bs[0] = tb_E5;
bs[1] = tb_NOSQUARE; /* it marks the end of list */
bp[0] = tb_KING;
bp[1] = tb_NOPIECE; /* it marks the end of list */
#else
/* needs 4-pc installed */
/* FEN: 8/8/6p1/4K3/7b/8/8/2k5 w - - 0 76 */
stm = tb_WHITE_TO_MOVE;/* 0 = white to move, 1 = black to move */
epsquare = tb_NOSQUARE; /* no ep available */
castling = tb_NOCASTLE; /* no castling available, otherwise combine all
the castling possibilities with '|', for instance
white could castle both sides, black can't:
castling = tb_WOO | tb_WOOO;
both could castle on the king side:
castling = tb_WOO | tb_WOO;
etc.
*/
ws[0] = tb_E5;
ws[1] = tb_NOSQUARE; /* it marks the end of list */
wp[0] = tb_KING;
wp[1] = tb_NOPIECE; /* it marks the end of list */
bs[0] = tb_H4;
bs[1] = tb_G6;
bs[2] = tb_C1;
bs[3] = tb_NOSQUARE; /* it marks the end of list */
bp[0] = tb_BISHOP;
bp[1] = tb_PAWN;
bp[2] = tb_KING;
bp[3] = tb_NOPIECE; /* it marks the end of list */
#endif
/*--------------------------------------*\
|
| PROBING TBs #1 (HARD)
|
\*--------------------------------------*/
/*
probing hard will go to the cache first, if the info is not found there,
it will finally go to the Hard Drive to find it
*/
tb_available = tb_probe_hard (stm, epsquare, castling, ws, bs, wp, bp, &info, &pliestomate);
/* print info */
dtm_print (stm, tb_available, info, pliestomate);
/*--------------------------------------*\
|
| ASSIGNING POSITIONAL VALUES for
| another example
|
\*--------------------------------------*/
/* only the rook position is different, the rest is the same */
ws[1] = tb_B6;
/*--------------------------------------*\
|
| PROBING TBs #2 (SOFT)
|
\*--------------------------------------*/
/*
probing soft goes to cache, if info not found there, it returns FALSE
It will **NEVER** go to the Hard Drive
If info is found, it is because the previous probe #1 filled up
the cache with the info needed for probe #2
*/
tb_available = tb_probe_soft (stm, epsquare, castling, ws, bs, wp, bp, &info, &pliestomate);
/* print info */
dtm_print (stm, tb_available, info, pliestomate);
/*--------------------------------------*\
|
| PROBING TBs #3 (SOFT)
| An example of what happens
| after tbcache_flush()
| which may be used to clear it
| for epd tests, etc.
|
\*--------------------------------------*/
/*
cache is flushed, so probing soft with the same position as #2
will surely return FALSE
*/
tbcache_flush();
/* same as #2 */
tb_available = tb_probe_soft (stm, epsquare, castling, ws, bs, wp, bp, &info, &pliestomate);
/* print info */
dtm_print (stm, tb_available, info, pliestomate);
/*--------------------------------------*\
|
| PROBING TBs #4
| (HARD, only win, draw, lose)
|
\*--------------------------------------*/
/*
Probing with the WDL versions of the probing functions
will return only the info needed to know whether a position
is a win, draw, or a loss.
The Gaviota tablebase library will try to return this info
with the best performance possible. If the only info needed for
a position is WDL, this function should be used rather
than the regular tb_probe_hard() function.
This function would be the "equivalent" of one that probes a bitbase.
*/
tb_available = tb_probe_WDL_hard (stm, epsquare, castling, ws, bs, wp, bp, &info);
/* print info */
wdl_print (stm, tb_available, info);
/*--------------------------------------*\
|
| RESTART?
| What if the user changes
| the conditions during run?
|
\*--------------------------------------*/
/*
| NEW INFO BY THE USER, example
\*---------------------------------------------*/
scheme = tb_CP2; /* compression scheme changes */
path1 = "gtb/gtb2";
path2 = "gtb/gtb1";
cache_size = 16*1024*1024; /* 16 MiB is the new cache size */
wdl_fraction = 104; /* more cache for wdl info than before */
/*
| RESTART PROCESS
\*---------------------------------------------*/
/* cleanup old paths */
paths = tbpaths_done(paths);
/* init new paths */
paths = tbpaths_init();
paths = tbpaths_add (paths, path1);
paths = tbpaths_add (paths, path2);
/* restart */
initinfo = tb_restart (verbosity, scheme, paths);
tbcache_restart(cache_size, wdl_fraction);
/* information to be output for the user, or to be saved in logs etc.*/
if (initinfo != NULL)
printf ("%s",initinfo);
/*
| Just to show, It is possible to know what TBs are installed.
| But, I can only find out after tb_init or tb_restart
\*----------------------------------------------------------------------------------------*/
{
unsigned av = tb_availability();
if (0 != (av& 1)) printf ("Some 3-pc TBs available\n"); else printf ("No 3-pc TBs available\n");
if (0 != (av& 2)) printf ("3-pc TBs complete\n");
if (0 != (av& 4)) printf ("Some 4-pc TBs available\n"); else printf ("No 4-pc TBs available\n");
if (0 != (av& 8)) printf ("4-pc TBs complete\n");
if (0 != (av&16)) printf ("Some 5-pc TBs available\n"); else printf ("No 5-pc TBs available\n");
if (0 != (av&32)) printf ("5-pc TBs complete\n");
printf ("\n");
}
/*
| Now that TBs have been restarted, we probe once again (HARD)
\*----------------------------------------------------------------------------------------*/
tb_available = tb_probe_hard (stm, epsquare, castling, ws, bs, wp, bp, &info, &pliestomate);
/* print info */
dtm_print (stm, tb_available, info, pliestomate);
/*--------------------------------------*\
|
| Clean up at the end of the program
|
\*--------------------------------------*/
tbcache_done();
tb_done();
paths = tbpaths_done(paths);
/*--------------------------------------*\
|
| Return
|
\*--------------------------------------*/
if (tb_available)
return EXIT_SUCCESS;
else
return EXIT_FAILURE;
}
/*----------------------------------------------------------------------*\
| These are local functions that just print the results after probing
\*----------------------------------------------------------------------*/
static void
dtm_print (unsigned stm, int tb_available, unsigned info, unsigned pliestomate)
{
if (tb_available) {
if (info == tb_DRAW)
printf ("Draw\n");
else if (info == tb_WMATE && stm == tb_WHITE_TO_MOVE)
printf ("White mates, plies=%u\n", pliestomate);
else if (info == tb_BMATE && stm == tb_BLACK_TO_MOVE)
printf ("Black mates, plies=%u\n", pliestomate);
else if (info == tb_WMATE && stm == tb_BLACK_TO_MOVE)
printf ("Black is mated, plies=%u\n", pliestomate);
else if (info == tb_BMATE && stm == tb_WHITE_TO_MOVE)
printf ("White is mated, plies=%u\n", pliestomate);
else {
printf ("FATAL ERROR, This should never be reached\n");
exit(EXIT_FAILURE);
}
printf ("\n");
} else {
printf ("Tablebase info not available\n\n");
}
}
static void
wdl_print (unsigned stm, int tb_available, unsigned info)
{
if (tb_available) {
if (info == tb_DRAW)
printf ("Draw\n");
else if (info == tb_WMATE && stm == tb_WHITE_TO_MOVE)
printf ("White mates\n");
else if (info == tb_BMATE && stm == tb_BLACK_TO_MOVE)
printf ("Black mates\n");
else if (info == tb_WMATE && stm == tb_BLACK_TO_MOVE)
printf ("Black is mated\n");
else if (info == tb_BMATE && stm == tb_WHITE_TO_MOVE)
printf ("White is mated\n");
else {
printf ("FATAL ERROR, This should never be reached\n");
exit(EXIT_FAILURE);
}
printf ("\n");
} else {
printf ("Tablebase info not available\n\n");
}
}