Cute Chess 0.1
tbprobe.h
1/*
2 * tbprobe.h
3 * (C) 2015 basil, all rights reserved,
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#ifndef TBPROBE_H
25#define TBPROBE_H
26
27#include "tbconfig.h"
28
29#ifdef __cplusplus
30extern "C"
31{
32#endif
33
34#ifndef TB_NO_STDINT
35#include <stdint.h>
36#else
37typedef unsigned char uint8_t;
38typedef unsigned short uint16_t;
39typedef unsigned uint32_t;
40typedef long long unsigned uint64_t;
41typedef char int8_t;
42typedef short int16_t;
43typedef int int32_t;
44typedef long long int64_t;
45#endif
46
47#ifndef TB_NO_STDBOOL
48#include <stdbool.h>
49#else
50#ifndef __cplusplus
51typedef uint8_t bool;
52#define true 1
53#define false 0
54#endif
55#endif
56
57/*
58 * Internal definitions. Do not call these functions directly.
59 */
60extern bool tb_init_impl(const char *_path);
61extern unsigned tb_probe_wdl_impl(
62 uint64_t _white,
63 uint64_t _black,
64 uint64_t _kings,
65 uint64_t _queens,
66 uint64_t _rooks,
67 uint64_t _bishops,
68 uint64_t _knights,
69 uint64_t _pawns,
70 unsigned _ep,
71 bool _turn);
72extern unsigned tb_probe_root_impl(
73 uint64_t _white,
74 uint64_t _black,
75 uint64_t _kings,
76 uint64_t _queens,
77 uint64_t _rooks,
78 uint64_t _bishops,
79 uint64_t _knights,
80 uint64_t _pawns,
81 unsigned _rule50,
82 unsigned _ep,
83 bool _turn,
84 unsigned *_results);
85
86/****************************************************************************/
87/* MAIN API */
88/****************************************************************************/
89
90#define TB_MAX_MOVES (192+1)
91
92#define TB_CASTLING_K 0x1 /* White king-side. */
93#define TB_CASTLING_Q 0x2 /* White queen-side. */
94#define TB_CASTLING_k 0x4 /* Black king-side. */
95#define TB_CASTLING_q 0x8 /* Black queen-side. */
96
97#define TB_LOSS 0 /* LOSS */
98#define TB_BLESSED_LOSS 1 /* LOSS but 50-move draw */
99#define TB_DRAW 2 /* DRAW */
100#define TB_CURSED_WIN 3 /* WIN but 50-move draw */
101#define TB_WIN 4 /* WIN */
102
103#define TB_PROMOTES_NONE 0
104#define TB_PROMOTES_QUEEN 1
105#define TB_PROMOTES_ROOK 2
106#define TB_PROMOTES_BISHOP 3
107#define TB_PROMOTES_KNIGHT 4
108
109#define TB_RESULT_WDL_MASK 0x0000000F
110#define TB_RESULT_TO_MASK 0x000003F0
111#define TB_RESULT_FROM_MASK 0x0000FC00
112#define TB_RESULT_PROMOTES_MASK 0x00070000
113#define TB_RESULT_EP_MASK 0x00080000
114#define TB_RESULT_DTZ_MASK 0xFFF00000
115#define TB_RESULT_WDL_SHIFT 0
116#define TB_RESULT_TO_SHIFT 4
117#define TB_RESULT_FROM_SHIFT 10
118#define TB_RESULT_PROMOTES_SHIFT 16
119#define TB_RESULT_EP_SHIFT 19
120#define TB_RESULT_DTZ_SHIFT 20
121
122#define TB_GET_WDL(_res) \
123 (((_res) & TB_RESULT_WDL_MASK) >> TB_RESULT_WDL_SHIFT)
124#define TB_GET_TO(_res) \
125 (((_res) & TB_RESULT_TO_MASK) >> TB_RESULT_TO_SHIFT)
126#define TB_GET_FROM(_res) \
127 (((_res) & TB_RESULT_FROM_MASK) >> TB_RESULT_FROM_SHIFT)
128#define TB_GET_PROMOTES(_res) \
129 (((_res) & TB_RESULT_PROMOTES_MASK) >> TB_RESULT_PROMOTES_SHIFT)
130#define TB_GET_EP(_res) \
131 (((_res) & TB_RESULT_EP_MASK) >> TB_RESULT_EP_SHIFT)
132#define TB_GET_DTZ(_res) \
133 (((_res) & TB_RESULT_DTZ_MASK) >> TB_RESULT_DTZ_SHIFT)
134
135#define TB_SET_WDL(_res, _wdl) \
136 (((_res) & ~TB_RESULT_WDL_MASK) | \
137 (((_wdl) << TB_RESULT_WDL_SHIFT) & TB_RESULT_WDL_MASK))
138#define TB_SET_TO(_res, _to) \
139 (((_res) & ~TB_RESULT_TO_MASK) | \
140 (((_to) << TB_RESULT_TO_SHIFT) & TB_RESULT_TO_MASK))
141#define TB_SET_FROM(_res, _from) \
142 (((_res) & ~TB_RESULT_FROM_MASK) | \
143 (((_from) << TB_RESULT_FROM_SHIFT) & TB_RESULT_FROM_MASK))
144#define TB_SET_PROMOTES(_res, _promotes) \
145 (((_res) & ~TB_RESULT_PROMOTES_MASK) | \
146 (((_promotes) << TB_RESULT_PROMOTES_SHIFT) & TB_RESULT_PROMOTES_MASK))
147#define TB_SET_EP(_res, _ep) \
148 (((_res) & ~TB_RESULT_EP_MASK) | \
149 (((_ep) << TB_RESULT_EP_SHIFT) & TB_RESULT_EP_MASK))
150#define TB_SET_DTZ(_res, _dtz) \
151 (((_res) & ~TB_RESULT_DTZ_MASK) | \
152 (((_dtz) << TB_RESULT_DTZ_SHIFT) & TB_RESULT_DTZ_MASK))
153
154#define TB_RESULT_CHECKMATE TB_SET_WDL(0, TB_WIN)
155#define TB_RESULT_STALEMATE TB_SET_WDL(0, TB_DRAW)
156#define TB_RESULT_FAILED 0xFFFFFFFF
157
158/*
159 * The tablebase can be probed for any position where #pieces <= TB_LARGEST.
160 */
161extern unsigned TB_LARGEST;
162
163/*
164 * Initialize the tablebase.
165 *
166 * PARAMETERS:
167 * - path:
168 * The tablebase PATH string.
169 *
170 * RETURN:
171 * - true=succes, false=failed. The TB_LARGEST global will also be
172 * initialized. If no tablebase files are found, then `true' is returned
173 * and TB_LARGEST is set to zero.
174 */
175static inline bool tb_init(const char *_path)
176{
177 return tb_init_impl(_path);
178}
179
180/*
181 * Probe the Win-Draw-Loss (WDL) table.
182 *
183 * PARAMETERS:
184 * - white, black, kings, queens, rooks, bishops, knights, pawns:
185 * The current position (bitboards).
186 * - rule50:
187 * The 50-move half-move clock.
188 * - castling:
189 * Castling rights. Set to zero if no castling is possible.
190 * - ep:
191 * The en passant square (if exists). Set to zero if there is no en passant
192 * square.
193 * - turn:
194 * true=white, false=black
195 *
196 * RETURN:
197 * - One of {TB_LOSS, TB_BLESSED_LOSS, TB_DRAW, TB_CURSED_WIN, TB_WIN}.
198 * Otherwise returns TB_RESULT_FAILED if the probe failed.
199 *
200 * NOTES:
201 * - Engines should use this function during search.
202 * - This function is thread safe assuming TB_NO_THREADS is disabled.
203 */
204static inline unsigned tb_probe_wdl(
205 uint64_t _white,
206 uint64_t _black,
207 uint64_t _kings,
208 uint64_t _queens,
209 uint64_t _rooks,
210 uint64_t _bishops,
211 uint64_t _knights,
212 uint64_t _pawns,
213 unsigned _rule50,
214 unsigned _castling,
215 unsigned _ep,
216 bool _turn)
217{
218 if (_castling != 0)
219 return TB_RESULT_FAILED;
220 if (_rule50 != 0)
221 return TB_RESULT_FAILED;
222 return tb_probe_wdl_impl(_white, _black, _kings, _queens, _rooks,
223 _bishops, _knights, _pawns, _ep, _turn);
224}
225
226/*
227 * Probe the Distance-To-Zero (DTZ) table.
228 *
229 * PARAMETERS:
230 * - white, black, kings, queens, rooks, bishops, knights, pawns:
231 * The current position (bitboards).
232 * - rule50:
233 * The 50-move half-move clock.
234 * - castling:
235 * Castling rights. Set to zero if no castling is possible.
236 * - ep:
237 * The en passant square (if exists). Set to zero if there is no en passant
238 * square.
239 * - turn:
240 * true=white, false=black
241 * - results (OPTIONAL):
242 * Alternative results, one for each possible legal move. The passed array
243 * must be TB_MAX_MOVES in size.
244 * If alternative results are not desired then set results=NULL.
245 *
246 * RETURN:
247 * - A TB_RESULT value comprising:
248 * 1) The WDL value (TB_GET_WDL)
249 * 2) The suggested move (TB_GET_FROM, TB_GET_TO, TB_GET_PROMOTES, TB_GET_EP)
250 * 3) The DTZ value (TB_GET_DTZ)
251 * The suggested move is guaranteed to preserved the WDL value.
252 *
253 * Otherwise:
254 * 1) TB_RESULT_STALEMATE is returned if the position is in stalemate.
255 * 2) TB_RESULT_CHECKMATE is returned if the position is in checkmate.
256 * 3) TB_RESULT_FAILED is returned if the probe failed.
257 *
258 * If results!=NULL, then a TB_RESULT for each legal move will be generated
259 * and stored in the results array. The results array will be terminated
260 * by TB_RESULT_FAILED.
261 *
262 * NOTES:
263 * - Engines can use this function to probe at the root. This function should
264 * not be used during search.
265 * - DTZ tablebases can suggest unnatural moves, especially for losing
266 * positions. Engines may prefer to traditional search combined with WDL
267 * move filtering using the alternative results array.
268 * - This function is NOT thread safe. For engines this function should only
269 * be called once at the root per search.
270 */
271static inline unsigned tb_probe_root(
272 uint64_t _white,
273 uint64_t _black,
274 uint64_t _kings,
275 uint64_t _queens,
276 uint64_t _rooks,
277 uint64_t _bishops,
278 uint64_t _knights,
279 uint64_t _pawns,
280 unsigned _rule50,
281 unsigned _castling,
282 unsigned _ep,
283 bool _turn,
284 unsigned *_results)
285{
286 if (_castling != 0)
287 return TB_RESULT_FAILED;
288 return tb_probe_root_impl(_white, _black, _kings, _queens, _rooks,
289 _bishops, _knights, _pawns, _rule50, _ep, _turn, _results);
290}
291
292/****************************************************************************/
293/* HELPER API */
294/****************************************************************************/
295
296/*
297 * The HELPER API provides some useful additional functions. It is optional
298 * and can be disabled by defining TB_NO_HELPER_API. Engines should disable
299 * the HELPER API.
300 */
301
302#ifndef TB_NO_HELPER_API
303
304extern unsigned tb_pop_count(uint64_t _bb);
305extern unsigned tb_lsb(uint64_t _bb);
306extern uint64_t tb_pop_lsb(uint64_t _bb);
307extern uint64_t tb_king_attacks(unsigned _square);
308extern uint64_t tb_queen_attacks(unsigned _square, uint64_t _occ);
309extern uint64_t tb_rook_attacks(unsigned _square, uint64_t _occ);
310extern uint64_t tb_bishop_attacks(unsigned _square, uint64_t _occ);
311extern uint64_t tb_knight_attacks(unsigned _square);
312extern uint64_t tb_pawn_attacks(unsigned _square, bool _color);
313
314#endif
315
316#ifdef __cplusplus
317}
318#endif
319
320#endif