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