]> git.sesse.net Git - stockfish/blob - src/syzygy/tbcore.h
MSVC compiling fixes
[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
26 #define BSWAP32(v) __builtin_bswap32(v)
27 #define BSWAP64(v) __builtin_bswap64(v)
28 #else
29 #define LOCK_T HANDLE
30 #define LOCK_INIT(x) do { x = CreateMutex(NULL, FALSE, NULL); } while (0)
31 #define LOCK(x) WaitForSingleObject(x, INFINITE)
32 #define UNLOCK(x) ReleaseMutex(x)
33
34 #define BSWAP32(v) _byteswap_ulong(v)
35 #define BSWAP64(v) _byteswap_uint64(v)
36 #endif
37
38 #define WDLSUFFIX ".rtbw"
39 #define DTZSUFFIX ".rtbz"
40 #define WDLDIR "RTBWDIR"
41 #define DTZDIR "RTBZDIR"
42 #define TBPIECES 6
43
44 typedef unsigned long long uint64;
45 typedef unsigned int uint32;
46 typedef unsigned char ubyte;
47 typedef unsigned short ushort;
48
49 const ubyte WDL_MAGIC[4] = { 0x71, 0xe8, 0x23, 0x5d };
50 const ubyte DTZ_MAGIC[4] = { 0xd7, 0x66, 0x0c, 0xa5 };
51
52 #define TBHASHBITS 10
53
54 struct TBHashEntry;
55
56 typedef uint64 base_t;
57
58 struct PairsData {
59   char *indextable;
60   ushort *sizetable;
61   ubyte *data;
62   ushort *offset;
63   ubyte *symlen;
64   ubyte *sympat;
65   int blocksize;
66   int idxbits;
67   int min_len;
68   base_t base[1]; // C++ complains about base[]...
69 };
70
71 struct TBEntry {
72   char *data;
73   uint64 key;
74   uint64 mapping;
75   ubyte ready;
76   ubyte num;
77   ubyte symmetric;
78   ubyte has_pawns;
79 }
80 #ifndef _WIN32
81 __attribute__((__may_alias__))
82 #endif
83 ;
84
85 struct TBEntry_piece {
86   char *data;
87   uint64 key;
88   uint64 mapping;
89   ubyte ready;
90   ubyte num;
91   ubyte symmetric;
92   ubyte has_pawns;
93   ubyte enc_type;
94   struct PairsData *precomp[2];
95   int factor[2][TBPIECES];
96   ubyte pieces[2][TBPIECES];
97   ubyte norm[2][TBPIECES];
98 };
99
100 struct TBEntry_pawn {
101   char *data;
102   uint64 key;
103   uint64 mapping;
104   ubyte ready;
105   ubyte num;
106   ubyte symmetric;
107   ubyte has_pawns;
108   ubyte pawns[2];
109   struct {
110     struct PairsData *precomp[2];
111     int factor[2][TBPIECES];
112     ubyte pieces[2][TBPIECES];
113     ubyte norm[2][TBPIECES];
114   } file[4];
115 };
116
117 struct DTZEntry_piece {
118   char *data;
119   uint64 key;
120   uint64 mapping;
121   ubyte ready;
122   ubyte num;
123   ubyte symmetric;
124   ubyte has_pawns;
125   ubyte enc_type;
126   struct PairsData *precomp;
127   int factor[TBPIECES];
128   ubyte pieces[TBPIECES];
129   ubyte norm[TBPIECES];
130   ubyte flags; // accurate, mapped, side
131   ushort map_idx[4];
132   ubyte *map;
133 };
134
135 struct DTZEntry_pawn {
136   char *data;
137   uint64 key;
138   uint64 mapping;
139   ubyte ready;
140   ubyte num;
141   ubyte symmetric;
142   ubyte has_pawns;
143   ubyte pawns[2];
144   struct {
145     struct PairsData *precomp;
146     int factor[TBPIECES];
147     ubyte pieces[TBPIECES];
148     ubyte norm[TBPIECES];
149   } file[4];
150   ubyte flags[4];
151   ushort map_idx[4][4];
152   ubyte *map;
153 };
154
155 struct TBHashEntry {
156   uint64 key;
157   struct TBEntry *ptr;
158 };
159
160 struct DTZTableEntry {
161   uint64 key1;
162   uint64 key2;
163   struct TBEntry *entry;
164 };
165
166 #endif
167