Subversion Repositories Games.Chess Giants

Rev

Rev 176 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
96 pmbaty 1
/*
169 pmbaty 2
  Stockfish, a UCI chess playing engine derived from Glaurung 2.1
96 pmbaty 3
  Copyright (c) 2013 Ronald de Man
169 pmbaty 4
  Copyright (C) 2016-2018 Marco Costalba, Lucas Braesch
96 pmbaty 5
 
169 pmbaty 6
  Stockfish is free software: you can redistribute it and/or modify
7
  it under the terms of the GNU General Public License as published by
8
  the Free Software Foundation, either version 3 of the License, or
9
  (at your option) any later version.
10
 
11
  Stockfish is distributed in the hope that it will be useful,
12
  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
  GNU General Public License for more details.
15
 
16
  You should have received a copy of the GNU General Public License
17
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
96 pmbaty 18
*/
19
 
20
#include <algorithm>
169 pmbaty 21
#include <atomic>
22
#include <cstdint>
185 pmbaty 23
#include <cstring>   // For std::memset and std::memcpy
169 pmbaty 24
#include <deque>
25
#include <fstream>
26
#include <iostream>
27
#include <list>
28
#include <sstream>
29
#include <type_traits>
96 pmbaty 30
 
169 pmbaty 31
#include "../bitboard.h"
32
#include "../movegen.h"
96 pmbaty 33
#include "../position.h"
34
#include "../search.h"
169 pmbaty 35
#include "../thread_win32.h"
36
#include "../types.h"
185 pmbaty 37
#include "../uci.h"
96 pmbaty 38
 
39
#include "tbprobe.h"
40
 
169 pmbaty 41
#ifndef _WIN32
42
#include <fcntl.h>
43
#include <unistd.h>
44
#include <sys/mman.h>
45
#include <sys/stat.h>
46
#else
47
#define WIN32_LEAN_AND_MEAN
48
#define NOMINMAX
49
#include <windows.h>
50
#endif
96 pmbaty 51
 
169 pmbaty 52
using namespace Tablebases;
96 pmbaty 53
 
169 pmbaty 54
int Tablebases::MaxCardinality;
96 pmbaty 55
 
169 pmbaty 56
namespace {
96 pmbaty 57
 
185 pmbaty 58
constexpr int TBPIECES = 7; // Max number of supported pieces
59
 
60
enum { BigEndian, LittleEndian };
61
enum TBType { KEY, WDL, DTZ }; // Used as template parameter
62
 
169 pmbaty 63
// Each table has a set of flags: all of them refer to DTZ tables, the last one to WDL tables
185 pmbaty 64
enum TBFlag { STM = 1, Mapped = 2, WinPlies = 4, LossPlies = 8, Wide = 16, SingleValue = 128 };
96 pmbaty 65
 
169 pmbaty 66
inline WDLScore operator-(WDLScore d) { return WDLScore(-int(d)); }
67
inline Square operator^=(Square& s, int i) { return s = Square(int(s) ^ i); }
68
inline Square operator^(Square s, int i) { return Square(int(s) ^ i); }
96 pmbaty 69
 
185 pmbaty 70
const std::string PieceToChar = " PNBRQK  pnbrqk";
96 pmbaty 71
 
169 pmbaty 72
int MapPawns[SQUARE_NB];
73
int MapB1H1H7[SQUARE_NB];
74
int MapA1D1D4[SQUARE_NB];
75
int MapKK[10][SQUARE_NB]; // [MapA1D1D4][SQUARE_NB]
76
 
185 pmbaty 77
int Binomial[6][SQUARE_NB];    // [k][n] k elements from a set of n elements
78
int LeadPawnIdx[6][SQUARE_NB]; // [leadPawnsCnt][SQUARE_NB]
79
int LeadPawnsSize[6][4];       // [leadPawnsCnt][FILE_A..FILE_D]
80
 
169 pmbaty 81
// Comparison function to sort leading pawns in ascending MapPawns[] order
82
bool pawns_comp(Square i, Square j) { return MapPawns[i] < MapPawns[j]; }
83
int off_A1H8(Square sq) { return int(rank_of(sq)) - file_of(sq); }
84
 
185 pmbaty 85
constexpr Value WDL_to_value[] = {
169 pmbaty 86
   -VALUE_MATE + MAX_PLY + 1,
87
    VALUE_DRAW - 2,
88
    VALUE_DRAW,
89
    VALUE_DRAW + 2,
90
    VALUE_MATE - MAX_PLY - 1
91
};
92
 
93
template<typename T, int Half = sizeof(T) / 2, int End = sizeof(T) - 1>
185 pmbaty 94
inline void swap_endian(T& x)
96 pmbaty 95
{
185 pmbaty 96
    static_assert(std::is_unsigned<T>::value, "Argument of swap_endian not unsigned");
97
 
98
    uint8_t tmp, *c = (uint8_t*)&x;
169 pmbaty 99
    for (int i = 0; i < Half; ++i)
100
        tmp = c[i], c[i] = c[End - i], c[End - i] = tmp;
96 pmbaty 101
}
185 pmbaty 102
template<> inline void swap_endian<uint8_t>(uint8_t&) {}
96 pmbaty 103
 
169 pmbaty 104
template<typename T, int LE> T number(void* addr)
96 pmbaty 105
{
185 pmbaty 106
    static const union { uint32_t i; char c[4]; } Le = { 0x01020304 };
107
    static const bool IsLittleEndian = (Le.c[0] == 4);
96 pmbaty 108
 
169 pmbaty 109
    T v;
96 pmbaty 110
 
169 pmbaty 111
    if ((uintptr_t)addr & (alignof(T) - 1)) // Unaligned pointer (very rare)
112
        std::memcpy(&v, addr, sizeof(T));
113
    else
114
        v = *((T*)addr);
96 pmbaty 115
 
169 pmbaty 116
    if (LE != IsLittleEndian)
185 pmbaty 117
        swap_endian(v);
169 pmbaty 118
    return v;
119
}
120
 
185 pmbaty 121
// DTZ tables don't store valid scores for moves that reset the rule50 counter
122
// like captures and pawn moves but we can easily recover the correct dtz of the
123
// previous move if we know the position's WDL score.
124
int dtz_before_zeroing(WDLScore wdl) {
125
    return wdl == WDLWin         ?  1   :
126
           wdl == WDLCursedWin   ?  101 :
127
           wdl == WDLBlessedLoss ? -101 :
128
           wdl == WDLLoss        ? -1   : 0;
129
}
169 pmbaty 130
 
185 pmbaty 131
// Return the sign of a number (-1, 0, 1)
132
template <typename T> int sign_of(T val) {
133
    return (T(0) < val) - (val < T(0));
134
}
169 pmbaty 135
 
185 pmbaty 136
// Numbers in little endian used by sparseIndex[] to point into blockLength[]
137
struct SparseEntry {
138
    char block[4];   // Number of block
139
    char offset[2];  // Offset within the block
140
};
169 pmbaty 141
 
185 pmbaty 142
static_assert(sizeof(SparseEntry) == 6, "SparseEntry must be 6 bytes");
169 pmbaty 143
 
185 pmbaty 144
typedef uint16_t Sym; // Huffman symbol
169 pmbaty 145
 
185 pmbaty 146
struct LR {
147
    enum Side { Left, Right };
169 pmbaty 148
 
185 pmbaty 149
    uint8_t lr[3]; // The first 12 bits is the left-hand symbol, the second 12
150
                   // bits is the right-hand symbol. If symbol has length 1,
151
                   // then the left-hand symbol is the stored value.
152
    template<Side S>
153
    Sym get() {
154
        return S == Left  ? ((lr[1] & 0xF) << 8) | lr[0] :
155
               S == Right ?  (lr[2] << 4) | (lr[1] >> 4) : (assert(false), Sym(-1));
169 pmbaty 156
    }
185 pmbaty 157
};
169 pmbaty 158
 
185 pmbaty 159
static_assert(sizeof(LR) == 3, "LR tree entry must be 3 bytes");
169 pmbaty 160
 
185 pmbaty 161
// Tablebases data layout is structured as following:
162
//
163
//  TBFile:   memory maps/unmaps the physical .rtbw and .rtbz files
164
//  TBTable:  one object for each file with corresponding indexing information
165
//  TBTables: has ownership of TBTable objects, keeping a list and a hash
169 pmbaty 166
 
185 pmbaty 167
// class TBFile memory maps/unmaps the single .rtbw and .rtbz files. Files are
168
// memory mapped for best performance. Files are mapped at first access: at init
169
// time only existence of the file is checked.
169 pmbaty 170
class TBFile : public std::ifstream {
171
 
172
    std::string fname;
173
 
174
public:
175
    // Look for and open the file among the Paths directories where the .rtbw
176
    // and .rtbz files can be found. Multiple directories are separated by ";"
177
    // on Windows and by ":" on Unix-based operating systems.
178
    //
179
    // Example:
180
    // C:\tb\wdl345;C:\tb\wdl6;D:\tb\dtz345;D:\tb\dtz6
181
    static std::string Paths;
182
 
183
    TBFile(const std::string& f) {
184
 
185
#ifndef _WIN32
185 pmbaty 186
        constexpr char SepChar = ':';
96 pmbaty 187
#else
185 pmbaty 188
        constexpr char SepChar = ';';
96 pmbaty 189
#endif
169 pmbaty 190
        std::stringstream ss(Paths);
191
        std::string path;
192
 
193
        while (std::getline(ss, path, SepChar)) {
194
            fname = path + "/" + f;
195
            std::ifstream::open(fname);
196
            if (is_open())
197
                return;
198
        }
96 pmbaty 199
    }
200
 
169 pmbaty 201
    // Memory map the file and check it. File should be already open and will be
202
    // closed after mapping.
185 pmbaty 203
    uint8_t* map(void** baseAddress, uint64_t* mapping, TBType type) {
169 pmbaty 204
 
205
        assert(is_open());
206
 
207
        close(); // Need to re-open to get native file descriptor
208
 
209
#ifndef _WIN32
210
        struct stat statbuf;
211
        int fd = ::open(fname.c_str(), O_RDONLY);
212
 
213
        if (fd == -1)
214
            return *baseAddress = nullptr, nullptr;
215
 
216
        fstat(fd, &statbuf);
217
        *mapping = statbuf.st_size;
218
        *baseAddress = mmap(nullptr, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
185 pmbaty 219
        madvise(*baseAddress, statbuf.st_size, MADV_RANDOM);
169 pmbaty 220
        ::close(fd);
221
 
222
        if (*baseAddress == MAP_FAILED) {
223
            std::cerr << "Could not mmap() " << fname << std::endl;
224
            exit(1);
225
        }
226
#else
227
        HANDLE fd = CreateFile(fname.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr,
228
                               OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
229
 
230
        if (fd == INVALID_HANDLE_VALUE)
231
            return *baseAddress = nullptr, nullptr;
232
 
233
        DWORD size_high;
234
        DWORD size_low = GetFileSize(fd, &size_high);
235
        HANDLE mmap = CreateFileMapping(fd, nullptr, PAGE_READONLY, size_high, size_low, nullptr);
236
        CloseHandle(fd);
237
 
238
        if (!mmap) {
239
            std::cerr << "CreateFileMapping() failed" << std::endl;
240
            exit(1);
241
        }
242
 
243
        *mapping = (uint64_t)mmap;
244
        *baseAddress = MapViewOfFile(mmap, FILE_MAP_READ, 0, 0, 0);
245
 
246
        if (!*baseAddress) {
247
            std::cerr << "MapViewOfFile() failed, name = " << fname
248
                      << ", error = " << GetLastError() << std::endl;
249
            exit(1);
250
        }
251
#endif
252
        uint8_t* data = (uint8_t*)*baseAddress;
253
 
185 pmbaty 254
        constexpr uint8_t Magics[][4] = { { 0xD7, 0x66, 0x0C, 0xA5 },
255
                                          { 0x71, 0xE8, 0x23, 0x5D } };
256
 
257
        if (memcmp(data, Magics[type == WDL], 4)) {
169 pmbaty 258
            std::cerr << "Corrupted table in file " << fname << std::endl;
259
            unmap(*baseAddress, *mapping);
260
            return *baseAddress = nullptr, nullptr;
261
        }
262
 
185 pmbaty 263
        return data + 4; // Skip Magics's header
96 pmbaty 264
    }
265
 
169 pmbaty 266
    static void unmap(void* baseAddress, uint64_t mapping) {
267
 
268
#ifndef _WIN32
269
        munmap(baseAddress, mapping);
270
#else
271
        UnmapViewOfFile(baseAddress);
272
        CloseHandle((HANDLE)mapping);
273
#endif
96 pmbaty 274
    }
169 pmbaty 275
};
276
 
277
std::string TBFile::Paths;
278
 
185 pmbaty 279
// struct PairsData contains low level indexing information to access TB data.
280
// There are 8, 4 or 2 PairsData records for each TBTable, according to type of
281
// table and if positions have pawns or not. It is populated at first access.
282
struct PairsData {
283
    uint8_t flags;                 // Table flags, see enum TBFlag
284
    uint8_t maxSymLen;             // Maximum length in bits of the Huffman symbols
285
    uint8_t minSymLen;             // Minimum length in bits of the Huffman symbols
286
    uint32_t blocksNum;            // Number of blocks in the TB file
287
    size_t sizeofBlock;            // Block size in bytes
288
    size_t span;                   // About every span values there is a SparseIndex[] entry
289
    Sym* lowestSym;                // lowestSym[l] is the symbol of length l with the lowest value
290
    LR* btree;                     // btree[sym] stores the left and right symbols that expand sym
291
    uint16_t* blockLength;         // Number of stored positions (minus one) for each block: 1..65536
292
    uint32_t blockLengthSize;      // Size of blockLength[] table: padded so it's bigger than blocksNum
293
    SparseEntry* sparseIndex;      // Partial indices into blockLength[]
294
    size_t sparseIndexSize;        // Size of SparseIndex[] table
295
    uint8_t* data;                 // Start of Huffman compressed data
296
    std::vector<uint64_t> base64;  // base64[l - min_sym_len] is the 64bit-padded lowest symbol of length l
297
    std::vector<uint8_t> symlen;   // Number of values (-1) represented by a given Huffman symbol: 1..256
298
    Piece pieces[TBPIECES];        // Position pieces: the order of pieces defines the groups
299
    uint64_t groupIdx[TBPIECES+1]; // Start index used for the encoding of the group's pieces
300
    int groupLen[TBPIECES+1];      // Number of pieces in a given group: KRKN -> (3, 1)
301
    uint16_t map_idx[4];           // WDLWin, WDLLoss, WDLCursedWin, WDLBlessedLoss (used in DTZ)
302
};
169 pmbaty 303
 
185 pmbaty 304
// struct TBTable contains indexing information to access the corresponding TBFile.
305
// There are 2 types of TBTable, corresponding to a WDL or a DTZ file. TBTable
306
// is populated at init time but the nested PairsData records are populated at
307
// first access, when the corresponding file is memory mapped.
308
template<TBType Type>
309
struct TBTable {
310
    typedef typename std::conditional<Type == WDL, WDLScore, int>::type Ret;
311
 
312
    static constexpr int Sides = Type == WDL ? 2 : 1;
313
 
314
    std::atomic_bool ready;
315
    void* baseAddress;
316
    uint8_t* map;
317
    uint64_t mapping;
318
    Key key;
319
    Key key2;
320
    int pieceCount;
321
    bool hasPawns;
322
    bool hasUniquePieces;
323
    uint8_t pawnCount[2]; // [Lead color / other color]
324
    PairsData items[Sides][4]; // [wtm / btm][FILE_A..FILE_D or 0]
325
 
326
    PairsData* get(int stm, int f) {
327
        return &items[stm % Sides][hasPawns ? f : 0];
328
    }
329
 
330
    TBTable() : ready(false), baseAddress(nullptr) {}
331
    explicit TBTable(const std::string& code);
332
    explicit TBTable(const TBTable<WDL>& wdl);
333
 
334
    ~TBTable() {
335
        if (baseAddress)
336
            TBFile::unmap(baseAddress, mapping);
337
    }
338
};
339
 
340
template<>
341
TBTable<WDL>::TBTable(const std::string& code) : TBTable() {
342
 
169 pmbaty 343
    StateInfo st;
344
    Position pos;
345
 
346
    key = pos.set(code, WHITE, &st).material_key();
185 pmbaty 347
    pieceCount = pos.count<ALL_PIECES>();
169 pmbaty 348
    hasPawns = pos.pieces(PAWN);
349
 
185 pmbaty 350
    hasUniquePieces = false;
169 pmbaty 351
    for (Color c = WHITE; c <= BLACK; ++c)
352
        for (PieceType pt = PAWN; pt < KING; ++pt)
353
            if (popcount(pos.pieces(c, pt)) == 1)
354
                hasUniquePieces = true;
355
 
185 pmbaty 356
    // Set the leading color. In case both sides have pawns the leading color
357
    // is the side with less pawns because this leads to better compression.
358
    bool c =   !pos.count<PAWN>(BLACK)
359
            || (   pos.count<PAWN>(WHITE)
360
                && pos.count<PAWN>(BLACK) >= pos.count<PAWN>(WHITE));
169 pmbaty 361
 
185 pmbaty 362
    pawnCount[0] = pos.count<PAWN>(c ? WHITE : BLACK);
363
    pawnCount[1] = pos.count<PAWN>(c ? BLACK : WHITE);
96 pmbaty 364
 
169 pmbaty 365
    key2 = pos.set(code, BLACK, &st).material_key();
96 pmbaty 366
}
367
 
185 pmbaty 368
template<>
369
TBTable<DTZ>::TBTable(const TBTable<WDL>& wdl) : TBTable() {
96 pmbaty 370
 
185 pmbaty 371
    // Use the corresponding WDL table to avoid recalculating all from scratch
169 pmbaty 372
    key = wdl.key;
373
    key2 = wdl.key2;
374
    pieceCount = wdl.pieceCount;
375
    hasPawns = wdl.hasPawns;
376
    hasUniquePieces = wdl.hasUniquePieces;
185 pmbaty 377
    pawnCount[0] = wdl.pawnCount[0];
378
    pawnCount[1] = wdl.pawnCount[1];
379
}
169 pmbaty 380
 
185 pmbaty 381
// class TBTables creates and keeps ownership of the TBTable objects, one for
382
// each TB file found. It supports a fast, hash based, table lookup. Populated
383
// at init time, accessed at probe time.
384
class TBTables {
385
 
386
    typedef std::tuple<Key, TBTable<WDL>*, TBTable<DTZ>*> Entry;
387
 
388
    static constexpr int Size = 1 << 12; // 4K table, indexed by key's 12 lsb
389
    static constexpr int Overflow = 1;  // Number of elements allowed to map to the last bucket
390
 
391
    Entry hashTable[Size + Overflow];
392
 
393
    std::deque<TBTable<WDL>> wdlTable;
394
    std::deque<TBTable<DTZ>> dtzTable;
395
 
396
    void insert(Key key, TBTable<WDL>* wdl, TBTable<DTZ>* dtz) {
397
        uint32_t homeBucket = (uint32_t)key & (Size - 1);
398
        Entry entry = std::make_tuple(key, wdl, dtz);
399
 
400
        // Ensure last element is empty to avoid overflow when looking up
401
        for (uint32_t bucket = homeBucket; bucket < Size + Overflow - 1; ++bucket) {
402
            Key otherKey = std::get<KEY>(hashTable[bucket]);
403
            if (otherKey == key || !std::get<WDL>(hashTable[bucket])) {
404
                hashTable[bucket] = entry;
405
                return;
406
            }
407
 
408
            // Robin Hood hashing: If we've probed for longer than this element,
409
            // insert here and search for a new spot for the other element instead.
410
            uint32_t otherHomeBucket = (uint32_t)otherKey & (Size - 1);
411
            if (otherHomeBucket > homeBucket) {
412
                swap(entry, hashTable[bucket]);
413
                key = otherKey;
414
                homeBucket = otherHomeBucket;
415
            }
416
        }
417
        std::cerr << "TB hash table size too low!" << std::endl;
418
        exit(1);
96 pmbaty 419
    }
420
 
185 pmbaty 421
public:
422
    template<TBType Type>
423
    TBTable<Type>* get(Key key) {
424
        for (const Entry* entry = &hashTable[(uint32_t)key & (Size - 1)]; ; ++entry) {
425
            if (std::get<KEY>(*entry) == key || !std::get<Type>(*entry))
426
                return std::get<Type>(*entry);
427
        }
428
    }
96 pmbaty 429
 
185 pmbaty 430
    void clear() {
431
        memset(hashTable, 0, sizeof(hashTable));
432
        wdlTable.clear();
433
        dtzTable.clear();
434
    }
435
    size_t size() const { return wdlTable.size(); }
436
    void add(const std::vector<PieceType>& pieces);
437
};
169 pmbaty 438
 
185 pmbaty 439
TBTables TBTables;
169 pmbaty 440
 
185 pmbaty 441
// If the corresponding file exists two new objects TBTable<WDL> and TBTable<DTZ>
442
// are created and added to the lists and hash table. Called at init time.
443
void TBTables::add(const std::vector<PieceType>& pieces) {
169 pmbaty 444
 
445
    std::string code;
446
 
447
    for (PieceType pt : pieces)
448
        code += PieceToChar[pt];
449
 
450
    TBFile file(code.insert(code.find('K', 1), "v") + ".rtbw"); // KRK -> KRvK
451
 
452
    if (!file.is_open()) // Only WDL file is checked
453
        return;
454
 
455
    file.close();
456
 
457
    MaxCardinality = std::max((int)pieces.size(), MaxCardinality);
458
 
459
    wdlTable.emplace_back(code);
460
    dtzTable.emplace_back(wdlTable.back());
461
 
185 pmbaty 462
    // Insert into the hash keys for both colors: KRvK with KR white and black
169 pmbaty 463
    insert(wdlTable.back().key , &wdlTable.back(), &dtzTable.back());
464
    insert(wdlTable.back().key2, &wdlTable.back(), &dtzTable.back());
465
}
466
 
467
// TB tables are compressed with canonical Huffman code. The compressed data is divided into
468
// blocks of size d->sizeofBlock, and each block stores a variable number of symbols.
469
// Each symbol represents either a WDL or a (remapped) DTZ value, or a pair of other symbols
470
// (recursively). If you keep expanding the symbols in a block, you end up with up to 65536
471
// WDL or DTZ values. Each symbol represents up to 256 values and will correspond after
472
// Huffman coding to at least 1 bit. So a block of 32 bytes corresponds to at most
473
// 32 x 8 x 256 = 65536 values. This maximum is only reached for tables that consist mostly
474
// of draws or mostly of wins, but such tables are actually quite common. In principle, the
475
// blocks in WDL tables are 64 bytes long (and will be aligned on cache lines). But for
476
// mostly-draw or mostly-win tables this can leave many 64-byte blocks only half-filled, so
477
// in such cases blocks are 32 bytes long. The blocks of DTZ tables are up to 1024 bytes long.
478
// The generator picks the size that leads to the smallest table. The "book" of symbols and
479
// Huffman codes is the same for all blocks in the table. A non-symmetric pawnless TB file
480
// will have one table for wtm and one for btm, a TB file with pawns will have tables per
481
// file a,b,c,d also in this case one set for wtm and one for btm.
482
int decompress_pairs(PairsData* d, uint64_t idx) {
483
 
484
    // Special case where all table positions store the same value
485
    if (d->flags & TBFlag::SingleValue)
486
        return d->minSymLen;
487
 
488
    // First we need to locate the right block that stores the value at index "idx".
489
    // Because each block n stores blockLength[n] + 1 values, the index i of the block
490
    // that contains the value at position idx is:
491
    //
492
    //                    for (i = -1, sum = 0; sum <= idx; i++)
493
    //                        sum += blockLength[i + 1] + 1;
494
    //
495
    // This can be slow, so we use SparseIndex[] populated with a set of SparseEntry that
496
    // point to known indices into blockLength[]. Namely SparseIndex[k] is a SparseEntry
497
    // that stores the blockLength[] index and the offset within that block of the value
498
    // with index I(k), where:
499
    //
500
    //       I(k) = k * d->span + d->span / 2      (1)
501
 
502
    // First step is to get the 'k' of the I(k) nearest to our idx, using definition (1)
185 pmbaty 503
    uint32_t k = idx / d->span;
169 pmbaty 504
 
505
    // Then we read the corresponding SparseIndex[] entry
506
    uint32_t block = number<uint32_t, LittleEndian>(&d->sparseIndex[k].block);
507
    int offset     = number<uint16_t, LittleEndian>(&d->sparseIndex[k].offset);
508
 
509
    // Now compute the difference idx - I(k). From definition of k we know that
510
    //
511
    //       idx = k * d->span + idx % d->span    (2)
512
    //
513
    // So from (1) and (2) we can compute idx - I(K):
514
    int diff = idx % d->span - d->span / 2;
515
 
516
    // Sum the above to offset to find the offset corresponding to our idx
517
    offset += diff;
518
 
519
    // Move to previous/next block, until we reach the correct block that contains idx,
520
    // that is when 0 <= offset <= d->blockLength[block]
521
    while (offset < 0)
522
        offset += d->blockLength[--block] + 1;
523
 
524
    while (offset > d->blockLength[block])
525
        offset -= d->blockLength[block++] + 1;
526
 
527
    // Finally, we find the start address of our block of canonical Huffman symbols
185 pmbaty 528
    uint32_t* ptr = (uint32_t*)(d->data + ((uint64_t)block * d->sizeofBlock));
169 pmbaty 529
 
530
    // Read the first 64 bits in our block, this is a (truncated) sequence of
531
    // unknown number of symbols of unknown length but we know the first one
532
    // is at the beginning of this 64 bits sequence.
533
    uint64_t buf64 = number<uint64_t, BigEndian>(ptr); ptr += 2;
534
    int buf64Size = 64;
535
    Sym sym;
536
 
537
    while (true) {
538
        int len = 0; // This is the symbol length - d->min_sym_len
539
 
540
        // Now get the symbol length. For any symbol s64 of length l right-padded
541
        // to 64 bits we know that d->base64[l-1] >= s64 >= d->base64[l] so we
542
        // can find the symbol length iterating through base64[].
543
        while (buf64 < d->base64[len])
544
            ++len;
545
 
546
        // All the symbols of a given length are consecutive integers (numerical
547
        // sequence property), so we can compute the offset of our symbol of
548
        // length len, stored at the beginning of buf64.
185 pmbaty 549
        sym = (buf64 - d->base64[len]) >> (64 - len - d->minSymLen);
169 pmbaty 550
 
551
        // Now add the value of the lowest symbol of length len to get our symbol
552
        sym += number<Sym, LittleEndian>(&d->lowestSym[len]);
553
 
554
        // If our offset is within the number of values represented by symbol sym
555
        // we are done...
556
        if (offset < d->symlen[sym] + 1)
557
            break;
558
 
559
        // ...otherwise update the offset and continue to iterate
560
        offset -= d->symlen[sym] + 1;
561
        len += d->minSymLen; // Get the real length
562
        buf64 <<= len;       // Consume the just processed symbol
563
        buf64Size -= len;
564
 
565
        if (buf64Size <= 32) { // Refill the buffer
566
            buf64Size += 32;
567
            buf64 |= (uint64_t)number<uint32_t, BigEndian>(ptr++) << (64 - buf64Size);
568
        }
96 pmbaty 569
    }
570
 
169 pmbaty 571
    // Ok, now we have our symbol that expands into d->symlen[sym] + 1 symbols.
572
    // We binary-search for our value recursively expanding into the left and
573
    // right child symbols until we reach a leaf node where symlen[sym] + 1 == 1
574
    // that will store the value we need.
575
    while (d->symlen[sym]) {
576
 
577
        Sym left = d->btree[sym].get<LR::Left>();
578
 
579
        // If a symbol contains 36 sub-symbols (d->symlen[sym] + 1 = 36) and
580
        // expands in a pair (d->symlen[left] = 23, d->symlen[right] = 11), then
581
        // we know that, for instance the ten-th value (offset = 10) will be on
582
        // the left side because in Recursive Pairing child symbols are adjacent.
583
        if (offset < d->symlen[left] + 1)
584
            sym = left;
585
        else {
586
            offset -= d->symlen[left] + 1;
587
            sym = d->btree[sym].get<LR::Right>();
588
        }
96 pmbaty 589
    }
590
 
185 pmbaty 591
    return d->btree[sym].get<LR::Left>();
169 pmbaty 592
}
96 pmbaty 593
 
185 pmbaty 594
bool check_dtz_stm(TBTable<WDL>*, int, File) { return true; }
169 pmbaty 595
 
185 pmbaty 596
bool check_dtz_stm(TBTable<DTZ>* entry, int stm, File f) {
169 pmbaty 597
 
185 pmbaty 598
    auto flags = entry->get(stm, f)->flags;
169 pmbaty 599
    return   (flags & TBFlag::STM) == stm
600
          || ((entry->key == entry->key2) && !entry->hasPawns);
601
}
602
 
603
// DTZ scores are sorted by frequency of occurrence and then assigned the
604
// values 0, 1, 2, ... in order of decreasing frequency. This is done for each
605
// of the four WDLScore values. The mapping information necessary to reconstruct
606
// the original values is stored in the TB file and read during map[] init.
185 pmbaty 607
WDLScore map_score(TBTable<WDL>*, File, int value, WDLScore) { return WDLScore(value - 2); }
169 pmbaty 608
 
185 pmbaty 609
int map_score(TBTable<DTZ>* entry, File f, int value, WDLScore wdl) {
169 pmbaty 610
 
185 pmbaty 611
    constexpr int WDLMap[] = { 1, 3, 0, 2, 0 };
169 pmbaty 612
 
185 pmbaty 613
    auto flags = entry->get(0, f)->flags;
169 pmbaty 614
 
185 pmbaty 615
    uint8_t* map = entry->map;
616
    uint16_t* idx = entry->get(0, f)->map_idx;
617
    if (flags & TBFlag::Mapped) {
618
        if (flags & TBFlag::Wide)
619
            value = ((uint16_t *)map)[idx[WDLMap[wdl + 2]] + value];
620
        else
621
            value = map[idx[WDLMap[wdl + 2]] + value];
622
    }
169 pmbaty 623
 
624
    // DTZ tables store distance to zero in number of moves or plies. We
625
    // want to return plies, so we have convert to plies when needed.
626
    if (   (wdl == WDLWin  && !(flags & TBFlag::WinPlies))
627
        || (wdl == WDLLoss && !(flags & TBFlag::LossPlies))
628
        ||  wdl == WDLCursedWin
629
        ||  wdl == WDLBlessedLoss)
630
        value *= 2;
631
 
632
    return value + 1;
633
}
634
 
635
// Compute a unique index out of a position and use it to probe the TB file. To
636
// encode k pieces of same type and color, first sort the pieces by square in
637
// ascending order s1 <= s2 <= ... <= sk then compute the unique index as:
638
//
639
//      idx = Binomial[1][s1] + Binomial[2][s2] + ... + Binomial[k][sk]
640
//
185 pmbaty 641
template<typename T, typename Ret = typename T::Ret>
642
Ret do_probe_table(const Position& pos, T* entry, WDLScore wdl, ProbeState* result) {
169 pmbaty 643
 
644
    Square squares[TBPIECES];
645
    Piece pieces[TBPIECES];
646
    uint64_t idx;
647
    int next = 0, size = 0, leadPawnsCnt = 0;
648
    PairsData* d;
649
    Bitboard b, leadPawns = 0;
650
    File tbFile = FILE_A;
651
 
652
    // A given TB entry like KRK has associated two material keys: KRvk and Kvkr.
653
    // If both sides have the same pieces keys are equal. In this case TB tables
654
    // only store the 'white to move' case, so if the position to lookup has black
655
    // to move, we need to switch the color and flip the squares before to lookup.
656
    bool symmetricBlackToMove = (entry->key == entry->key2 && pos.side_to_move());
657
 
658
    // TB files are calculated for white as stronger side. For instance we have
659
    // KRvK, not KvKR. A position where stronger side is white will have its
660
    // material key == entry->key, otherwise we have to switch the color and
661
    // flip the squares before to lookup.
662
    bool blackStronger = (pos.material_key() != entry->key);
663
 
664
    int flipColor   = (symmetricBlackToMove || blackStronger) * 8;
665
    int flipSquares = (symmetricBlackToMove || blackStronger) * 070;
666
    int stm         = (symmetricBlackToMove || blackStronger) ^ pos.side_to_move();
667
 
668
    // For pawns, TB files store 4 separate tables according if leading pawn is on
669
    // file a, b, c or d after reordering. The leading pawn is the one with maximum
670
    // MapPawns[] value, that is the one most toward the edges and with lowest rank.
671
    if (entry->hasPawns) {
672
 
673
        // In all the 4 tables, pawns are at the beginning of the piece sequence and
674
        // their color is the reference one. So we just pick the first one.
185 pmbaty 675
        Piece pc = Piece(entry->get(0, 0)->pieces[0] ^ flipColor);
169 pmbaty 676
 
677
        assert(type_of(pc) == PAWN);
678
 
679
        leadPawns = b = pos.pieces(color_of(pc), PAWN);
680
        do
681
            squares[size++] = pop_lsb(&b) ^ flipSquares;
682
        while (b);
683
 
684
        leadPawnsCnt = size;
685
 
686
        std::swap(squares[0], *std::max_element(squares, squares + leadPawnsCnt, pawns_comp));
687
 
688
        tbFile = file_of(squares[0]);
689
        if (tbFile > FILE_D)
690
            tbFile = file_of(squares[0] ^ 7); // Horizontal flip: SQ_H1 -> SQ_A1
185 pmbaty 691
    }
169 pmbaty 692
 
693
    // DTZ tables are one-sided, i.e. they store positions only for white to
694
    // move or only for black to move, so check for side to move to be stm,
695
    // early exit otherwise.
185 pmbaty 696
    if (!check_dtz_stm(entry, stm, tbFile))
697
        return *result = CHANGE_STM, Ret();
169 pmbaty 698
 
699
    // Now we are ready to get all the position pieces (but the lead pawns) and
700
    // directly map them to the correct color and square.
701
    b = pos.pieces() ^ leadPawns;
96 pmbaty 702
    do {
169 pmbaty 703
        Square s = pop_lsb(&b);
704
        squares[size] = s ^ flipSquares;
705
        pieces[size++] = Piece(pos.piece_on(s) ^ flipColor);
706
    } while (b);
707
 
708
    assert(size >= 2);
709
 
185 pmbaty 710
    d = entry->get(stm, tbFile);
711
 
169 pmbaty 712
    // Then we reorder the pieces to have the same sequence as the one stored
185 pmbaty 713
    // in pieces[i]: the sequence that ensures the best compression.
169 pmbaty 714
    for (int i = leadPawnsCnt; i < size; ++i)
715
        for (int j = i; j < size; ++j)
716
            if (d->pieces[i] == pieces[j])
717
            {
718
                std::swap(pieces[i], pieces[j]);
719
                std::swap(squares[i], squares[j]);
720
                break;
721
            }
722
 
723
    // Now we map again the squares so that the square of the lead piece is in
724
    // the triangle A1-D1-D4.
725
    if (file_of(squares[0]) > FILE_D)
726
        for (int i = 0; i < size; ++i)
727
            squares[i] ^= 7; // Horizontal flip: SQ_H1 -> SQ_A1
728
 
729
    // Encode leading pawns starting with the one with minimum MapPawns[] and
730
    // proceeding in ascending order.
731
    if (entry->hasPawns) {
732
        idx = LeadPawnIdx[leadPawnsCnt][squares[0]];
733
 
734
        std::sort(squares + 1, squares + leadPawnsCnt, pawns_comp);
735
 
736
        for (int i = 1; i < leadPawnsCnt; ++i)
737
            idx += Binomial[i][MapPawns[squares[i]]];
738
 
739
        goto encode_remaining; // With pawns we have finished special treatments
96 pmbaty 740
    }
169 pmbaty 741
 
742
    // In positions withouth pawns, we further flip the squares to ensure leading
743
    // piece is below RANK_5.
744
    if (rank_of(squares[0]) > RANK_4)
745
        for (int i = 0; i < size; ++i)
746
            squares[i] ^= 070; // Vertical flip: SQ_A8 -> SQ_A1
747
 
748
    // Look for the first piece of the leading group not on the A1-D4 diagonal
749
    // and ensure it is mapped below the diagonal.
750
    for (int i = 0; i < d->groupLen[0]; ++i) {
751
        if (!off_A1H8(squares[i]))
752
            continue;
753
 
754
        if (off_A1H8(squares[i]) > 0) // A1-H8 diagonal flip: SQ_A3 -> SQ_C3
755
            for (int j = i; j < size; ++j)
756
                squares[j] = Square(((squares[j] >> 3) | (squares[j] << 3)) & 63);
757
        break;
96 pmbaty 758
    }
759
 
169 pmbaty 760
    // Encode the leading group.
761
    //
762
    // Suppose we have KRvK. Let's say the pieces are on square numbers wK, wR
763
    // and bK (each 0...63). The simplest way to map this position to an index
764
    // is like this:
765
    //
766
    //   index = wK * 64 * 64 + wR * 64 + bK;
767
    //
768
    // But this way the TB is going to have 64*64*64 = 262144 positions, with
769
    // lots of positions being equivalent (because they are mirrors of each
770
    // other) and lots of positions being invalid (two pieces on one square,
771
    // adjacent kings, etc.).
772
    // Usually the first step is to take the wK and bK together. There are just
773
    // 462 ways legal and not-mirrored ways to place the wK and bK on the board.
774
    // Once we have placed the wK and bK, there are 62 squares left for the wR
775
    // Mapping its square from 0..63 to available squares 0..61 can be done like:
776
    //
777
    //   wR -= (wR > wK) + (wR > bK);
778
    //
779
    // In words: if wR "comes later" than wK, we deduct 1, and the same if wR
780
    // "comes later" than bK. In case of two same pieces like KRRvK we want to
781
    // place the two Rs "together". If we have 62 squares left, we can place two
782
    // Rs "together" in 62 * 61 / 2 ways (we divide by 2 because rooks can be
783
    // swapped and still get the same position.)
784
    //
785
    // In case we have at least 3 unique pieces (inlcuded kings) we encode them
786
    // together.
787
    if (entry->hasUniquePieces) {
96 pmbaty 788
 
169 pmbaty 789
        int adjust1 =  squares[1] > squares[0];
790
        int adjust2 = (squares[2] > squares[0]) + (squares[2] > squares[1]);
96 pmbaty 791
 
169 pmbaty 792
        // First piece is below a1-h8 diagonal. MapA1D1D4[] maps the b1-d1-d3
793
        // triangle to 0...5. There are 63 squares for second piece and and 62
794
        // (mapped to 0...61) for the third.
795
        if (off_A1H8(squares[0]))
796
            idx = (   MapA1D1D4[squares[0]]  * 63
797
                   + (squares[1] - adjust1)) * 62
798
                   +  squares[2] - adjust2;
799
 
800
        // First piece is on a1-h8 diagonal, second below: map this occurence to
801
        // 6 to differentiate from the above case, rank_of() maps a1-d4 diagonal
802
        // to 0...3 and finally MapB1H1H7[] maps the b1-h1-h7 triangle to 0..27.
803
        else if (off_A1H8(squares[1]))
804
            idx = (  6 * 63 + rank_of(squares[0]) * 28
805
                   + MapB1H1H7[squares[1]])       * 62
806
                   + squares[2] - adjust2;
807
 
808
        // First two pieces are on a1-h8 diagonal, third below
809
        else if (off_A1H8(squares[2]))
810
            idx =  6 * 63 * 62 + 4 * 28 * 62
811
                 +  rank_of(squares[0])        * 7 * 28
812
                 + (rank_of(squares[1]) - adjust1) * 28
813
                 +  MapB1H1H7[squares[2]];
814
 
815
        // All 3 pieces on the diagonal a1-h8
816
        else
817
            idx = 6 * 63 * 62 + 4 * 28 * 62 + 4 * 7 * 28
818
                 +  rank_of(squares[0])         * 7 * 6
819
                 + (rank_of(squares[1]) - adjust1)  * 6
820
                 + (rank_of(squares[2]) - adjust2);
821
    } else
822
        // We don't have at least 3 unique pieces, like in KRRvKBB, just map
823
        // the kings.
824
        idx = MapKK[MapA1D1D4[squares[0]]][squares[1]];
825
 
826
encode_remaining:
827
    idx *= d->groupIdx[0];
828
    Square* groupSq = squares + d->groupLen[0];
829
 
830
    // Encode remainig pawns then pieces according to square, in ascending order
185 pmbaty 831
    bool remainingPawns = entry->hasPawns && entry->pawnCount[1];
169 pmbaty 832
 
833
    while (d->groupLen[++next])
834
    {
835
        std::sort(groupSq, groupSq + d->groupLen[next]);
836
        uint64_t n = 0;
837
 
838
        // Map down a square if "comes later" than a square in the previous
839
        // groups (similar to what done earlier for leading group pieces).
840
        for (int i = 0; i < d->groupLen[next]; ++i)
841
        {
842
            auto f = [&](Square s) { return groupSq[i] > s; };
843
            auto adjust = std::count_if(squares, groupSq, f);
844
            n += Binomial[i + 1][groupSq[i] - adjust - 8 * remainingPawns];
845
        }
846
 
847
        remainingPawns = false;
848
        idx += n * d->groupIdx[next];
849
        groupSq += d->groupLen[next];
850
    }
851
 
852
    // Now that we have the index, decompress the pair and get the score
853
    return map_score(entry, tbFile, decompress_pairs(d, idx), wdl);
96 pmbaty 854
}
855
 
169 pmbaty 856
// Group together pieces that will be encoded together. The general rule is that
857
// a group contains pieces of same type and color. The exception is the leading
858
// group that, in case of positions withouth pawns, can be formed by 3 different
859
// pieces (default) or by the king pair when there is not a unique piece apart
860
// from the kings. When there are pawns, pawns are always first in pieces[].
861
//
862
// As example KRKN -> KRK + N, KNNK -> KK + NN, KPPKP -> P + PP + K + K
863
//
864
// The actual grouping depends on the TB generator and can be inferred from the
865
// sequence of pieces in piece[] array.
866
template<typename T>
867
void set_groups(T& e, PairsData* d, int order[], File f) {
96 pmbaty 868
 
169 pmbaty 869
    int n = 0, firstLen = e.hasPawns ? 0 : e.hasUniquePieces ? 3 : 2;
870
    d->groupLen[n] = 1;
871
 
872
    // Number of pieces per group is stored in groupLen[], for instance in KRKN
873
    // the encoder will default on '111', so groupLen[] will be (3, 1).
874
    for (int i = 1; i < e.pieceCount; ++i)
875
        if (--firstLen > 0 || d->pieces[i] == d->pieces[i - 1])
876
            d->groupLen[n]++;
877
        else
878
            d->groupLen[++n] = 1;
879
 
880
    d->groupLen[++n] = 0; // Zero-terminated
881
 
882
    // The sequence in pieces[] defines the groups, but not the order in which
883
    // they are encoded. If the pieces in a group g can be combined on the board
884
    // in N(g) different ways, then the position encoding will be of the form:
885
    //
886
    //           g1 * N(g2) * N(g3) + g2 * N(g3) + g3
887
    //
888
    // This ensures unique encoding for the whole position. The order of the
889
    // groups is a per-table parameter and could not follow the canonical leading
890
    // pawns/pieces -> remainig pawns -> remaining pieces. In particular the
891
    // first group is at order[0] position and the remaining pawns, when present,
892
    // are at order[1] position.
185 pmbaty 893
    bool pp = e.hasPawns && e.pawnCount[1]; // Pawns on both sides
169 pmbaty 894
    int next = pp ? 2 : 1;
895
    int freeSquares = 64 - d->groupLen[0] - (pp ? d->groupLen[1] : 0);
896
    uint64_t idx = 1;
897
 
898
    for (int k = 0; next < n || k == order[0] || k == order[1]; ++k)
899
        if (k == order[0]) // Leading pawns or pieces
900
        {
901
            d->groupIdx[0] = idx;
902
            idx *=         e.hasPawns ? LeadPawnsSize[d->groupLen[0]][f]
903
                  : e.hasUniquePieces ? 31332 : 462;
904
        }
905
        else if (k == order[1]) // Remaining pawns
906
        {
907
            d->groupIdx[1] = idx;
908
            idx *= Binomial[d->groupLen[1]][48 - d->groupLen[0]];
909
        }
910
        else // Remainig pieces
911
        {
912
            d->groupIdx[next] = idx;
913
            idx *= Binomial[d->groupLen[next]][freeSquares];
914
            freeSquares -= d->groupLen[next++];
915
        }
916
 
917
    d->groupIdx[n] = idx;
918
}
919
 
920
// In Recursive Pairing each symbol represents a pair of childern symbols. So
921
// read d->btree[] symbols data and expand each one in his left and right child
922
// symbol until reaching the leafs that represent the symbol value.
923
uint8_t set_symlen(PairsData* d, Sym s, std::vector<bool>& visited) {
924
 
925
    visited[s] = true; // We can set it now because tree is acyclic
926
    Sym sr = d->btree[s].get<LR::Right>();
927
 
928
    if (sr == 0xFFF)
929
        return 0;
930
 
931
    Sym sl = d->btree[s].get<LR::Left>();
932
 
933
    if (!visited[sl])
934
        d->symlen[sl] = set_symlen(d, sl, visited);
935
 
936
    if (!visited[sr])
937
        d->symlen[sr] = set_symlen(d, sr, visited);
938
 
939
    return d->symlen[sl] + d->symlen[sr] + 1;
940
}
941
 
942
uint8_t* set_sizes(PairsData* d, uint8_t* data) {
943
 
944
    d->flags = *data++;
945
 
946
    if (d->flags & TBFlag::SingleValue) {
947
        d->blocksNum = d->blockLengthSize = 0;
948
        d->span = d->sparseIndexSize = 0; // Broken MSVC zero-init
949
        d->minSymLen = *data++; // Here we store the single value
950
        return data;
96 pmbaty 951
    }
952
 
169 pmbaty 953
    // groupLen[] is a zero-terminated list of group lengths, the last groupIdx[]
954
    // element stores the biggest index that is the tb size.
955
    uint64_t tbSize = d->groupIdx[std::find(d->groupLen, d->groupLen + 7, 0) - d->groupLen];
956
 
957
    d->sizeofBlock = 1ULL << *data++;
958
    d->span = 1ULL << *data++;
185 pmbaty 959
    d->sparseIndexSize = (tbSize + d->span - 1) / d->span; // Round up
960
    auto padding = number<uint8_t, LittleEndian>(data++);
169 pmbaty 961
    d->blocksNum = number<uint32_t, LittleEndian>(data); data += sizeof(uint32_t);
962
    d->blockLengthSize = d->blocksNum + padding; // Padded to ensure SparseIndex[]
963
                                                 // does not point out of range.
964
    d->maxSymLen = *data++;
965
    d->minSymLen = *data++;
966
    d->lowestSym = (Sym*)data;
967
    d->base64.resize(d->maxSymLen - d->minSymLen + 1);
968
 
969
    // The canonical code is ordered such that longer symbols (in terms of
970
    // the number of bits of their Huffman code) have lower numeric value,
971
    // so that d->lowestSym[i] >= d->lowestSym[i+1] (when read as LittleEndian).
972
    // Starting from this we compute a base64[] table indexed by symbol length
973
    // and containing 64 bit values so that d->base64[i] >= d->base64[i+1].
974
    // See http://www.eecs.harvard.edu/~michaelm/E210/huffman.pdf
975
    for (int i = d->base64.size() - 2; i >= 0; --i) {
976
        d->base64[i] = (d->base64[i + 1] + number<Sym, LittleEndian>(&d->lowestSym[i])
977
                                         - number<Sym, LittleEndian>(&d->lowestSym[i + 1])) / 2;
978
 
979
        assert(d->base64[i] * 2 >= d->base64[i+1]);
980
    }
981
 
982
    // Now left-shift by an amount so that d->base64[i] gets shifted 1 bit more
983
    // than d->base64[i+1] and given the above assert condition, we ensure that
984
    // d->base64[i] >= d->base64[i+1]. Moreover for any symbol s64 of length i
985
    // and right-padded to 64 bits holds d->base64[i-1] >= s64 >= d->base64[i].
986
    for (size_t i = 0; i < d->base64.size(); ++i)
987
        d->base64[i] <<= 64 - i - d->minSymLen; // Right-padding to 64 bits
988
 
989
    data += d->base64.size() * sizeof(Sym);
990
    d->symlen.resize(number<uint16_t, LittleEndian>(data)); data += sizeof(uint16_t);
991
    d->btree = (LR*)data;
992
 
185 pmbaty 993
    // The compression scheme used is "Recursive Pairing", that replaces the most
169 pmbaty 994
    // frequent adjacent pair of symbols in the source message by a new symbol,
995
    // reevaluating the frequencies of all of the symbol pairs with respect to
996
    // the extended alphabet, and then repeating the process.
997
    // See http://www.larsson.dogma.net/dcc99.pdf
998
    std::vector<bool> visited(d->symlen.size());
999
 
1000
    for (Sym sym = 0; sym < d->symlen.size(); ++sym)
1001
        if (!visited[sym])
1002
            d->symlen[sym] = set_symlen(d, sym, visited);
1003
 
1004
    return data + d->symlen.size() * sizeof(LR) + (d->symlen.size() & 1);
96 pmbaty 1005
}
1006
 
185 pmbaty 1007
uint8_t* set_dtz_map(TBTable<WDL>&, uint8_t* data, File) { return data; }
96 pmbaty 1008
 
185 pmbaty 1009
uint8_t* set_dtz_map(TBTable<DTZ>& e, uint8_t* data, File maxFile) {
96 pmbaty 1010
 
185 pmbaty 1011
    e.map = data;
169 pmbaty 1012
 
1013
    for (File f = FILE_A; f <= maxFile; ++f) {
185 pmbaty 1014
        auto flags = e.get(0, f)->flags;
1015
        if (flags & TBFlag::Mapped) {
1016
            if (flags & TBFlag::Wide) {
1017
                data += (uintptr_t)data & 1;  // Word alignment, we may have a mixed table
1018
                for (int i = 0; i < 4; ++i) { // Sequence like 3,x,x,x,1,x,0,2,x,x
1019
                    e.get(0, f)->map_idx[i] = (uint16_t)((uint16_t *)data - (uint16_t *)e.map + 1);
1020
                    data += 2 * number<uint16_t, LittleEndian>(data) + 2;
1021
                }
169 pmbaty 1022
            }
185 pmbaty 1023
            else {
1024
                for (int i = 0; i < 4; ++i) {
1025
                    e.get(0, f)->map_idx[i] = (uint16_t)(data - e.map + 1);
1026
                    data += *data + 1;
1027
                }
1028
            }
1029
        }
96 pmbaty 1030
    }
1031
 
169 pmbaty 1032
    return data += (uintptr_t)data & 1; // Word alignment
96 pmbaty 1033
}
1034
 
185 pmbaty 1035
// Populate entry's PairsData records with data from the just memory mapped file.
1036
// Called at first access.
1037
template<typename T>
1038
void set(T& e, uint8_t* data) {
96 pmbaty 1039
 
169 pmbaty 1040
    PairsData* d;
96 pmbaty 1041
 
169 pmbaty 1042
    enum { Split = 1, HasPawns = 2 };
96 pmbaty 1043
 
169 pmbaty 1044
    assert(e.hasPawns        == !!(*data & HasPawns));
1045
    assert((e.key != e.key2) == !!(*data & Split));
96 pmbaty 1046
 
169 pmbaty 1047
    data++; // First byte stores flags
1048
 
185 pmbaty 1049
    const int sides = T::Sides == 2 && (e.key != e.key2) ? 2 : 1;
1050
    const File maxFile = e.hasPawns ? FILE_D : FILE_A;
169 pmbaty 1051
 
185 pmbaty 1052
    bool pp = e.hasPawns && e.pawnCount[1]; // Pawns on both sides
169 pmbaty 1053
 
185 pmbaty 1054
    assert(!pp || e.pawnCount[0]);
169 pmbaty 1055
 
185 pmbaty 1056
    for (File f = FILE_A; f <= maxFile; ++f) {
169 pmbaty 1057
 
185 pmbaty 1058
        for (int i = 0; i < sides; i++)
1059
            *e.get(i, f) = PairsData();
169 pmbaty 1060
 
1061
        int order[][2] = { { *data & 0xF, pp ? *(data + 1) & 0xF : 0xF },
1062
                           { *data >>  4, pp ? *(data + 1) >>  4 : 0xF } };
1063
        data += 1 + pp;
1064
 
1065
        for (int k = 0; k < e.pieceCount; ++k, ++data)
185 pmbaty 1066
            for (int i = 0; i < sides; i++)
1067
                e.get(i, f)->pieces[k] = Piece(i ? *data >>  4 : *data & 0xF);
169 pmbaty 1068
 
185 pmbaty 1069
        for (int i = 0; i < sides; ++i)
1070
            set_groups(e, e.get(i, f), order[i], f);
169 pmbaty 1071
    }
1072
 
1073
    data += (uintptr_t)data & 1; // Word alignment
1074
 
185 pmbaty 1075
    for (File f = FILE_A; f <= maxFile; ++f)
1076
        for (int i = 0; i < sides; i++)
1077
            data = set_sizes(e.get(i, f), data);
169 pmbaty 1078
 
185 pmbaty 1079
    data = set_dtz_map(e, data, maxFile);
169 pmbaty 1080
 
185 pmbaty 1081
    for (File f = FILE_A; f <= maxFile; ++f)
1082
        for (int i = 0; i < sides; i++) {
1083
            (d = e.get(i, f))->sparseIndex = (SparseEntry*)data;
169 pmbaty 1084
            data += d->sparseIndexSize * sizeof(SparseEntry);
96 pmbaty 1085
        }
169 pmbaty 1086
 
185 pmbaty 1087
    for (File f = FILE_A; f <= maxFile; ++f)
1088
        for (int i = 0; i < sides; i++) {
1089
            (d = e.get(i, f))->blockLength = (uint16_t*)data;
169 pmbaty 1090
            data += d->blockLengthSize * sizeof(uint16_t);
1091
        }
1092
 
185 pmbaty 1093
    for (File f = FILE_A; f <= maxFile; ++f)
1094
        for (int i = 0; i < sides; i++) {
169 pmbaty 1095
            data = (uint8_t*)(((uintptr_t)data + 0x3F) & ~0x3F); // 64 byte alignment
185 pmbaty 1096
            (d = e.get(i, f))->data = data;
169 pmbaty 1097
            data += d->blocksNum * d->sizeofBlock;
1098
        }
1099
}
1100
 
185 pmbaty 1101
// If the TB file corresponding to the given position is already memory mapped
1102
// then return its base address, otherwise try to memory map and init it. Called
1103
// at every probe, memory map and init only at first access. Function is thread
1104
// safe and can be called concurrently.
1105
template<TBType Type>
1106
void* mapped(TBTable<Type>& e, const Position& pos) {
169 pmbaty 1107
 
1108
    static Mutex mutex;
1109
 
185 pmbaty 1110
    // Use 'aquire' to avoid a thread reads 'ready' == true while another is
1111
    // still working, this could happen due to compiler reordering.
169 pmbaty 1112
    if (e.ready.load(std::memory_order_acquire))
185 pmbaty 1113
        return e.baseAddress; // Could be nullptr if file does not exsist
169 pmbaty 1114
 
1115
    std::unique_lock<Mutex> lk(mutex);
1116
 
1117
    if (e.ready.load(std::memory_order_relaxed)) // Recheck under lock
1118
        return e.baseAddress;
1119
 
1120
    // Pieces strings in decreasing order for each color, like ("KPP","KR")
1121
    std::string fname, w, b;
1122
    for (PieceType pt = KING; pt >= PAWN; --pt) {
1123
        w += std::string(popcount(pos.pieces(WHITE, pt)), PieceToChar[pt]);
1124
        b += std::string(popcount(pos.pieces(BLACK, pt)), PieceToChar[pt]);
96 pmbaty 1125
    }
1126
 
169 pmbaty 1127
    fname =  (e.key == pos.material_key() ? w + 'v' + b : b + 'v' + w)
185 pmbaty 1128
           + (Type == WDL ? ".rtbw" : ".rtbz");
169 pmbaty 1129
 
185 pmbaty 1130
    uint8_t* data = TBFile(fname).map(&e.baseAddress, &e.mapping, Type);
1131
 
169 pmbaty 1132
    if (data)
185 pmbaty 1133
        set(e, data);
169 pmbaty 1134
 
1135
    e.ready.store(true, std::memory_order_release);
1136
    return e.baseAddress;
96 pmbaty 1137
}
1138
 
185 pmbaty 1139
template<TBType Type, typename Ret = typename TBTable<Type>::Ret>
1140
Ret probe_table(const Position& pos, ProbeState* result, WDLScore wdl = WDLDraw) {
96 pmbaty 1141
 
185 pmbaty 1142
    if (pos.count<ALL_PIECES>() == 2) // KvK
1143
        return Ret(WDLDraw);
96 pmbaty 1144
 
185 pmbaty 1145
    TBTable<Type>* entry = TBTables.get<Type>(pos.material_key());
96 pmbaty 1146
 
185 pmbaty 1147
    if (!entry || !mapped(*entry, pos))
1148
        return *result = FAIL, Ret();
96 pmbaty 1149
 
169 pmbaty 1150
    return do_probe_table(pos, entry, wdl, result);
1151
}
96 pmbaty 1152
 
169 pmbaty 1153
// For a position where the side to move has a winning capture it is not necessary
1154
// to store a winning value so the generator treats such positions as "don't cares"
1155
// and tries to assign to it a value that improves the compression ratio. Similarly,
1156
// if the side to move has a drawing capture, then the position is at least drawn.
1157
// If the position is won, then the TB needs to store a win value. But if the
1158
// position is drawn, the TB may store a loss value if that is better for compression.
1159
// All of this means that during probing, the engine must look at captures and probe
1160
// their results and must probe the position itself. The "best" result of these
1161
// probes is the correct result for the position.
185 pmbaty 1162
// DTZ tables do not store values when a following move is a zeroing winning move
169 pmbaty 1163
// (winning capture or winning pawn move). Also DTZ store wrong values for positions
1164
// where the best move is an ep-move (even if losing). So in all these cases set
1165
// the state to ZEROING_BEST_MOVE.
185 pmbaty 1166
template<bool CheckZeroingMoves>
169 pmbaty 1167
WDLScore search(Position& pos, ProbeState* result) {
1168
 
1169
    WDLScore value, bestValue = WDLLoss;
1170
    StateInfo st;
1171
 
1172
    auto moveList = MoveList<LEGAL>(pos);
1173
    size_t totalCount = moveList.size(), moveCount = 0;
1174
 
1175
    for (const Move& move : moveList)
1176
    {
1177
        if (   !pos.capture(move)
1178
            && (!CheckZeroingMoves || type_of(pos.moved_piece(move)) != PAWN))
1179
            continue;
1180
 
1181
        moveCount++;
1182
 
1183
        pos.do_move(move, st);
185 pmbaty 1184
        value = -search<false>(pos, result);
169 pmbaty 1185
        pos.undo_move(move);
1186
 
1187
        if (*result == FAIL)
1188
            return WDLDraw;
1189
 
1190
        if (value > bestValue)
1191
        {
1192
            bestValue = value;
1193
 
1194
            if (value >= WDLWin)
1195
            {
1196
                *result = ZEROING_BEST_MOVE; // Winning DTZ-zeroing move
1197
                return value;
1198
            }
1199
        }
1200
    }
1201
 
1202
    // In case we have already searched all the legal moves we don't have to probe
1203
    // the TB because the stored score could be wrong. For instance TB tables
1204
    // do not contain information on position with ep rights, so in this case
1205
    // the result of probe_wdl_table is wrong. Also in case of only capture
1206
    // moves, for instance here 4K3/4q3/6p1/2k5/6p1/8/8/8 w - - 0 7, we have to
1207
    // return with ZEROING_BEST_MOVE set.
1208
    bool noMoreMoves = (moveCount && moveCount == totalCount);
1209
 
1210
    if (noMoreMoves)
1211
        value = bestValue;
96 pmbaty 1212
    else
169 pmbaty 1213
    {
185 pmbaty 1214
        value = probe_table<WDL>(pos, result);
96 pmbaty 1215
 
169 pmbaty 1216
        if (*result == FAIL)
1217
            return WDLDraw;
96 pmbaty 1218
    }
1219
 
169 pmbaty 1220
    // DTZ stores a "don't care" value if bestValue is a win
1221
    if (bestValue >= value)
1222
        return *result = (   bestValue > WDLDraw
1223
                          || noMoreMoves ? ZEROING_BEST_MOVE : OK), bestValue;
96 pmbaty 1224
 
169 pmbaty 1225
    return *result = OK, value;
1226
}
1227
 
1228
} // namespace
1229
 
185 pmbaty 1230
 
1231
/// Tablebases::init() is called at startup and after every change to
1232
/// "SyzygyPath" UCI option to (re)create the various tables. It is not thread
1233
/// safe, nor it needs to be.
169 pmbaty 1234
void Tablebases::init(const std::string& paths) {
1235
 
185 pmbaty 1236
    TBTables.clear();
169 pmbaty 1237
    MaxCardinality = 0;
1238
    TBFile::Paths = paths;
1239
 
185 pmbaty 1240
    if (paths.empty() || paths == "<empty>")
169 pmbaty 1241
        return;
1242
 
1243
    // MapB1H1H7[] encodes a square below a1-h8 diagonal to 0..27
1244
    int code = 0;
1245
    for (Square s = SQ_A1; s <= SQ_H8; ++s)
1246
        if (off_A1H8(s) < 0)
1247
            MapB1H1H7[s] = code++;
1248
 
1249
    // MapA1D1D4[] encodes a square in the a1-d1-d4 triangle to 0..9
1250
    std::vector<Square> diagonal;
1251
    code = 0;
1252
    for (Square s = SQ_A1; s <= SQ_D4; ++s)
1253
        if (off_A1H8(s) < 0 && file_of(s) <= FILE_D)
1254
            MapA1D1D4[s] = code++;
1255
 
1256
        else if (!off_A1H8(s) && file_of(s) <= FILE_D)
1257
            diagonal.push_back(s);
1258
 
1259
    // Diagonal squares are encoded as last ones
1260
    for (auto s : diagonal)
1261
        MapA1D1D4[s] = code++;
1262
 
1263
    // MapKK[] encodes all the 461 possible legal positions of two kings where
1264
    // the first is in the a1-d1-d4 triangle. If the first king is on the a1-d4
1265
    // diagonal, the other one shall not to be above the a1-h8 diagonal.
1266
    std::vector<std::pair<int, Square>> bothOnDiagonal;
1267
    code = 0;
1268
    for (int idx = 0; idx < 10; idx++)
1269
        for (Square s1 = SQ_A1; s1 <= SQ_D4; ++s1)
1270
            if (MapA1D1D4[s1] == idx && (idx || s1 == SQ_B1)) // SQ_B1 is mapped to 0
1271
            {
1272
                for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
1273
                    if ((PseudoAttacks[KING][s1] | s1) & s2)
1274
                        continue; // Illegal position
1275
 
1276
                    else if (!off_A1H8(s1) && off_A1H8(s2) > 0)
1277
                        continue; // First on diagonal, second above
1278
 
1279
                    else if (!off_A1H8(s1) && !off_A1H8(s2))
185 pmbaty 1280
                        bothOnDiagonal.emplace_back(idx, s2);
169 pmbaty 1281
 
1282
                    else
1283
                        MapKK[idx][s2] = code++;
1284
            }
1285
 
1286
    // Legal positions with both kings on diagonal are encoded as last ones
1287
    for (auto p : bothOnDiagonal)
1288
        MapKK[p.first][p.second] = code++;
1289
 
1290
    // Binomial[] stores the Binomial Coefficents using Pascal rule. There
1291
    // are Binomial[k][n] ways to choose k elements from a set of n elements.
1292
    Binomial[0][0] = 1;
1293
 
1294
    for (int n = 1; n < 64; n++) // Squares
1295
        for (int k = 0; k < 6 && k <= n; ++k) // Pieces
1296
            Binomial[k][n] =  (k > 0 ? Binomial[k - 1][n - 1] : 0)
1297
                            + (k < n ? Binomial[k    ][n - 1] : 0);
1298
 
1299
    // MapPawns[s] encodes squares a2-h7 to 0..47. This is the number of possible
1300
    // available squares when the leading one is in 's'. Moreover the pawn with
1301
    // highest MapPawns[] is the leading pawn, the one nearest the edge and,
1302
    // among pawns with same file, the one with lowest rank.
1303
    int availableSquares = 47; // Available squares when lead pawn is in a2
1304
 
185 pmbaty 1305
    // Init the tables for the encoding of leading pawns group: with 7-men TB we
1306
    // can have up to 5 leading pawns (KPPPPPK).
1307
    for (int leadPawnsCnt = 1; leadPawnsCnt <= 5; ++leadPawnsCnt)
169 pmbaty 1308
        for (File f = FILE_A; f <= FILE_D; ++f)
1309
        {
1310
            // Restart the index at every file because TB table is splitted
1311
            // by file, so we can reuse the same index for different files.
1312
            int idx = 0;
1313
 
1314
            // Sum all possible combinations for a given file, starting with
1315
            // the leading pawn on rank 2 and increasing the rank.
1316
            for (Rank r = RANK_2; r <= RANK_7; ++r)
1317
            {
1318
                Square sq = make_square(f, r);
1319
 
1320
                // Compute MapPawns[] at first pass.
1321
                // If sq is the leading pawn square, any other pawn cannot be
1322
                // below or more toward the edge of sq. There are 47 available
1323
                // squares when sq = a2 and reduced by 2 for any rank increase
1324
                // due to mirroring: sq == a3 -> no a2, h2, so MapPawns[a3] = 45
1325
                if (leadPawnsCnt == 1)
1326
                {
1327
                    MapPawns[sq] = availableSquares--;
1328
                    MapPawns[sq ^ 7] = availableSquares--; // Horizontal flip
1329
                }
1330
                LeadPawnIdx[leadPawnsCnt][sq] = idx;
1331
                idx += Binomial[leadPawnsCnt - 1][MapPawns[sq]];
1332
            }
1333
            // After a file is traversed, store the cumulated per-file index
1334
            LeadPawnsSize[leadPawnsCnt][f] = idx;
96 pmbaty 1335
        }
169 pmbaty 1336
 
185 pmbaty 1337
    // Add entries in TB tables if the corresponding ".rtbw" file exsists
169 pmbaty 1338
    for (PieceType p1 = PAWN; p1 < KING; ++p1) {
185 pmbaty 1339
        TBTables.add({KING, p1, KING});
169 pmbaty 1340
 
1341
        for (PieceType p2 = PAWN; p2 <= p1; ++p2) {
185 pmbaty 1342
            TBTables.add({KING, p1, p2, KING});
1343
            TBTables.add({KING, p1, KING, p2});
169 pmbaty 1344
 
1345
            for (PieceType p3 = PAWN; p3 < KING; ++p3)
185 pmbaty 1346
                TBTables.add({KING, p1, p2, KING, p3});
169 pmbaty 1347
 
1348
            for (PieceType p3 = PAWN; p3 <= p2; ++p3) {
185 pmbaty 1349
                TBTables.add({KING, p1, p2, p3, KING});
169 pmbaty 1350
 
185 pmbaty 1351
                for (PieceType p4 = PAWN; p4 <= p3; ++p4) {
1352
                    TBTables.add({KING, p1, p2, p3, p4, KING});
169 pmbaty 1353
 
185 pmbaty 1354
                    for (PieceType p5 = PAWN; p5 <= p4; ++p5)
1355
                        TBTables.add({KING, p1, p2, p3, p4, p5, KING});
1356
 
1357
                    for (PieceType p5 = PAWN; p5 < KING; ++p5)
1358
                        TBTables.add({KING, p1, p2, p3, p4, KING, p5});
1359
                }
1360
 
1361
                for (PieceType p4 = PAWN; p4 < KING; ++p4) {
1362
                    TBTables.add({KING, p1, p2, p3, KING, p4});
1363
 
1364
                    for (PieceType p5 = PAWN; p5 <= p4; ++p5)
1365
                        TBTables.add({KING, p1, p2, p3, KING, p4, p5});
1366
                }
169 pmbaty 1367
            }
1368
 
1369
            for (PieceType p3 = PAWN; p3 <= p1; ++p3)
1370
                for (PieceType p4 = PAWN; p4 <= (p1 == p3 ? p2 : p3); ++p4)
185 pmbaty 1371
                    TBTables.add({KING, p1, p2, KING, p3, p4});
169 pmbaty 1372
        }
96 pmbaty 1373
    }
169 pmbaty 1374
 
185 pmbaty 1375
    sync_cout << "info string Found " << TBTables.size() << " tablebases" << sync_endl;
96 pmbaty 1376
}
1377
 
169 pmbaty 1378
// Probe the WDL table for a particular position.
1379
// If *result != FAIL, the probe was successful.
1380
// The return value is from the point of view of the side to move:
1381
// -2 : loss
1382
// -1 : loss, but draw under 50-move rule
1383
//  0 : draw
1384
//  1 : win, but draw under 50-move rule
1385
//  2 : win
1386
WDLScore Tablebases::probe_wdl(Position& pos, ProbeState* result) {
96 pmbaty 1387
 
169 pmbaty 1388
    *result = OK;
185 pmbaty 1389
    return search<false>(pos, result);
169 pmbaty 1390
}
1391
 
96 pmbaty 1392
// Probe the DTZ table for a particular position.
169 pmbaty 1393
// If *result != FAIL, the probe was successful.
96 pmbaty 1394
// The return value is from the point of view of the side to move:
1395
//         n < -100 : loss, but draw under 50-move rule
1396
// -100 <= n < -1   : loss in n ply (assuming 50-move counter == 0)
185 pmbaty 1397
//        -1        : loss, the side to move is mated
96 pmbaty 1398
//         0        : draw
1399
//     1 < n <= 100 : win in n ply (assuming 50-move counter == 0)
1400
//   100 < n        : win, but draw under 50-move rule
1401
//
1402
// The return value n can be off by 1: a return value -n can mean a loss
1403
// in n+1 ply and a return value +n can mean a win in n+1 ply. This
1404
// cannot happen for tables with positions exactly on the "edge" of
1405
// the 50-move rule.
1406
//
1407
// This implies that if dtz > 0 is returned, the position is certainly
1408
// a win if dtz + 50-move-counter <= 99. Care must be taken that the engine
1409
// picks moves that preserve dtz + 50-move-counter <= 99.
1410
//
1411
// If n = 100 immediately after a capture or pawn move, then the position
1412
// is also certainly a win, and during the whole phase until the next
1413
// capture or pawn move, the inequality to be preserved is
1414
// dtz + 50-movecounter <= 100.
1415
//
1416
// In short, if a move is available resulting in dtz + 50-move-counter <= 99,
1417
// then do not accept moves leading to dtz + 50-move-counter == 100.
169 pmbaty 1418
int Tablebases::probe_dtz(Position& pos, ProbeState* result) {
96 pmbaty 1419
 
169 pmbaty 1420
    *result = OK;
1421
    WDLScore wdl = search<true>(pos, result);
96 pmbaty 1422
 
169 pmbaty 1423
    if (*result == FAIL || wdl == WDLDraw) // DTZ tables don't store draws
1424
        return 0;
96 pmbaty 1425
 
169 pmbaty 1426
    // DTZ stores a 'don't care' value in this case, or even a plain wrong
1427
    // one as in case the best move is a losing ep, so it cannot be probed.
1428
    if (*result == ZEROING_BEST_MOVE)
1429
        return dtz_before_zeroing(wdl);
96 pmbaty 1430
 
185 pmbaty 1431
    int dtz = probe_table<DTZ>(pos, result, wdl);
96 pmbaty 1432
 
169 pmbaty 1433
    if (*result == FAIL)
1434
        return 0;
1435
 
1436
    if (*result != CHANGE_STM)
1437
        return (dtz + 100 * (wdl == WDLBlessedLoss || wdl == WDLCursedWin)) * sign_of(wdl);
1438
 
1439
    // DTZ stores results for the other side, so we need to do a 1-ply search and
1440
    // find the winning move that minimizes DTZ.
1441
    StateInfo st;
1442
    int minDTZ = 0xFFFF;
1443
 
1444
    for (const Move& move : MoveList<LEGAL>(pos))
1445
    {
1446
        bool zeroing = pos.capture(move) || type_of(pos.moved_piece(move)) == PAWN;
1447
 
1448
        pos.do_move(move, st);
1449
 
1450
        // For zeroing moves we want the dtz of the move _before_ doing it,
1451
        // otherwise we will get the dtz of the next move sequence. Search the
1452
        // position after the move to get the score sign (because even in a
1453
        // winning position we could make a losing capture or going for a draw).
185 pmbaty 1454
        dtz = zeroing ? -dtz_before_zeroing(search<false>(pos, result))
169 pmbaty 1455
                      : -probe_dtz(pos, result);
1456
 
185 pmbaty 1457
        // If the move mates, force minDTZ to 1
1458
        if (dtz == 1 && pos.checkers() && MoveList<LEGAL>(pos).size() == 0)
1459
            minDTZ = 1;
169 pmbaty 1460
 
1461
        // Convert result from 1-ply search. Zeroing moves are already accounted
1462
        // by dtz_before_zeroing() that returns the DTZ of the previous move.
1463
        if (!zeroing)
1464
            dtz += sign_of(dtz);
1465
 
1466
        // Skip the draws and if we are winning only pick positive dtz
1467
        if (dtz < minDTZ && sign_of(dtz) == sign_of(wdl))
1468
            minDTZ = dtz;
185 pmbaty 1469
 
1470
        pos.undo_move(move);
1471
 
1472
        if (*result == FAIL)
1473
            return 0;
96 pmbaty 1474
    }
1475
 
185 pmbaty 1476
    // When there are no legal moves, the position is mate: we return -1
169 pmbaty 1477
    return minDTZ == 0xFFFF ? -1 : minDTZ;
96 pmbaty 1478
}
1479
 
169 pmbaty 1480
 
185 pmbaty 1481
// Use the DTZ tables to rank root moves.
96 pmbaty 1482
//
185 pmbaty 1483
// A return value false indicates that not all probes were successful.
1484
bool Tablebases::root_probe(Position& pos, Search::RootMoves& rootMoves) {
96 pmbaty 1485
 
169 pmbaty 1486
    ProbeState result;
185 pmbaty 1487
    StateInfo st;
96 pmbaty 1488
 
185 pmbaty 1489
    // Obtain 50-move counter for the root position
1490
    int cnt50 = pos.rule50_count();
96 pmbaty 1491
 
185 pmbaty 1492
    // Check whether a position was repeated since the last zeroing move.
1493
    bool rep = pos.has_repeated();
169 pmbaty 1494
 
185 pmbaty 1495
    int dtz, bound = Options["Syzygy50MoveRule"] ? 900 : 1;
169 pmbaty 1496
 
185 pmbaty 1497
    // Probe and rank each move
1498
    for (auto& m : rootMoves)
1499
    {
1500
        pos.do_move(m.pv[0], st);
169 pmbaty 1501
 
185 pmbaty 1502
        // Calculate dtz for the current move counting from the root position
1503
        if (pos.rule50_count() == 0)
1504
        {
1505
            // In case of a zeroing move, dtz is one of -101/-1/0/1/101
1506
            WDLScore wdl = -probe_wdl(pos, &result);
1507
            dtz = dtz_before_zeroing(wdl);
169 pmbaty 1508
        }
185 pmbaty 1509
        else
1510
        {
1511
            // Otherwise, take dtz for the new position and correct by 1 ply
1512
            dtz = -probe_dtz(pos, &result);
1513
            dtz =  dtz > 0 ? dtz + 1
1514
                 : dtz < 0 ? dtz - 1 : dtz;
1515
        }
169 pmbaty 1516
 
185 pmbaty 1517
        // Make sure that a mating move is assigned a dtz value of 1
1518
        if (   pos.checkers()
1519
            && dtz == 2
1520
            && MoveList<LEGAL>(pos).size() == 0)
1521
            dtz = 1;
169 pmbaty 1522
 
185 pmbaty 1523
        pos.undo_move(m.pv[0]);
169 pmbaty 1524
 
1525
        if (result == FAIL)
1526
            return false;
1527
 
185 pmbaty 1528
        // Better moves are ranked higher. Certain wins are ranked equally.
1529
        // Losing moves are ranked equally unless a 50-move draw is in sight.
1530
        int r =  dtz > 0 ? (dtz + cnt50 <= 99 && !rep ? 1000 : 1000 - (dtz + cnt50))
1531
               : dtz < 0 ? (-dtz * 2 + cnt50 < 100 ? -1000 : -1000 + (-dtz + cnt50))
1532
               : 0;
1533
        m.tbRank = r;
96 pmbaty 1534
 
185 pmbaty 1535
        // Determine the score to be displayed for this move. Assign at least
1536
        // 1 cp to cursed wins and let it grow to 49 cp as the positions gets
1537
        // closer to a real win.
1538
        m.tbScore =  r >= bound ? VALUE_MATE - MAX_PLY - 1
1539
                   : r >  0     ? Value((std::max( 3, r - 800) * int(PawnValueEg)) / 200)
1540
                   : r == 0     ? VALUE_DRAW
1541
                   : r > -bound ? Value((std::min(-3, r + 800) * int(PawnValueEg)) / 200)
1542
                   :             -VALUE_MATE + MAX_PLY + 1;
96 pmbaty 1543
    }
1544
 
169 pmbaty 1545
    return true;
96 pmbaty 1546
}
1547
 
185 pmbaty 1548
 
1549
// Use the WDL tables to rank root moves.
96 pmbaty 1550
// This is a fallback for the case that some or all DTZ tables are missing.
1551
//
185 pmbaty 1552
// A return value false indicates that not all probes were successful.
1553
bool Tablebases::root_probe_wdl(Position& pos, Search::RootMoves& rootMoves) {
96 pmbaty 1554
 
185 pmbaty 1555
    static const int WDL_to_rank[] = { -1000, -899, 0, 899, 1000 };
96 pmbaty 1556
 
185 pmbaty 1557
    ProbeState result;
1558
    StateInfo st;
96 pmbaty 1559
 
185 pmbaty 1560
    bool rule50 = Options["Syzygy50MoveRule"];
96 pmbaty 1561
 
185 pmbaty 1562
    // Probe and rank each move
1563
    for (auto& m : rootMoves)
1564
    {
1565
        pos.do_move(m.pv[0], st);
96 pmbaty 1566
 
185 pmbaty 1567
        WDLScore wdl = -probe_wdl(pos, &result);
96 pmbaty 1568
 
185 pmbaty 1569
        pos.undo_move(m.pv[0]);
169 pmbaty 1570
 
1571
        if (result == FAIL)
1572
            return false;
1573
 
185 pmbaty 1574
        m.tbRank = WDL_to_rank[wdl + 2];
169 pmbaty 1575
 
185 pmbaty 1576
        if (!rule50)
1577
            wdl =  wdl > WDLDraw ? WDLWin
1578
                 : wdl < WDLDraw ? WDLLoss : WDLDraw;
1579
        m.tbScore = WDL_to_value[wdl + 2];
169 pmbaty 1580
    }
1581
 
1582
    return true;
96 pmbaty 1583
}