-using namespace Tablebases;
-
-int Tablebases::MaxCardinality;
-
-namespace {
-
-// Each table has a set of flags: all of them refer to DTZ tables, the last one to WDL tables
-enum TBFlag { STM = 1, Mapped = 2, WinPlies = 4, LossPlies = 8, SingleValue = 128 };
-
-inline WDLScore operator-(WDLScore d) { return WDLScore(-int(d)); }
-inline Square operator^=(Square& s, int i) { return s = Square(int(s) ^ i); }
-inline Square operator^(Square s, int i) { return Square(int(s) ^ i); }
-
-// DTZ tables don't store valid scores for moves that reset the rule50 counter
-// like captures and pawn moves but we can easily recover the correct dtz of the
-// previous move if we know the position's WDL score.
-int dtz_before_zeroing(WDLScore wdl) {
- return wdl == WDLWin ? 1 :
- wdl == WDLCursedWin ? 101 :
- wdl == WDLCursedLoss ? -101 :
- wdl == WDLLoss ? -1 : 0;
-}
-
-// Return the sign of a number (-1, 0, 1)
-template <typename T> int sign_of(T val) {
- return (T(0) < val) - (val < T(0));
-}
-
-// Numbers in little endian used by sparseIndex[] to point into blockLength[]
-struct SparseEntry {
- char block[4]; // Number of block
- char offset[2]; // Offset within the block
-};
-
-static_assert(sizeof(SparseEntry) == 6, "SparseEntry must be 6 bytes");
-
-typedef uint16_t Sym; // Huffman symbol
-
-struct LR {
- enum Side { Left, Right, Value };
-
- uint8_t lr[3]; // The first 12 bits is the left-hand symbol, the second 12
- // bits is the right-hand symbol. If symbol has length 1,
- // then the first byte is the stored value.
- template<Side S>
- Sym get() {
- return S == Left ? ((lr[1] & 0xF) << 8) | lr[0] :
- S == Right ? (lr[2] << 4) | (lr[1] >> 4) :
- S == Value ? lr[0] : (assert(false), Sym(-1));
- }
-};
-
-static_assert(sizeof(LR) == 3, "LR tree entry must be 3 bytes");
-
-const int TBPIECES = 6;
-
-struct PairsData {
- int flags;
- size_t sizeofBlock; // Block size in bytes
- size_t span; // About every span values there is a SparseIndex[] entry
- int blocksNum; // Number of blocks in the TB file
- int maxSymLen; // Maximum length in bits of the Huffman symbols
- int minSymLen; // Minimum length in bits of the Huffman symbols
- Sym* lowestSym; // lowestSym[l] is the symbol of length l with the lowest value
- LR* btree; // btree[sym] stores the left and right symbols that expand sym
- uint16_t* blockLength; // Number of stored positions (minus one) for each block: 1..65536
- int blockLengthSize; // Size of blockLength[] table: padded so it's bigger than blocksNum
- SparseEntry* sparseIndex; // Partial indices into blockLength[]
- size_t sparseIndexSize; // Size of SparseIndex[] table
- uint8_t* data; // Start of Huffman compressed data
- std::vector<uint64_t> base64; // base64[l - min_sym_len] is the 64bit-padded lowest symbol of length l
- std::vector<uint8_t> symlen; // Number of values (-1) represented by a given Huffman symbol: 1..256
- Piece pieces[TBPIECES]; // Position pieces: the order of pieces defines the groups
- uint64_t groupIdx[TBPIECES+1]; // Start index used for the encoding of the group's pieces
- int groupLen[TBPIECES+1]; // Number of pieces in a given group: KRKN -> (3, 1)
-};
-
-// Helper struct to avoid manually defining entry copy constructor as we
-// should because the default one is not compatible with std::atomic_bool.
-struct Atomic {
- Atomic() = default;
- Atomic(const Atomic& e) { ready = e.ready.load(); } // MSVC 2013 wants assignment within body
- std::atomic_bool ready;
-};