]> git.sesse.net Git - stockfish/blob - src/syzygy/tbcore.h
Syzygy tablebases
[stockfish] / src / syzygy / tbcore.h
1 /*
2   Copyright (c) 2011-2013 Ronald de Man
3 */
4
5 #ifndef TBCORE_H
6 #define TBCORE_H
7
8 #ifndef __WIN32__
9 #include <pthread.h>
10 #define SEP_CHAR ':'
11 #define FD int
12 #define FD_ERR -1
13 #else
14 #include <windows.h>
15 #define SEP_CHAR ';'
16 #define FD HANDLE
17 #define FD_ERR INVALID_HANDLE_VALUE
18 #endif
19
20 #ifndef __WIN32__
21 #define LOCK_T pthread_mutex_t
22 #define LOCK_INIT(x) pthread_mutex_init(&(x), NULL)
23 #define LOCK(x) pthread_mutex_lock(&(x))
24 #define UNLOCK(x) pthread_mutex_unlock(&(x))
25 #else
26 #define LOCK_T HANDLE
27 #define LOCK_INIT(x) do { x = CreateMutex(NULL, FALSE, NULL); } while (0)
28 #define LOCK(x) WaitForSingleObject(x, INFINITE)
29 #define UNLOCK(x) ReleaseMutex(x)
30 #endif
31
32 #define WDLSUFFIX ".rtbw"
33 #define DTZSUFFIX ".rtbz"
34 #define WDLDIR "RTBWDIR"
35 #define DTZDIR "RTBZDIR"
36 #define TBPIECES 6
37
38 typedef unsigned long long uint64;
39 typedef unsigned int uint32;
40 typedef unsigned char ubyte;
41 typedef unsigned short ushort;
42
43 const ubyte WDL_MAGIC[4] = { 0x71, 0xe8, 0x23, 0x5d };
44 const ubyte DTZ_MAGIC[4] = { 0xd7, 0x66, 0x0c, 0xa5 };
45
46 #define TBHASHBITS 10
47
48 struct TBHashEntry;
49
50 typedef uint64 base_t;
51
52 struct PairsData {
53   char *indextable;
54   ushort *sizetable;
55   ubyte *data;
56   ushort *offset;
57   ubyte *symlen;
58   ubyte *sympat;
59   int blocksize;
60   int idxbits;
61   int min_len;
62   base_t base[1]; // C++ complains about base[]...
63 };
64
65 struct TBEntry {
66   char *data;
67   uint64 key;
68   uint64 mapping;
69   ubyte ready;
70   ubyte num;
71   ubyte symmetric;
72   ubyte has_pawns;
73 } __attribute__((__may_alias__));
74
75 struct TBEntry_piece {
76   char *data;
77   uint64 key;
78   uint64 mapping;
79   ubyte ready;
80   ubyte num;
81   ubyte symmetric;
82   ubyte has_pawns;
83   ubyte enc_type;
84   struct PairsData *precomp[2];
85   int factor[2][TBPIECES];
86   ubyte pieces[2][TBPIECES];
87   ubyte norm[2][TBPIECES];
88 };
89
90 struct TBEntry_pawn {
91   char *data;
92   uint64 key;
93   uint64 mapping;
94   ubyte ready;
95   ubyte num;
96   ubyte symmetric;
97   ubyte has_pawns;
98   ubyte pawns[2];
99   struct {
100     struct PairsData *precomp[2];
101     int factor[2][TBPIECES];
102     ubyte pieces[2][TBPIECES];
103     ubyte norm[2][TBPIECES];
104   } file[4];
105 };
106
107 struct DTZEntry_piece {
108   char *data;
109   uint64 key;
110   uint64 mapping;
111   ubyte ready;
112   ubyte num;
113   ubyte symmetric;
114   ubyte has_pawns;
115   ubyte enc_type;
116   struct PairsData *precomp;
117   int factor[TBPIECES];
118   ubyte pieces[TBPIECES];
119   ubyte norm[TBPIECES];
120   ubyte flags; // accurate, mapped, side
121   ushort map_idx[4];
122   ubyte *map;
123 };
124
125 struct DTZEntry_pawn {
126   char *data;
127   uint64 key;
128   uint64 mapping;
129   ubyte ready;
130   ubyte num;
131   ubyte symmetric;
132   ubyte has_pawns;
133   ubyte pawns[2];
134   struct {
135     struct PairsData *precomp;
136     int factor[TBPIECES];
137     ubyte pieces[TBPIECES];
138     ubyte norm[TBPIECES];
139   } file[4];
140   ubyte flags[4];
141   ushort map_idx[4][4];
142   ubyte *map;
143 };
144
145 struct TBHashEntry {
146   uint64 key;
147   struct TBEntry *ptr;
148 };
149
150 struct DTZTableEntry {
151   uint64 key1;
152   uint64 key2;
153   struct TBEntry *entry;
154 };
155
156 #endif
157