[go: up one dir, main page]

Menu

[r1123]: / src / tablebase.c  Maximize  Restore  History

Download this file

381 lines (305 with data), 9.6 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
 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
/*
Protector -- a UCI chess engine
Copyright (C) 2009-2010 Raimund Heid (Raimund_Heid@yahoo.com)
This program 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
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tablebase.h"
#include "protector.h"
#include "io.h"
#include <assert.h>
#ifdef INCLUDE_TABLEBASE_ACCESS
#include <pthread.h>
#if defined __linux__
pthread_mutex_t mutex;
#else
pthread_spinlock_t lock;
#endif
volatile int tbAccessCount = 0;
#define MAX_PIECES_PER_SIDE 3
#define NO_EP 127
bool tbAvailable = FALSE;
typedef unsigned long long INDEX;
typedef unsigned int squaret;
char *cache = 0;
#define TB_FASTCALL /* __fastcall */
typedef INDEX(*PfnCalcIndex) (squaret *, squaret *, squaret, int fInverse);
extern int IInitializeTb(char *pszPath);
extern int FTbSetCacheSize(void *pv, unsigned long cbSize);
extern void VTbCloseFiles(void);
extern int IDescFindFromCounters(int *piCount);
extern int FRegisteredFun(int iTb, int side);
extern PfnCalcIndex PfnIndCalcFun(int iTb, int side);
extern int TB_FASTCALL L_TbtProbeTable(int iTb, int side, INDEX indOffset);
#define pageL 65536
#define tbbe_ssL ((pageL-4)/2)
#define bev_broken (tbbe_ssL+1) /* illegal or busted */
#define bev_mi1 tbbe_ssL /* mate in 1 move */
#define bev_mimin 1 /* mate in max moves */
#define bev_draw 0 /* draw */
#define bev_limax (-1) /* mated in max moves */
#define bev_li0 (-tbbe_ssL) /* mated in 0 moves */
#define PfnIndCalc PfnIndCalcFun
int setTablebaseCacheSize(unsigned int size)
{
unsigned long numBytes = (unsigned long) size * 1024 * 1024;
if (cache != 0)
{
free(cache);
}
cache = malloc(numBytes);
if (cache == 0)
{
logDebug("### Could not allocate tablebase cache (%lu bytes). ###\n",
numBytes);
return -1;
}
if (FTbSetCacheSize(cache, numBytes) == 0)
{
logDebug("### Could not set tablebase cache size (%lu bytes). ###\n",
numBytes);
free(cache);
cache = 0;
return -2;
}
/* logDebug("table base cache size set to %lu\n", numBytes); */
return 0;
}
int initializeTablebase(const char *path)
{
int result = IInitializeTb((char *) path);
if (result > 0)
{
if (setTablebaseCacheSize(4) != 0)
{
closeTablebaseFiles();
return -1;
}
/* logDebug("Tablebases found at %s\n", path); */
}
else
{
logDebug("### Error while looking for tablebases in %s ###\n", path);
}
tbAvailable = (bool) (result > 0);
return (result > 0 ? 0 : -1);
}
void closeTablebaseFiles()
{
VTbCloseFiles();
}
static void initializePieceData(const Bitboard * pieces, int *pieceCount,
unsigned int *pieceLocation)
{
Bitboard tmp = *pieces;
Square square;
*pieceCount = 0;
ITERATE_BITBOARD(&tmp, square)
{
*(pieceLocation++) = square;
(*pieceCount)++;
}
}
static int getMateValue(int fullMoves)
{
if (fullMoves > 0)
{
return 1 - VALUE_MATED - 2 * fullMoves;
}
else
{
return VALUE_MATED - 2 * fullMoves;
}
}
int probeTablebase(const Position * position)
{
int pieceCount[10];
unsigned int whitePieces[MAX_PIECES_PER_SIDE * 5 + 1];
unsigned int blackPieces[MAX_PIECES_PER_SIDE * 5 + 1];
unsigned int *pwhite, *pblack;
int tableNr, fInvert, tableValue, fullMoves;
Color color;
INDEX index;
Square enPassantSquare = (Square) NO_EP;
initializePieceData(&position->piecesOfType[WHITE_PAWN], &pieceCount[0],
&whitePieces[0 * MAX_PIECES_PER_SIDE]);
initializePieceData(&position->piecesOfType[WHITE_KNIGHT], &pieceCount[1],
&whitePieces[1 * MAX_PIECES_PER_SIDE]);
initializePieceData(&position->piecesOfType[WHITE_BISHOP], &pieceCount[2],
&whitePieces[2 * MAX_PIECES_PER_SIDE]);
initializePieceData(&position->piecesOfType[WHITE_ROOK], &pieceCount[3],
&whitePieces[3 * MAX_PIECES_PER_SIDE]);
initializePieceData(&position->piecesOfType[WHITE_QUEEN], &pieceCount[4],
&whitePieces[4 * MAX_PIECES_PER_SIDE]);
initializePieceData(&position->piecesOfType[BLACK_PAWN], &pieceCount[5],
&blackPieces[0 * MAX_PIECES_PER_SIDE]);
initializePieceData(&position->piecesOfType[BLACK_KNIGHT], &pieceCount[6],
&blackPieces[1 * MAX_PIECES_PER_SIDE]);
initializePieceData(&position->piecesOfType[BLACK_BISHOP], &pieceCount[7],
&blackPieces[2 * MAX_PIECES_PER_SIDE]);
initializePieceData(&position->piecesOfType[BLACK_ROOK], &pieceCount[8],
&blackPieces[3 * MAX_PIECES_PER_SIDE]);
initializePieceData(&position->piecesOfType[BLACK_QUEEN], &pieceCount[9],
&blackPieces[4 * MAX_PIECES_PER_SIDE]);
tableNr = IDescFindFromCounters(pieceCount);
if (tableNr == 0)
{
return TABLEBASE_ERROR;
}
whitePieces[5 * MAX_PIECES_PER_SIDE] = position->king[WHITE];
blackPieces[5 * MAX_PIECES_PER_SIDE] = position->king[BLACK];
if (tableNr > 0)
{
color = position->activeColor;
fInvert = 0;
pwhite = whitePieces;
pblack = blackPieces;
}
else
{
color = opponent(position->activeColor);
fInvert = 1;
pwhite = blackPieces;
pblack = whitePieces;
tableNr = -tableNr;
}
#if defined __APPLE__
pthread_mutex_lock((pthread_mutex_t *) & lock);
#else
#if defined __linux__
pthread_mutex_lock(&mutex);
#else
pthread_spin_lock(&lock);
#endif
#endif
if (FRegisteredFun(tableNr, color) == FALSE)
{
#if defined __APPLE__
pthread_mutex_unlock((pthread_mutex_t *) & lock);
#else
#if defined __linux__
pthread_mutex_unlock(&mutex);
#else
pthread_spin_unlock(&lock);
#endif
#endif
return TABLEBASE_ERROR;
}
if (position->enPassantSquare != NO_SQUARE)
{
const Bitboard attackers =
position->piecesOfType[PAWN | position->activeColor] &
generalMoves[PAWN | opponent(position->activeColor)]
[position->enPassantSquare];
if (attackers != EMPTY_BITBOARD)
{
/*
dumpSquare(position->enPassantSquare);
dumpPosition(position);
*/
enPassantSquare = position->enPassantSquare;
}
}
index = PfnIndCalcFun(tableNr, color)
(pwhite, pblack, enPassantSquare, fInvert);
tableValue = L_TbtProbeTable(tableNr, color, index);
#if defined __APPLE__
pthread_mutex_unlock((pthread_mutex_t *) & lock);
#else
#if defined __linux__
pthread_mutex_unlock(&mutex);
#else
pthread_spin_unlock(&lock);
#endif
#endif
if (tableValue == bev_broken)
{
return TABLEBASE_ERROR;
}
if (tableValue == 0)
{
return 0;
}
fullMoves = (tableValue > 0 ?
1 + tbbe_ssL - tableValue : bev_li0 - tableValue);
return getMateValue(fullMoves);
}
int initializeModuleTablebase()
{
#if defined __APPLE__
pthread_mutex_init((pthread_mutex_t *) & lock, tbAccessCount);
#else
#if defined __linux__
pthread_mutexattr_t mattr;
pthread_mutex_init(&mutex, &mattr);
#else
pthread_spin_init(&lock, tbAccessCount);
#endif
#endif
if (commandlineOptions.tablebasePath != 0)
{
return initializeTablebase(commandlineOptions.tablebasePath);
}
else
{
return 0;
}
}
static int testTableFinder()
{
#ifndef NDEBUG
Variation variation;
int tableValue;
initializeVariation(&variation, "B6k/8/8/8/8/8/8/K6N w - - 0 1");
tableValue = probeTablebase(&variation.singlePosition);
assert(tableValue == 29941);
initializeVariation(&variation, "B6k/8/8/8/8/8/8/K6N b - - 0 1");
tableValue = probeTablebase(&variation.singlePosition);
assert(tableValue == -29940);
#endif
return 0;
}
static int testMateValues()
{
#ifndef NDEBUG
Variation variation;
int tableValue;
initializeVariation(&variation, "R6k/8/6K1/8/8/8/8/8 b - - 0 1");
tableValue = probeTablebase(&variation.singlePosition);
assert(tableValue == VALUE_MATED);
initializeVariation(&variation, "7k/8/6K1/8/8/8/8/R7 w - - 0 1");
tableValue = probeTablebase(&variation.singlePosition);
assert(tableValue == -VALUE_MATED - 1);
#endif
return 0;
}
int testModuleTablebase()
{
int result;
if (tbAvailable == FALSE)
{
return 0;
}
if (commandlineOptions.tablebasePath == 0)
{
return 0;
}
if ((result = testTableFinder()) != 0)
{
return result;
}
if ((result = testMateValues()) != 0)
{
return result;
}
return 0;
}
#endif