Implement exclude-moves feature
[bonanza.git] / shogi.h
1 #ifndef SHOGI_H
2 #define SHOGI_H
3
4 #include <stdio.h>
5 #include "bitop.h"
6 #include "param.h"
7
8 #if defined(_WIN32)
9
10 #  include <Winsock2.h>
11 #  define CONV              __fastcall
12 #  define SCKT_NULL         INVALID_SOCKET
13 typedef SOCKET sckt_t;
14
15 #else
16
17 #  include <pthread.h>
18 #  include <sys/times.h>
19 #  define CONV
20 #  define SCKT_NULL         -1
21 #  define SOCKET_ERROR      -1
22 typedef int sckt_t;
23
24 #endif
25
26 /* Microsoft C/C++ on x86 and x86-64 */
27 #if defined(_MSC_VER)
28
29 #  define _CRT_DISABLE_PERFCRIT_LOCKS
30 #  define UINT64_MAX    ULLONG_MAX
31 #  define PRIu64        "I64u"
32 #  define PRIx64        "I64x"
33 #  define UINT64_C(u)  ( u )
34
35 #  define restrict      __restrict
36 #  define strtok_r      strtok_s
37 #  define read          _read
38 #  define strncpy( dst, src, len ) strncpy_s( dst, len, src, _TRUNCATE )
39 #  define snprintf( buf, size, fmt, ... )   \
40           _snprintf_s( buf, size, _TRUNCATE, fmt, __VA_ARGS__ )
41 #  define vsnprintf( buf, size, fmt, list ) \
42           _vsnprintf_s( buf, size, _TRUNCATE, fmt, list )
43 typedef unsigned __int64 uint64_t;
44 typedef volatile long lock_t;
45
46 /* GNU C and Intel C/C++ on x86 and x86-64 */
47 #elif defined(__GNUC__) && ( defined(__i386__) || defined(__x86_64__) )
48
49 #  include <inttypes.h>
50 #  define restrict __restrict
51 typedef volatile int lock_t;
52
53 /* other targets. */
54 #else
55
56 #  include <inttypes.h>
57 typedef struct { unsigned int p[3]; } bitboard_t;
58 typedef pthread_mutex_t lock_t;
59 extern unsigned char aifirst_one[512];
60 extern unsigned char ailast_one[512];
61
62 #endif
63
64 #define BK_TINY
65 /*
66   #define BK_SMALL
67   #define BK_ULTRA_NARROW
68   #define BK_COM
69   #define NO_STDOUT
70   #define DBG_EASY
71 */
72
73 #if defined(CSASHOGI)
74 #  define NO_STDOUT
75 #  if ! defined(WIN32_PIPE)
76 #    define WIN32_PIPE
77 #  endif
78 #endif
79
80 #if defined(TLP)
81 #  define SHARE volatile
82 #  define SEARCH_ABORT ( root_abort || ptree->tlp_abort )
83 #else
84 #  define SHARE
85 #  define SEARCH_ABORT root_abort
86 #endif
87
88 #define NUM_UNMAKE              2
89 #define QUIES_PLY_LIMIT         7
90 #define SHELL_H_LEN             7
91 #define MAX_ANSWER              8
92 #define PLY_INC                 8
93 #define PLY_MAX                 128
94 #define RAND_N                  624
95 #define TC_NMOVE                35U
96 #define SEC_MARGIN              15U
97 #define SEC_KEEP_ALIVE          180U
98 #define TIME_RESPONSE           200U
99 #define RESIGN_THRESHOLD       ( ( MT_CAP_DRAGON * 5 ) /  8 )
100 #define BNZ_VER                 "6.0"
101
102 #define REP_MAX_PLY             32
103 #define REP_HIST_LEN            256
104
105 #define EHASH_MASK              0x3fffffU      /* occupies 32MB */
106 #define MATE3_MASK              0x07ffffU      /* occupies 4MB */
107
108 #define HIST_SIZE               0x4000U
109 #define HIST_INVALID            0xffffU
110 #define HIST_MAX                0x8000U
111
112 #define REJEC_MASK              0x0ffffU
113 #define REJEC_MIN_DEPTH        ( ( PLY_INC  * 5 ) )
114
115 #define EXT_RECAP1             ( ( PLY_INC  * 1 ) /  4 )
116 #define EXT_RECAP2             ( ( PLY_INC  * 2 ) /  4 )
117 #define EXT_ONEREP             ( ( PLY_INC  * 2 ) /  4 )
118 #define EXT_CHECK              ( ( PLY_INC  * 4 ) /  4 )
119
120 #define EFUTIL_MG1             ( ( MT_CAP_DRAGON * 2 ) /  8 )
121 #define EFUTIL_MG2             ( ( MT_CAP_DRAGON * 2 ) /  8 )
122
123 #define FMG_MG                 ( ( MT_CAP_DRAGON * 2 ) / 16 )
124 #define FMG_MG_KING            ( ( MT_CAP_DRAGON * 3 ) / 16 )
125 #define FMG_MG_MT              ( ( MT_CAP_DRAGON * 8 ) / 16 )
126 #define FMG_MISC               ( ( MT_CAP_DRAGON * 2 ) /  8 )
127 #define FMG_CAP                ( ( MT_CAP_DRAGON * 2 ) /  8 )
128 #define FMG_DROP               ( ( MT_CAP_DRAGON * 2 ) /  8 )
129 #define FMG_MT                 ( ( MT_CAP_DRAGON * 2 ) /  8 )
130 #define FMG_MISC_KING          ( ( MT_CAP_DRAGON * 2 ) /  8 )
131 #define FMG_CAP_KING           ( ( MT_CAP_DRAGON * 2 ) /  8 )
132
133 #define FV_WINDOW               256
134 #define FV_SCALE                32
135 #define FV_PENALTY             ( 0.2 / (double)FV_SCALE )
136
137 #define MPV_MAX_PV              16
138
139 #define TLP_MAX_THREADS         12
140 #define TLP_NUM_WORK           ( TLP_MAX_THREADS * 8 )
141
142 #define TIME_CHECK_MIN_NODE     10000U
143 #define TIME_CHECK_MAX_NODE     100000U
144
145 #define SIZE_FILENAME           256
146 #define SIZE_PLAYERNAME         256
147 #define SIZE_MESSAGE            512
148 #define SIZE_CSALINE            512
149
150 #if defined(USI)
151 #  define SIZE_CMDLINE          ( 1024 * 16 )
152 #  define SIZE_CMDBUFFER        ( 1024 * 16 )
153 #else
154 #  define SIZE_CMDLINE          512
155 #  define SIZE_CMDBUFFER        512
156 #endif
157
158 #define IsMove(move)           ( (move) & 0xffffffU )
159 #define MOVE_NA                 0x00000000U
160 #define MOVE_PASS               0x01000000U
161 #define MOVE_PONDER_FAILED      0xfe000000U
162 #define MOVE_RESIGN             0xff000000U
163 #define MOVE_CHK_SET            0x80000000U
164 #define MOVE_CHK_CLEAR          0x40000000U
165
166 #define MAX_LEGAL_MOVES         700
167 #define MAX_LEGAL_EVASION       256
168 #define MOVE_LIST_LEN           16384
169
170 #define MAX_SIZE_SECTION        0xffff
171 #define NUM_SECTION             0x4000
172
173 #define MATERIAL            (ptree->posi.material)
174 #define HAND_B              (ptree->posi.hand_black)
175 #define HAND_W              (ptree->posi.hand_white)
176
177 #define BB_BOCCUPY          (ptree->posi.b_occupied)
178 #define BB_BTGOLD           (ptree->posi.b_tgold)
179 #define BB_B_HDK            (ptree->posi.b_hdk)
180 #define BB_B_BH             (ptree->posi.b_bh)
181 #define BB_B_RD             (ptree->posi.b_rd)
182 #define BB_BPAWN_ATK        (ptree->posi.b_pawn_attacks)
183 #define BB_BPAWN            (ptree->posi.b_pawn)
184 #define BB_BLANCE           (ptree->posi.b_lance)
185 #define BB_BKNIGHT          (ptree->posi.b_knight)
186 #define BB_BSILVER          (ptree->posi.b_silver)
187 #define BB_BGOLD            (ptree->posi.b_gold)
188 #define BB_BBISHOP          (ptree->posi.b_bishop)
189 #define BB_BROOK            (ptree->posi.b_rook)
190 #define BB_BKING            (abb_mask[SQ_BKING])
191 #define BB_BPRO_PAWN        (ptree->posi.b_pro_pawn)
192 #define BB_BPRO_LANCE       (ptree->posi.b_pro_lance)
193 #define BB_BPRO_KNIGHT      (ptree->posi.b_pro_knight)
194 #define BB_BPRO_SILVER      (ptree->posi.b_pro_silver)
195 #define BB_BHORSE           (ptree->posi.b_horse)
196 #define BB_BDRAGON          (ptree->posi.b_dragon)
197
198 #define BB_WOCCUPY          (ptree->posi.w_occupied)
199 #define BB_WTGOLD           (ptree->posi.w_tgold)
200 #define BB_W_HDK            (ptree->posi.w_hdk)
201 #define BB_W_BH             (ptree->posi.w_bh)
202 #define BB_W_RD             (ptree->posi.w_rd)
203 #define BB_WPAWN_ATK        (ptree->posi.w_pawn_attacks)
204 #define BB_WPAWN            (ptree->posi.w_pawn)
205 #define BB_WLANCE           (ptree->posi.w_lance)
206 #define BB_WKNIGHT          (ptree->posi.w_knight)
207 #define BB_WSILVER          (ptree->posi.w_silver)
208 #define BB_WGOLD            (ptree->posi.w_gold)
209 #define BB_WBISHOP          (ptree->posi.w_bishop)
210 #define BB_WROOK            (ptree->posi.w_rook)
211 #define BB_WKING            (abb_mask[SQ_WKING])
212 #define BB_WPRO_PAWN        (ptree->posi.w_pro_pawn)
213 #define BB_WPRO_LANCE       (ptree->posi.w_pro_lance)
214 #define BB_WPRO_KNIGHT      (ptree->posi.w_pro_knight)
215 #define BB_WPRO_SILVER      (ptree->posi.w_pro_silver)
216 #define BB_WHORSE           (ptree->posi.w_horse)
217 #define BB_WDRAGON          (ptree->posi.w_dragon)
218
219 #define OCCUPIED_FILE       (ptree->posi.occupied_rl90)
220 #define OCCUPIED_DIAG1      (ptree->posi.occupied_rr45)
221 #define OCCUPIED_DIAG2      (ptree->posi.occupied_rl45)
222 #define BOARD               (ptree->posi.asquare)
223
224 #define SQ_BKING            (ptree->posi.isquare_b_king)
225 #define SQ_WKING            (ptree->posi.isquare_w_king)
226
227 #define HASH_KEY            (ptree->posi.hash_key)
228 #define HASH_VALUE          (ptree->sort_value[0])
229 #define MOVE_CURR           (ptree->current_move[ply])
230 #define MOVE_LAST           (ptree->current_move[ply-1])
231
232 #define NullDepth(d) ( (d) <  PLY_INC*26/4 ? (d)-PLY_INC*12/4 :              \
233                      ( (d) <= PLY_INC*30/4 ? PLY_INC*14/4                    \
234                                            : (d)-PLY_INC*16/4) )
235
236 #define RecursionThreshold  ( PLY_INC * 3 )
237
238 #define RecursionDepth(d) ( (d) < PLY_INC*18/4 ? PLY_INC*6/4                 \
239                                                : (d)-PLY_INC*12/4 )
240
241 #define LimitExtension(e,ply) if ( (e) && (ply) > 2 * iteration_depth ) {     \
242                                 if ( (ply) < 4 * iteration_depth ) {          \
243                                   e *= 4 * iteration_depth - (ply);           \
244                                   e /= 2 * iteration_depth;                   \
245                                 } else { e = 0; } }
246
247 #define Flip(turn)          ((turn)^1)
248 #define Inv(sq)             (nsquare-1-sq)
249 #define PcOnSq(k,i)         pc_on_sq[k][(i)*((i)+3)/2]
250 #define PcPcOnSq(k,i,j)     pc_on_sq[k][(i)*((i)+1)/2+(j)]
251
252 /*
253   xxxxxxxx xxxxxxxx xxx11111  pawn
254   xxxxxxxx xxxxxxxx 111xxxxx  lance
255   xxxxxxxx xxxxx111 xxxxxxxx  knight
256   xxxxxxxx xx111xxx xxxxxxxx  silver
257   xxxxxxx1 11xxxxxx xxxxxxxx  gold
258   xxxxx11x xxxxxxxx xxxxxxxx  bishop
259   xxx11xxx xxxxxxxx xxxxxxxx  rook
260  */
261 #define I2HandPawn(hand)       (((hand) >>  0) & 0x1f)
262 #define I2HandLance(hand)      (((hand) >>  5) & 0x07)
263 #define I2HandKnight(hand)     (((hand) >>  8) & 0x07)
264 #define I2HandSilver(hand)     (((hand) >> 11) & 0x07)
265 #define I2HandGold(hand)       (((hand) >> 14) & 0x07)
266 #define I2HandBishop(hand)     (((hand) >> 17) & 0x03)
267 #define I2HandRook(hand)        ((hand) >> 19)
268 #define IsHandPawn(hand)       ((hand) & 0x000001f)
269 #define IsHandLance(hand)      ((hand) & 0x00000e0)
270 #define IsHandKnight(hand)     ((hand) & 0x0000700)
271 #define IsHandSilver(hand)     ((hand) & 0x0003800)
272 #define IsHandGold(hand)       ((hand) & 0x001c000)
273 #define IsHandBishop(hand)     ((hand) & 0x0060000)
274 #define IsHandRook(hand)       ((hand) & 0x0180000)
275 #define IsHandSGBR(hand)       ((hand) & 0x01ff800)
276 /*
277   xxxxxxxx xxxxxxxx x1111111  destination
278   xxxxxxxx xx111111 1xxxxxxx  starting square or drop piece+nsquare-1
279   xxxxxxxx x1xxxxxx xxxxxxxx  flag for promotion
280   xxxxx111 1xxxxxxx xxxxxxxx  piece to move
281   x1111xxx xxxxxxxx xxxxxxxx  captured piece
282  */
283 #define To2Move(to)             ((unsigned int)(to)   <<  0)
284 #define From2Move(from)         ((unsigned int)(from) <<  7)
285 #define Drop2Move(piece)        ((nsquare-1+(piece))  <<  7)
286 #define Drop2From(piece)         (nsquare-1+(piece))
287 #define FLAG_PROMO               (1U                  << 14)
288 #define Piece2Move(piece)       ((piece)              << 15)
289 #define Cap2Move(piece)         ((piece)              << 19)
290 #define I2To(move)              (((move) >>  0) & 0x007fU)
291 #define I2From(move)            (((move) >>  7) & 0x007fU)
292 #define I2FromTo(move)          (((move) >>  0) & 0x3fffU)
293 #define I2IsPromote(move)       ((move) & FLAG_PROMO)
294 #define I2PieceMove(move)       (((move) >> 15) & 0x000fU)
295 #define UToFromToPromo(u)       ( (u) & 0x7ffffU )
296 #define UToCap(u)               (((u)    >> 19) & 0x000fU)
297 #define From2Drop(from)         ((from)-nsquare+1)
298
299
300 #define AttackFile(i)  (abb_file_attacks[i]                               \
301                          [((ptree->posi.occupied_rl90.p[aslide[i].irl90]) \
302                             >> aslide[i].srl90) & 0x7f])
303
304 #define AttackRank(i)  (abb_rank_attacks[i]                               \
305                          [((ptree->posi.b_occupied.p[aslide[i].ir0]       \
306                             |ptree->posi.w_occupied.p[aslide[i].ir0])     \
307                              >> aslide[i].sr0) & 0x7f ])
308
309 #define AttackDiag1(i)                                         \
310           (abb_bishop_attacks_rr45[i]                        \
311             [((ptree->posi.occupied_rr45.p[aslide[i].irr45]) \
312                >> aslide[i].srr45) & 0x7f])
313
314 #define AttackDiag2(i)                                         \
315           (abb_bishop_attacks_rl45[i]                        \
316             [((ptree->posi.occupied_rl45.p[aslide[i].irl45]) \
317                >> aslide[i].srl45) & 0x7f])
318
319 #define BishopAttack0(i) ( AttackDiag1(i).p[0] | AttackDiag2(i).p[0] )
320 #define BishopAttack1(i) ( AttackDiag1(i).p[1] | AttackDiag2(i).p[1] )
321 #define BishopAttack2(i) ( AttackDiag1(i).p[2] | AttackDiag2(i).p[2] )
322 #define AttackBLance(bb,i) BBAnd( bb, abb_minus_rays[i], AttackFile(i) )
323 #define AttackWLance(bb,i) BBAnd( bb, abb_plus_rays[i],  AttackFile(i) )
324 #define AttackBishop(bb,i) BBOr( bb, AttackDiag1(i), AttackDiag2(i) )
325 #define AttackRook(bb,i)   BBOr( bb, AttackFile(i), AttackRank(i) )
326 #define AttackHorse(bb,i)  AttackBishop(bb,i); BBOr(bb,bb,abb_king_attacks[i])
327 #define AttackDragon(bb,i) AttackRook(bb,i);   BBOr(bb,bb,abb_king_attacks[i])
328
329 #define InCheck(turn)                                        \
330          ( (turn) ? is_white_attacked( ptree, SQ_WKING )     \
331                   : is_black_attacked( ptree, SQ_BKING ) )
332
333 #define MakeMove(turn,move,ply)                                \
334                 ( (turn) ? make_move_w( ptree, move, ply ) \
335                          : make_move_b( ptree, move, ply ) )
336
337 #define UnMakeMove(turn,move,ply)                                \
338                 ( (turn) ? unmake_move_w( ptree, move, ply ) \
339                          : unmake_move_b( ptree, move, ply ) )
340
341 #define IsMoveCheck( ptree, turn, move )                        \
342                 ( (turn) ? is_move_check_w( ptree, move )   \
343                          : is_move_check_b( ptree, move ) )
344
345 #define GenCaptures(turn,pmove) ( (turn) ? w_gen_captures( ptree, pmove )   \
346                                          : b_gen_captures( ptree, pmove ) )
347
348 #define GenNoCaptures(turn,pmove)                                             \
349                                ( (turn) ? w_gen_nocaptures( ptree, pmove )  \
350                                         : b_gen_nocaptures( ptree, pmove ) )
351
352 #define GenDrop(turn,pmove)     ( (turn) ? w_gen_drop( ptree, pmove )       \
353                                          : b_gen_drop( ptree, pmove ) )
354
355 #define GenCapNoProEx2(turn,pmove)                                 \
356                 ( (turn) ? w_gen_cap_nopro_ex2( ptree, pmove )   \
357                          : b_gen_cap_nopro_ex2( ptree, pmove ) )
358
359 #define GenNoCapNoProEx2(turn,pmove)                                \
360                 ( (turn) ? w_gen_nocap_nopro_ex2( ptree, pmove )  \
361                          : b_gen_nocap_nopro_ex2( ptree, pmove ) )
362
363 #define GenEvasion(turn,pmove)                                  \
364                 ( (turn) ? w_gen_evasion( ptree, pmove )      \
365                          : b_gen_evasion( ptree, pmove ) )
366
367 #define GenCheck(turn,pmove)                                  \
368                 ( (turn) ? w_gen_checks( ptree, pmove )      \
369                          : b_gen_checks( ptree, pmove ) )
370
371 #define IsMateIn1Ply(turn)                                    \
372                 ( (turn) ? is_w_mate_in_1ply( ptree )         \
373                          : is_b_mate_in_1ply( ptree ) )
374
375 #define IsDiscoverBK(from,to)                                  \
376           idirec = (int)adirec[SQ_BKING][from],               \
377           ( idirec && ( idirec!=(int)adirec[SQ_BKING][to] )   \
378             && is_pinned_on_black_king( ptree, from, idirec ) )
379
380 #define IsDiscoverWK(from,to)                                  \
381           idirec = (int)adirec[SQ_WKING][from],               \
382           ( idirec && ( idirec!=(int)adirec[SQ_WKING][to] )   \
383             && is_pinned_on_white_king( ptree, from, idirec ) )
384 #define IsMateWPawnDrop(ptree,to) ( BOARD[(to)+9] == king                 \
385                                      && is_mate_w_pawn_drop( ptree, to ) )
386
387 #define IsMateBPawnDrop(ptree,to) ( BOARD[(to)-9] == -king                \
388                                      && is_mate_b_pawn_drop( ptree, to ) )
389
390 enum { b0000, b0001, b0010, b0011, b0100, b0101, b0110, b0111,
391        b1000, b1001, b1010, b1011, b1100, b1101, b1110, b1111 };
392
393 enum { A9 = 0, B9, C9, D9, E9, F9, G9, H9, I9,
394            A8, B8, C8, D8, E8, F8, G8, H8, I8,
395            A7, B7, C7, D7, E7, F7, G7, H7, I7,
396            A6, B6, C6, D6, E6, F6, G6, H6, I6,
397            A5, B5, C5, D5, E5, F5, G5, H5, I5,
398            A4, B4, C4, D4, E4, F4, G4, H4, I4,
399            A3, B3, C3, D3, E3, F3, G3, H3, I3,
400            A2, B2, C2, D2, E2, F2, G2, H2, I2,
401            A1, B1, C1, D1, E1, F1, G1, H1, I1 };
402
403 enum { promote = 8, empty = 0,
404        pawn, lance, knight, silver, gold, bishop, rook, king, pro_pawn,
405        pro_lance, pro_knight, pro_silver, piece_null, horse, dragon };
406
407 enum { npawn_max = 18,  nlance_max  = 4,  nknight_max = 4,  nsilver_max = 4,
408        ngold_max = 4,   nbishop_max = 2,  nrook_max   = 2,  nking_max   = 2 };
409
410 enum { rank1 = 0, rank2, rank3, rank4, rank5, rank6, rank7, rank8, rank9 };
411 enum { file1 = 0, file2, file3, file4, file5, file6, file7, file8, file9 };
412
413 enum { nhand = 7, nfile = 9,  nrank = 9,  nsquare = 81 };
414
415 enum { mask_file1 = (( 1U << 18 | 1U << 9 | 1U ) << 8) };
416
417 enum { flag_diag1 = b0001, flag_plus = b0010 };
418
419 enum { score_draw     =     1,
420        score_max_eval = 30000,
421        score_matelong = 30002,
422        score_mate1ply = 32598,
423        score_inferior = 32599,
424        score_bound    = 32600,
425        score_foul     = 32600 };
426
427 enum { phase_hash      = b0001,
428        phase_killer1   = b0001 << 1,
429        phase_killer2   = b0010 << 1,
430        phase_killer    = b0011 << 1,
431        phase_cap1      = b0001 << 3,
432        phase_cap_misc  = b0010 << 3,
433        phase_cap       = b0011 << 3,
434        phase_history1  = b0001 << 5,
435        phase_history2  = b0010 << 5,
436        phase_history   = b0011 << 5,
437        phase_misc      = b0100 << 5 };
438
439 enum { next_move_hash = 0,  next_move_capture,   next_move_history2,
440        next_move_misc };
441
442 /* next_evasion_hash should be the same as next_move_hash */
443 enum { next_evasion_hash = 0, next_evasion_genall, next_evasion_misc };
444
445
446 enum { next_quies_gencap, next_quies_captures, next_quies_misc };
447
448 enum { no_rep = 0, four_fold_rep, perpetual_check, perpetual_check2,
449        black_superi_rep, white_superi_rep, hash_hit, prev_solution, book_hit,
450        pv_fail_high, mate_search };
451
452 enum { record_misc, record_eof, record_next, record_resign, record_drawn,
453        record_error };
454
455 enum { black = 0, white = 1 };
456
457 enum { direc_misc           = b0000,
458        direc_file           = b0010, /* | */
459        direc_rank           = b0011, /* - */
460        direc_diag1          = b0100, /* / */
461        direc_diag2          = b0101, /* \ */
462        flag_cross           = b0010,
463        flag_diag            = b0100 };
464
465 enum { value_null           = b0000,
466        value_upper          = b0001,
467        value_lower          = b0010,
468        value_exact          = b0011,
469        flag_value_up_exact  = b0001,
470        flag_value_low_exact = b0010,
471        node_do_null         = b0100,
472        node_do_recap        = b1000,
473        node_do_mate         = b0001 << 4,
474        node_mate_threat     = b0010 << 4, /* <- don't change it */ 
475        node_do_futile       = b0100 << 4,
476        node_do_recursion    = b1000 << 4,
477        node_do_hashcut      = b0001 << 8,
478        state_node_end };
479 /* note: maximum bits are 8.  tlp_state_node uses type unsigned char. */
480
481 enum { flag_from_ponder     = b0001 };
482
483 enum { flag_time            = b0001,
484        flag_history         = b0010,
485        flag_rep             = b0100,
486        flag_detect_hang     = b1000,
487        flag_nomake_move     = b0010 << 4,
488        flag_nofmargin       = b0100 << 4 };
489
490 /* flags represent status of root move */
491 enum { flag_searched        = b0001,
492        flag_first           = b0010 };
493
494
495 enum { flag_mated           = b0001,
496        flag_resigned        = b0010,
497        flag_drawn           = b0100,
498        flag_suspend         = b1000,
499        mask_game_end        = b1111,
500        flag_quit            = b0001 << 4,
501        flag_puzzling        = b0010 << 4,
502        flag_pondering       = b0100 << 4,
503        flag_thinking        = b1000 << 4,
504        flag_problem         = b0001 << 8,
505        flag_move_now        = b0010 << 8,
506        flag_quit_ponder     = b0100 << 8,
507        flag_nostdout        = b1000 << 8,
508        flag_search_error    = b0001 << 12,
509        flag_nonewlog        = b0010 << 12,
510        flag_reverse         = b0100 << 12,
511        flag_narrow_book     = b1000 << 12,
512        flag_time_extendable = b0001 << 16,
513        flag_learning        = b0010 << 16,
514        flag_nobeep          = b0100 << 16,
515        flag_nostress        = b1000 << 16,
516        flag_nopeek          = b0001 << 20,
517        flag_noponder        = b0010 << 20,
518        flag_noprompt        = b0100 << 20,
519        flag_sendpv          = b1000 << 20,
520        flag_skip_root_move  = b0001 << 24 };
521
522
523 enum { flag_hand_pawn       = 1 <<  0,
524        flag_hand_lance      = 1 <<  5,
525        flag_hand_knight     = 1 <<  8,
526        flag_hand_silver     = 1 << 11,
527        flag_hand_gold       = 1 << 14,
528        flag_hand_bishop     = 1 << 17,
529        flag_hand_rook       = 1 << 19 };
530
531 enum { f_hand_pawn   =    0,
532        e_hand_pawn   =   19,
533        f_hand_lance  =   38,
534        e_hand_lance  =   43,
535        f_hand_knight =   48,
536        e_hand_knight =   53,
537        f_hand_silver =   58,
538        e_hand_silver =   63,
539        f_hand_gold   =   68,
540        e_hand_gold   =   73,
541        f_hand_bishop =   78,
542        e_hand_bishop =   81,
543        f_hand_rook   =   84,
544        e_hand_rook   =   87,
545        fe_hand_end   =   90,
546        f_pawn        =   81,
547        e_pawn        =  162,
548        f_lance       =  225,
549        e_lance       =  306,
550        f_knight      =  360,
551        e_knight      =  441,
552        f_silver      =  504,
553        e_silver      =  585,
554        f_gold        =  666,
555        e_gold        =  747,
556        f_bishop      =  828,
557        e_bishop      =  909,
558        f_horse       =  990,
559        e_horse       = 1071,
560        f_rook        = 1152,
561        e_rook        = 1233,
562        f_dragon      = 1314,
563        e_dragon      = 1395,
564        fe_end        = 1476,
565
566        kkp_hand_pawn   =   0,
567        kkp_hand_lance  =  19,
568        kkp_hand_knight =  24,
569        kkp_hand_silver =  29,
570        kkp_hand_gold   =  34,
571        kkp_hand_bishop =  39,
572        kkp_hand_rook   =  42,
573        kkp_hand_end    =  45,
574        kkp_pawn        =  36,
575        kkp_lance       = 108,
576        kkp_knight      = 171,
577        kkp_silver      = 252,
578        kkp_gold        = 333,
579        kkp_bishop      = 414,
580        kkp_horse       = 495,
581        kkp_rook        = 576,
582        kkp_dragon      = 657,
583        kkp_end         = 738 };
584
585 enum { pos_n = fe_end * ( fe_end + 1 ) / 2 };
586
587 typedef struct { bitboard_t gold, silver, knight, lance; } check_table_t;
588
589 #if ! defined(MINIMUM)
590 typedef struct { fpos_t fpos;  unsigned int games, moves, lines; } rpos_t;
591 typedef struct {
592   double pawn, lance, knight, silver, gold, bishop, rook;
593   double pro_pawn, pro_lance, pro_knight, pro_silver, horse, dragon;
594   float pc_on_sq[nsquare][fe_end*(fe_end+1)/2];
595   float kkp[nsquare][nsquare][kkp_end];
596 } param_t;
597 #endif
598
599 typedef enum { mode_write, mode_read_write, mode_read } record_mode_t;
600
601 typedef struct { uint64_t word1, word2; }                        trans_entry_t;
602 typedef struct { trans_entry_t prefer, always[2]; }              trans_table_t;
603 typedef struct { int count;  unsigned int cnst[2], vec[RAND_N]; }rand_work_t;
604
605 typedef struct {
606   int no1_value, no2_value;
607   unsigned int no1, no2;
608 } move_killer_t;
609
610 typedef struct { unsigned int no1, no2; } killer_t;
611
612 typedef struct {
613   union { char str_move[ MAX_ANSWER ][ 8 ]; } info;
614   char str_name1[ SIZE_PLAYERNAME ];
615   char str_name2[ SIZE_PLAYERNAME ];
616   FILE *pf;
617   unsigned int games, moves, lines;
618 } record_t;
619
620 typedef struct {
621   unsigned int a[PLY_MAX];
622   unsigned char type;
623   unsigned char length;
624   unsigned char depth;
625 } pv_t;
626
627 typedef struct {
628   unsigned char ir0,   sr0;
629   unsigned char irl90, srl90;
630   unsigned char irl45, srl45;
631   unsigned char irr45, srr45;
632 } slide_tbl_t;
633
634
635 typedef struct {
636   uint64_t hash_key;
637   bitboard_t b_occupied,     w_occupied;
638   bitboard_t occupied_rl90,  occupied_rl45, occupied_rr45;
639   bitboard_t b_hdk,          w_hdk;
640   bitboard_t b_tgold,        w_tgold;
641   bitboard_t b_bh,           w_bh;
642   bitboard_t b_rd,           w_rd;
643   bitboard_t b_pawn_attacks, w_pawn_attacks;
644   bitboard_t b_lance,        w_lance;
645   bitboard_t b_knight,       w_knight;
646   bitboard_t b_silver,       w_silver;
647   bitboard_t b_bishop,       w_bishop;
648   bitboard_t b_rook,         w_rook;
649   bitboard_t b_horse,        w_horse;
650   bitboard_t b_dragon,       w_dragon;
651   bitboard_t b_pawn,         w_pawn;
652   bitboard_t b_gold,         w_gold;
653   bitboard_t b_pro_pawn,     w_pro_pawn;
654   bitboard_t b_pro_lance,    w_pro_lance;
655   bitboard_t b_pro_knight,   w_pro_knight;
656   bitboard_t b_pro_silver,   w_pro_silver;
657   unsigned int hand_black, hand_white;
658   int material;
659   signed char asquare[nsquare];
660   unsigned char isquare_b_king, isquare_w_king;
661 } posi_t;
662
663
664 typedef struct {
665   unsigned int hand_black, hand_white;
666   char turn_to_move;
667   signed char asquare[nsquare];
668 } min_posi_t;
669
670 typedef struct {
671   uint64_t nodes;
672   unsigned int move, status;
673 #if defined(DFPN_CLIENT)
674   volatile int dfpn_cresult;
675 #endif
676 } root_move_t;
677
678 typedef struct {
679   unsigned int *move_last;
680   unsigned int move_cap1;
681   unsigned int move_cap2;
682   int phase_done, next_phase, remaining, value_cap1, value_cap2;
683 } next_move_t;
684
685 /* data: 31  1bit flag_learned */
686 /*       30  1bit is_flip      */
687 /*       15 16bit value        */
688 typedef struct {
689   uint64_t key_book;
690   unsigned int key_responsible, key_probed, key_played;
691   unsigned int hand_responsible, hand_probed, hand_played;
692   unsigned int move_played, move_responsible, move_probed, data;
693 } history_book_learn_t;
694
695 typedef struct tree tree_t;
696 struct tree {
697   posi_t posi;
698   uint64_t rep_board_list[ REP_HIST_LEN ];
699   uint64_t node_searched;
700   unsigned int *move_last[ PLY_MAX ];
701   next_move_t anext_move[ PLY_MAX ];
702   pv_t pv[ PLY_MAX ];
703   move_killer_t amove_killer[ PLY_MAX ];
704   unsigned int null_pruning_done;
705   unsigned int null_pruning_tried;
706   unsigned int check_extension_done;
707   unsigned int recap_extension_done;
708   unsigned int onerp_extension_done;
709   unsigned int neval_called;
710   unsigned int nquies_called;
711   unsigned int nfour_fold_rep;
712   unsigned int nperpetual_check;
713   unsigned int nsuperior_rep;
714   unsigned int nrep_tried;
715   unsigned int ntrans_always_hit;
716   unsigned int ntrans_prefer_hit;
717   unsigned int ntrans_probe;
718   unsigned int ntrans_exact;
719   unsigned int ntrans_lower;
720   unsigned int ntrans_upper;
721   unsigned int ntrans_superior_hit;
722   unsigned int ntrans_inferior_hit;
723   unsigned int fail_high;
724   unsigned int fail_high_first;
725   unsigned int rep_hand_list[ REP_HIST_LEN ];
726   unsigned int amove_hash[ PLY_MAX ];
727   unsigned int amove[ MOVE_LIST_LEN ];
728   unsigned int current_move[ PLY_MAX ];
729   killer_t killers[ PLY_MAX ];
730   unsigned int hist_nmove[ PLY_MAX ];
731   unsigned int hist_move[ PLY_MAX ][ MAX_LEGAL_MOVES ];
732   int sort_value[ MAX_LEGAL_MOVES ];
733   unsigned short hist_tried[ HIST_SIZE ];
734   unsigned short hist_good[ HIST_SIZE ];
735   short save_material[ PLY_MAX ];
736   int save_eval[ PLY_MAX+1 ];
737   unsigned char nsuc_check[ PLY_MAX+1 ];
738   int nrep;
739 #if defined(TLP)
740   struct tree *tlp_ptrees_sibling[ TLP_MAX_THREADS ];
741   struct tree *tlp_ptree_parent;
742   lock_t tlp_lock;
743   volatile int tlp_abort;
744   volatile int tlp_used;
745   unsigned short tlp_slot;
746   short tlp_beta;
747   short tlp_best;
748   volatile unsigned char tlp_nsibling;
749   unsigned char tlp_depth;
750   unsigned char tlp_state_node;
751   unsigned char tlp_id;
752   char tlp_turn;
753   char tlp_ply;
754 #endif
755 };
756
757
758 extern SHARE unsigned int game_status;
759
760 extern int npawn_box;
761 extern int nlance_box;
762 extern int nknight_box;
763 extern int nsilver_box;
764 extern int ngold_box;
765 extern int nbishop_box;
766 extern int nrook_box;
767
768 extern unsigned int ponder_move_list[ MAX_LEGAL_MOVES ];
769 extern unsigned int ponder_move;
770 extern int ponder_nmove;
771
772 extern root_move_t root_move_list[ MAX_LEGAL_MOVES ];
773 extern SHARE int root_abort;
774 extern int root_nmove;
775 extern int root_index;
776 extern int root_value;
777 extern int root_alpha;
778 extern int root_beta;
779 extern int root_turn;
780 extern int root_nfail_high;
781 extern int root_nfail_low;
782 extern int resign_threshold;
783
784 extern uint64_t node_limit;
785 extern unsigned int node_per_second;
786 extern unsigned int node_next_signal;
787 extern unsigned int node_last_check;
788
789 extern unsigned int hash_mask;
790 extern int trans_table_age;
791
792 extern pv_t last_pv;
793 extern pv_t alast_pv_save[NUM_UNMAKE];
794 extern int alast_root_value_save[NUM_UNMAKE];
795 extern int last_root_value;
796 extern int amaterial_save[NUM_UNMAKE];
797 extern unsigned int amove_save[NUM_UNMAKE];
798 extern unsigned char ansuc_check_save[NUM_UNMAKE];
799
800 extern SHARE trans_table_t *ptrans_table;
801 extern trans_table_t *ptrans_table_orig;
802 extern int log2_ntrans_table;
803
804 extern int depth_limit;
805
806 extern unsigned int time_last_result;
807 extern unsigned int time_last_eff_search;
808 extern unsigned int time_last_search;
809 extern unsigned int time_last_check;
810 extern unsigned int time_turn_start;
811 extern unsigned int time_start;
812 extern unsigned int time_max_limit;
813 extern unsigned int time_limit;
814 extern unsigned int time_response;
815 extern unsigned int sec_limit;
816 extern unsigned int sec_limit_up;
817 extern unsigned int sec_limit_depth;
818 extern unsigned int sec_elapsed;
819 extern unsigned int sec_b_total;
820 extern unsigned int sec_w_total;
821
822 extern record_t record_problems;
823 extern record_t record_game;
824 extern FILE *pf_book;
825 extern int record_num;
826
827 extern int p_value_ex[31];
828 extern int p_value_pm[15];
829 extern int p_value[31];
830 extern short pc_on_sq[nsquare][fe_end*(fe_end+1)/2];
831 extern short kkp[nsquare][nsquare][kkp_end];
832
833 extern uint64_t ehash_tbl[ EHASH_MASK + 1 ];
834 extern rand_work_t rand_work;
835 extern slide_tbl_t aslide[ nsquare ];
836 extern bitboard_t abb_b_knight_attacks[ nsquare ];
837 extern bitboard_t abb_b_silver_attacks[ nsquare ];
838 extern bitboard_t abb_b_gold_attacks[ nsquare ];
839 extern bitboard_t abb_w_knight_attacks[ nsquare ];
840 extern bitboard_t abb_w_silver_attacks[ nsquare ];
841 extern bitboard_t abb_w_gold_attacks[ nsquare ];
842 extern bitboard_t abb_king_attacks[ nsquare ];
843 extern bitboard_t abb_obstacle[ nsquare ][ nsquare ];
844 extern bitboard_t abb_bishop_attacks_rl45[ nsquare ][ 128 ];
845 extern bitboard_t abb_bishop_attacks_rr45[ nsquare ][ 128 ];
846 extern bitboard_t abb_rank_attacks[ nsquare ][ 128 ];
847 extern bitboard_t abb_file_attacks[ nsquare ][ 128 ];
848 extern bitboard_t abb_mask[ nsquare ];
849 extern bitboard_t abb_mask_rl90[ nsquare ];
850 extern bitboard_t abb_mask_rl45[ nsquare ];
851 extern bitboard_t abb_mask_rr45[ nsquare ];
852 extern bitboard_t abb_plus_rays[ nsquare ];
853 extern bitboard_t abb_minus_rays[ nsquare ];
854 extern uint64_t b_pawn_rand[ nsquare ];
855 extern uint64_t b_lance_rand[ nsquare ];
856 extern uint64_t b_knight_rand[ nsquare ];
857 extern uint64_t b_silver_rand[ nsquare ];
858 extern uint64_t b_gold_rand[ nsquare ];
859 extern uint64_t b_bishop_rand[ nsquare ];
860 extern uint64_t b_rook_rand[ nsquare ];
861 extern uint64_t b_king_rand[ nsquare ];
862 extern uint64_t b_pro_pawn_rand[ nsquare ];
863 extern uint64_t b_pro_lance_rand[ nsquare ];
864 extern uint64_t b_pro_knight_rand[ nsquare ];
865 extern uint64_t b_pro_silver_rand[ nsquare ];
866 extern uint64_t b_horse_rand[ nsquare ];
867 extern uint64_t b_dragon_rand[ nsquare ];
868 extern uint64_t b_hand_pawn_rand[ npawn_max ];
869 extern uint64_t b_hand_lance_rand[ nlance_max ];
870 extern uint64_t b_hand_knight_rand[ nknight_max ];
871 extern uint64_t b_hand_silver_rand[ nsilver_max ];
872 extern uint64_t b_hand_gold_rand[ ngold_max ];
873 extern uint64_t b_hand_bishop_rand[ nbishop_max ];
874 extern uint64_t b_hand_rook_rand[ nrook_max ];
875 extern uint64_t w_pawn_rand[ nsquare ];
876 extern uint64_t w_lance_rand[ nsquare ];
877 extern uint64_t w_knight_rand[ nsquare ];
878 extern uint64_t w_silver_rand[ nsquare ];
879 extern uint64_t w_gold_rand[ nsquare ];
880 extern uint64_t w_bishop_rand[ nsquare ];
881 extern uint64_t w_rook_rand[ nsquare ];
882 extern uint64_t w_king_rand[ nsquare ];
883 extern uint64_t w_pro_pawn_rand[ nsquare ];
884 extern uint64_t w_pro_lance_rand[ nsquare ];
885 extern uint64_t w_pro_knight_rand[ nsquare ];
886 extern uint64_t w_pro_silver_rand[ nsquare ];
887 extern uint64_t w_horse_rand[ nsquare ];
888 extern uint64_t w_dragon_rand[ nsquare ];
889 extern uint64_t w_hand_pawn_rand[ npawn_max ];
890 extern uint64_t w_hand_lance_rand[ nlance_max ];
891 extern uint64_t w_hand_knight_rand[ nknight_max ];
892 extern uint64_t w_hand_silver_rand[ nsilver_max ];
893 extern uint64_t w_hand_gold_rand[ ngold_max ];
894 extern uint64_t w_hand_bishop_rand[ nbishop_max ];
895 extern uint64_t w_hand_rook_rand[ nrook_max ];
896 extern unsigned int move_evasion_pchk;
897 extern int easy_abs;
898 extern int easy_min;
899 extern int easy_max;
900 extern int easy_value;
901 extern SHARE int fmg_misc;
902 extern SHARE int fmg_cap;
903 extern SHARE int fmg_drop;
904 extern SHARE int fmg_mt;
905 extern SHARE int fmg_misc_king;
906 extern SHARE int fmg_cap_king;
907 extern int iteration_depth;
908 extern unsigned char book_section[ MAX_SIZE_SECTION+1 ];
909 extern unsigned char adirec[nsquare][nsquare];
910 extern unsigned char is_same[16][16];
911 extern char str_message[ SIZE_MESSAGE ];
912 extern char str_cmdline[ SIZE_CMDLINE ];
913 extern char str_buffer_cmdline[ SIZE_CMDBUFFER ];
914 extern const char *str_error;
915
916 extern const char *astr_table_piece[ 16 ];
917 extern const char *str_resign;
918 extern const char *str_repetition;
919 extern const char *str_jishogi;
920 extern const char *str_record_error;
921 extern const char *str_unexpect_eof;
922 extern const char *str_ovrflw_line;
923 extern const char *str_warning;
924 extern const char *str_on;
925 extern const char *str_off;
926 extern const char *str_book;
927 extern const char *str_hash;
928 extern const char *str_fv;
929 extern const char *str_book_error;
930 extern const char *str_perpet_check;
931 extern const char *str_bad_cmdline;
932 extern const char *str_busy_think;
933 extern const char *str_bad_record;
934 extern const char *str_bad_board;
935 extern const char *str_delimiters;
936 extern const char *str_fmt_line;
937 extern const char *str_illegal_move;
938 extern const char *str_double_pawn;
939 extern const char *str_mate_drppawn;
940 extern const char *str_fopen_error;
941 extern const char *str_game_ended;
942 extern const char *str_io_error;
943 extern const char *str_spaces;
944 extern const char *str_no_legal_move;
945 extern const char *str_king_hang;
946 #if defined(CSA_LAN)
947 extern const char *str_server_err;
948 #endif
949 extern const char *str_myname;
950 extern const char *str_version;
951 extern const min_posi_t min_posi_no_handicap;
952 extern const int ashell_h[ SHELL_H_LEN ];
953 extern const int aikkp[16];
954 extern const int aikpp[31];
955 extern const int aikkp_hand[8];
956 extern const char ach_turn[2];
957 extern const unsigned char aifile[ nsquare ];
958 extern const unsigned char airank[ nsquare ];
959 extern int move_list[], move_ptr; // [HGM] undo: game history (used in proce.c and makemove.c)
960 extern char analyze_mode;         // [HGM] exclude: used in proce.c and root.c
961
962 #if defined(DFPN_CLIENT)
963 #  define DFPN_CLIENT_SIZE_SIGNATURE 64
964 enum { dfpn_client_na, dfpn_client_win, dfpn_client_lose, dfpn_client_misc };
965 typedef struct { char str_move[7], result; } dfpn_client_cresult_t;
966 extern volatile int dfpn_client_flag_read;
967 extern volatile sckt_t dfpn_client_sckt;
968 extern volatile unsigned int dfpn_client_move_unlocked;
969 extern volatile int dfpn_client_rresult_unlocked;
970 extern volatile int dfpn_client_num_cresult;
971 extern volatile char dfpn_client_signature[ DFPN_CLIENT_SIZE_SIGNATURE ];
972 extern volatile dfpn_client_cresult_t dfpn_client_cresult[ MAX_LEGAL_MOVES ];
973 extern volatile char dfpn_client_str_move[7];
974 extern volatile int dfpn_client_rresult;
975 extern unsigned int dfpn_client_best_move;
976 extern lock_t dfpn_client_lock;
977 extern char dfpn_client_str_addr[256];
978 extern int dfpn_client_port;
979 extern int dfpn_client_cresult_index;
980 void CONV dfpn_client_start( const tree_t * restrict ptree );
981 void CONV dfpn_client_check_results( void );
982 int CONV dfpn_client_out( const char *fmt, ... );
983 #endif
984
985 void CONV pv_close( tree_t * restrict ptree, int ply, int type );
986 void CONV pv_copy( tree_t * restrict ptree, int ply );
987 void set_derivative_param( void );
988 void CONV set_search_limit_time( int turn );
989 void CONV ehash_clear( void );
990 void CONV hash_store_pv( const tree_t * restrict ptree, unsigned int move,
991                          int turn );
992 void CONV check_futile_score_quies( const tree_t * restrict ptree,
993                                     unsigned int move, int old_val,
994                                     int new_val, int turn );
995 void out_warning( const char *format, ... );
996 void out_error( const char *format, ... );
997 void show_prompt( void );
998 void CONV make_move_w( tree_t * restrict ptree, unsigned int move, int ply );
999 void CONV make_move_b( tree_t * restrict ptree, unsigned int move, int ply );
1000 void CONV unmake_move_b( tree_t * restrict ptree, unsigned int move, int ply );
1001 void CONV unmake_move_w( tree_t * restrict ptree, unsigned int move, int ply );
1002 void ini_rand( unsigned int s );
1003 void out_CSA( tree_t * restrict ptree, record_t *pr, unsigned int move );
1004 void CONV out_pv( tree_t * restrict ptree, int value, int turn,
1005                   unsigned int time );
1006 void CONV hash_store( const tree_t * restrict ptree, int ply, int depth,
1007                       int turn, int value_type, int value, unsigned int move,
1008                       unsigned int state_node );
1009 void * CONV memory_alloc( size_t nbytes );
1010 void CONV adjust_time( unsigned int elapsed_new, int turn );
1011 int CONV load_fv( void );
1012 int CONV unmake_move_root( tree_t * restrict ptree );
1013 int CONV popu_count012( unsigned int u0, unsigned int u1, unsigned int u2 );
1014 int CONV first_one012( unsigned int u0, unsigned int u1, unsigned int u2 );
1015 int CONV last_one210( unsigned int u2, unsigned int u1, unsigned int u0 );
1016 int CONV first_one01( unsigned int u0, unsigned int u1 );
1017 int CONV first_one12( unsigned int u1, unsigned int u2 );
1018 int CONV last_one01( unsigned int u0, unsigned int u1 );
1019 int CONV last_one12( unsigned int u1, unsigned int u2 );
1020 int CONV first_one1( unsigned int u1 );
1021 int CONV first_one2( unsigned int u2 );
1022 int CONV last_one0( unsigned int u0 );
1023 int CONV last_one1( unsigned int u1 );
1024 int CONV memory_free( void *p );
1025 int CONV reset_time( unsigned int b_remain, unsigned int w_remain );
1026 int CONV gen_legal_moves( tree_t * restrict ptree, unsigned int *p0,
1027                           int flag );
1028 int CONV detect_signals( tree_t * restrict ptree );
1029 int ini( tree_t * restrict ptree );
1030 int fin( void );
1031 int ponder( tree_t * restrict ptree );
1032 int CONV book_on( void );
1033 int CONV book_off( void );
1034 int CONV solve_problems( tree_t * restrict ptree, unsigned int nposition );
1035 int CONV solve_mate_problems( tree_t * restrict ptree,
1036                               unsigned int nposition );
1037 int read_board_rep1( const char *str_line, min_posi_t *pmin_posi );
1038 int CONV com_turn_start( tree_t * restrict ptree, int flag );
1039 int read_record( tree_t * restrict ptree, const char *str_file,
1040                  unsigned int moves, int flag );
1041 int out_board( const tree_t * restrict ptree, FILE *pf, unsigned int move,
1042                int flag );
1043 int make_root_move_list( tree_t * restrict ptree );
1044 int record_wind( record_t *pr );
1045 int CONV book_probe( tree_t * restrict ptree );
1046 int CONV detect_repetition( tree_t * restrict ptree, int ply, int turn,
1047                             int nth );
1048 int CONV is_move( const char *str );
1049 int CONV is_mate( tree_t * restrict ptree, int ply );
1050 int CONV is_mate_w_pawn_drop( tree_t * restrict ptree, int sq_drop );
1051 int CONV is_mate_b_pawn_drop( tree_t * restrict ptree, int sq_drop );
1052 int CONV clear_trans_table( void );
1053 int CONV eval_max_score( const tree_t * restrict ptree, unsigned int move,
1054                          int value, int turn, int diff );
1055 int CONV estimate_score_diff( const tree_t * restrict ptree, unsigned int move,
1056                               int turn );
1057 int CONV eval_material( const tree_t * restrict ptree );
1058 int CONV ini_trans_table( void );
1059 int CONV is_hand_eq_supe( unsigned int u, unsigned int uref );
1060 int CONV is_move_valid( tree_t * restrict ptree, unsigned int move, int turn );
1061 int CONV iterate( tree_t * restrict ptree );
1062 int CONV gen_next_move( tree_t * restrict ptree, int ply, int turn );
1063 int CONV gen_next_evasion( tree_t * restrict ptree, int ply, int turn );
1064 int CONV ini_game( tree_t * restrict ptree, const min_posi_t *pmin_posi,
1065                    int flag, const char *str_name1, const char *str_name2 );
1066 int open_history( const char *str_name1, const char *str_name2 );
1067 int next_cmdline( int is_wait );
1068 int CONV procedure( tree_t * restrict ptree );
1069 int CONV get_cputime( unsigned int *ptime );
1070 int CONV get_elapsed( unsigned int *ptime );
1071 int interpret_CSA_move( tree_t * restrict ptree, unsigned int *pmove,
1072                         const char *str );
1073 int in_CSA( tree_t * restrict ptree, record_t *pr, unsigned int *pmove,
1074             int do_history );
1075 int in_CSA_record( FILE * restrict pf, tree_t * restrict ptree );
1076 int CONV update_time( int turn );
1077 int CONV exam_tree( const tree_t * restrict ptree );
1078 int rep_check_root( tree_t * restrict ptree );
1079 int CONV make_move_root( tree_t * restrict ptree, unsigned int move,
1080                          int flag );
1081 int CONV search_quies( tree_t * restrict ptree, int alpha, int beta, int turn,
1082                        int ply, int qui_ply );
1083 int CONV search( tree_t * restrict ptree, int alpha, int beta, int turn,
1084                  int depth, int ply, unsigned int state_node );
1085 int CONV searchr( tree_t * restrict ptree, int alpha, int beta, int turn,
1086              int depth );
1087 int CONV evaluate( tree_t * restrict ptree, int ply, int turn );
1088 int CONV swap( const tree_t * restrict ptree, unsigned int move, int alpha,
1089                int beta, int turn );
1090 int file_close( FILE *pf );
1091 int record_open( record_t *pr, const char *str_file,
1092                  record_mode_t record_mode, const char *str_name1,
1093                  const char *str_name2 );
1094 int record_close( record_t *pr );
1095 unsigned int CONV phash( unsigned int move, int turn );
1096 unsigned int CONV is_mate_in3ply( tree_t * restrict ptree, int turn, int ply );
1097 unsigned int CONV is_b_mate_in_1ply( tree_t * restrict ptree );
1098 unsigned int CONV is_w_mate_in_1ply( tree_t * restrict ptree );
1099 unsigned int CONV hash_probe( tree_t * restrict ptree, int ply, int depth,
1100                               int turn, int alpha, int beta,
1101                               unsigned int *pstate_node );
1102 unsigned int rand32( void );
1103 unsigned int CONV is_black_attacked( const tree_t * restrict ptree, int sq );
1104 unsigned int CONV is_white_attacked( const tree_t * restrict ptree, int sq );
1105 unsigned int CONV is_pinned_on_black_king( const tree_t * restrict ptree,
1106                                      int isquare, int idirec );
1107 unsigned int CONV is_pinned_on_white_king( const tree_t * restrict ptree,
1108                                      int isquare, int idirec );
1109 unsigned int * CONV b_gen_captures( const tree_t * restrict ptree,
1110                                     unsigned int * restrict pmove );
1111 unsigned int * CONV b_gen_nocaptures( const tree_t * restrict ptree,
1112                                       unsigned int * restrict pmove );
1113 unsigned int * CONV b_gen_drop( tree_t * restrict ptree,
1114                           unsigned int * restrict pmove );
1115 unsigned int * CONV b_gen_evasion( tree_t *restrict ptree,
1116                                    unsigned int * restrict pmove );
1117 unsigned int * CONV b_gen_checks( tree_t * restrict __ptree__,
1118                                   unsigned int * restrict pmove );
1119 unsigned int * CONV b_gen_cap_nopro_ex2( const tree_t * restrict ptree,
1120                                          unsigned int * restrict pmove );
1121 unsigned int * CONV b_gen_nocap_nopro_ex2( const tree_t * restrict ptree,
1122                                      unsigned int * restrict pmove );
1123 unsigned int * CONV w_gen_captures( const tree_t * restrict ptree,
1124                                     unsigned int * restrict pmove );
1125 unsigned int * CONV w_gen_nocaptures( const tree_t * restrict ptree,
1126                                       unsigned int * restrict pmove );
1127 unsigned int * CONV w_gen_drop( tree_t * restrict ptree,
1128                                 unsigned int * restrict pmove );
1129 unsigned int * CONV w_gen_evasion( tree_t * restrict ptree,
1130                                    unsigned int * restrict pmove );
1131 unsigned int * CONV w_gen_checks( tree_t * restrict __ptree__,
1132                                   unsigned int * restrict pmove );
1133 unsigned int * CONV w_gen_cap_nopro_ex2( const tree_t * restrict ptree,
1134                                          unsigned int * restrict pmove );
1135 unsigned int * CONV w_gen_nocap_nopro_ex2( const tree_t * restrict ptree,
1136                                            unsigned int * restrict pmove );
1137 int CONV b_have_checks( tree_t * restrict ptree );
1138 int CONV w_have_checks( tree_t * restrict ptree );
1139 int CONV b_have_evasion( tree_t * restrict ptree );
1140 int CONV w_have_evasion( tree_t * restrict ptree );
1141 int CONV is_move_check_b( const tree_t * restrict ptree, unsigned int move );
1142 int CONV is_move_check_w( const tree_t * restrict ptree, unsigned int move );
1143 uint64_t CONV hash_func( const tree_t * restrict ptree );
1144 uint64_t rand64( void );
1145 FILE *file_open( const char *str_file, const char *str_mode );
1146 bitboard_t CONV attacks_to_piece( const tree_t * restrict ptree, int sq );
1147 bitboard_t CONV b_attacks_to_piece( const tree_t * restrict ptree, int sq );
1148 bitboard_t CONV w_attacks_to_piece( const tree_t * restrict ptree, int sq );
1149 const char * CONV str_time( unsigned int time );
1150 const char * CONV str_time_symple( unsigned int time );
1151 const char *str_CSA_move( unsigned int move );
1152
1153 #if defined(MPV)
1154 int root_mpv;
1155 int mpv_num;
1156 int mpv_width;
1157 pv_t mpv_pv[ MPV_MAX_PV*2 + 1 ];
1158 #endif
1159
1160 #  if ! defined(_WIN32) && ( defined(DFPN_CLIENT) || defined(TLP) )
1161 extern pthread_attr_t pthread_attr;
1162 #  endif
1163
1164 #if defined(DFPN_CLIENT) || defined(TLP)
1165 void CONV lock( lock_t *plock );
1166 void CONV unlock( lock_t *plock );
1167 int CONV lock_init( lock_t *plock );
1168 int CONV lock_free( lock_t *plock );
1169 void tlp_yield( void );
1170 extern lock_t io_lock;
1171 #endif
1172
1173 #if defined(TLP)
1174 #  define SignKey(word2, word1) word2 ^= ( word1 )
1175 #  define TlpEnd()              tlp_end();
1176 #  if defined(MNJ_LAN) || defined(USI)
1177 uint64_t tlp_count_node( tree_t * restrict ptree );
1178 #  endif
1179 void tlp_set_abort( tree_t * restrict ptree );
1180 void tlp_end( void );
1181 int CONV tlp_search( tree_t * restrict ptree, int alpha, int beta, int turn,
1182                 int depth, int ply, unsigned int state_node );
1183 int tlp_split( tree_t * restrict ptree );
1184 int tlp_start( void );
1185 int tlp_is_descendant( const tree_t * restrict ptree, int slot_ancestor );
1186 extern lock_t tlp_lock;
1187 extern volatile int tlp_abort;
1188 extern volatile int tlp_idle;
1189 extern volatile int tlp_num;
1190 extern int tlp_max;
1191 extern int tlp_nsplit;
1192 extern int tlp_nabort;
1193 extern int tlp_nslot;
1194 extern tree_t tlp_atree_work[ TLP_NUM_WORK ];
1195 extern tree_t * volatile tlp_ptrees[ TLP_MAX_THREADS ];
1196 #else /* no TLP */
1197 #  define SignKey(word2, word1)
1198 #  define TlpEnd()
1199 extern tree_t tree;
1200 #endif
1201
1202 #if ! defined(_WIN32)
1203 extern clock_t clk_tck;
1204 #endif
1205
1206 #if ! defined(NDEBUG)
1207 int exam_bb( const tree_t *ptree );
1208 #endif
1209
1210 #if ! ( defined(NO_STDOUT) && defined(NO_LOGGING) )
1211 #  define Out( ... ) out( __VA_ARGS__ )
1212 void out( const char *format, ... );
1213 #else
1214 #  define Out( ... )
1215 #endif
1216
1217 #if ! defined(NO_LOGGING)
1218 extern FILE *pf_log;
1219 extern const char *str_dir_logs;
1220 #endif
1221
1222 #if defined(NO_STDOUT) || defined(WIN32_PIPE)
1223 #  define OutBeep()
1224 #  define StdoutStress(x,y) 1
1225 #  define StdoutNormal()    1
1226 #else
1227 #  define OutBeep()         out_beep()
1228 #  define StdoutStress(x,y) stdout_stress(x,y)
1229 #  define StdoutNormal()    stdout_normal()
1230 void out_beep( void );
1231 int stdout_stress( int is_promote, int ifrom );
1232 int stdout_normal( void );
1233 #endif
1234
1235 #if defined(CSA_LAN) || defined(MNJ_LAN) || defined(DFPN)
1236 void CONV shutdown_all( void );
1237 #  define ShutdownAll() shutdown_all();
1238 #else
1239 #  define ShutdownAll()
1240 #endif
1241
1242 #if defined(CSA_LAN)||defined(MNJ_LAN)||defined(DFPN_CLIENT)||defined(DFPN)
1243 int client_next_game( tree_t * restrict ptree, const char *str_addr,
1244                       int iport );
1245 sckt_t CONV sckt_connect( const char *str_addr, int iport );
1246 int CONV sckt_recv_all( sckt_t sd );
1247 int CONV sckt_shutdown( sckt_t sd );
1248 int CONV sckt_check( sckt_t sd );
1249 int CONV sckt_in( sckt_t sd, char *str, int n );
1250 int CONV sckt_out( sckt_t sd, const char *fmt, ... );
1251 extern unsigned int time_last_send;
1252 #endif
1253
1254 #if defined(DFPN)
1255 #  define DFPNOut( ... ) if ( dfpn_sckt != SCKT_NULL ) \
1256                            sckt_out( dfpn_sckt, __VA_ARGS__ )
1257 int CONV dfpn( tree_t * restrict ptree, int turn, int ply );
1258 int CONV dfpn_ini_hash( void );
1259 extern unsigned int dfpn_hash_log2;
1260 extern sckt_t dfpn_sckt;
1261 #else
1262 #  define DFPNOut( ... )
1263 #endif
1264
1265 #if defined(CSA_LAN)
1266 extern int client_turn;
1267 extern int client_ngame;
1268 extern int client_max_game;
1269 extern long client_port;
1270 extern char client_str_addr[256];
1271 extern char client_str_id[256];
1272 extern char client_str_pwd[256];
1273 extern sckt_t sckt_csa;
1274 #endif
1275
1276 #if defined(MNJ_LAN) || defined(USI)
1277 extern unsigned int moves_ignore[MAX_LEGAL_MOVES];
1278 #endif
1279
1280 #if defined(MNJ_LAN)
1281 #  define MnjOut( ... ) if ( sckt_mnj != SCKT_NULL ) \
1282                           sckt_out( sckt_mnj, __VA_ARGS__ )
1283 extern sckt_t sckt_mnj;
1284 extern int mnj_posi_id;
1285 extern int mnj_depth_stable;
1286 void CONV mnj_check_results( void );
1287 int CONV mnj_reset_tbl( int sd, unsigned int seed );
1288 int analyze( tree_t * restrict ptree );
1289 #else
1290 #  define MnjOut( ... )
1291 #endif
1292
1293 #if defined(USI)
1294 #  define USIOut( ... ) if ( usi_mode != usi_off ) usi_out( __VA_ARGS__ )
1295 enum usi_mode { usi_off, usi_on };
1296 extern enum usi_mode usi_mode;
1297 extern unsigned int usi_time_out_last;
1298 extern unsigned int usi_byoyomi;
1299 void CONV usi_out( const char *format, ... );
1300 int CONV usi_book( tree_t * restrict ptree );
1301 int CONV usi_root_list( tree_t * restrict ptree );
1302 int CONV usi2csa( const tree_t * restrict ptree, const char *str_usi,
1303                   char *str_csa );
1304 int CONV csa2usi( const tree_t * restrict ptree, const char *str_csa,
1305                   char *str_usi );
1306 int analyze( tree_t * restrict ptree );
1307 #else
1308 #  define USIOut( ... )
1309 #endif
1310
1311 #if defined(CSA_LAN) || defined(MNJ_LAN) || defined(DFPN_CLIENT)||defined(DFPN)
1312 const char *str_WSAError( const char *str );
1313 #endif
1314
1315 #if defined(CSASHOGI)
1316 #  define OutCsaShogi( ... ) out_csashogi( __VA_ARGS__ )
1317 void out_csashogi( const char *format, ... );
1318 #else
1319 #  define OutCsaShogi( ... )
1320 #endif
1321
1322
1323
1324 extern check_table_t b_chk_tbl[nsquare];
1325 extern check_table_t w_chk_tbl[nsquare];
1326
1327 #if defined(DBG_EASY)
1328 extern unsigned int easy_move;
1329 #endif
1330
1331 #if defined(INANIWA_SHIFT)
1332 extern int inaniwa_flag;
1333 #endif
1334
1335 #if defined(MINIMUM)
1336
1337 #  define MT_CAP_PAWN       ( DPawn      + DPawn )
1338 #  define MT_CAP_LANCE      ( DLance     + DLance )
1339 #  define MT_CAP_KNIGHT     ( DKnight    + DKnight )
1340 #  define MT_CAP_SILVER     ( DSilver    + DSilver )
1341 #  define MT_CAP_GOLD       ( DGold      + DGold )
1342 #  define MT_CAP_BISHOP     ( DBishop    + DBishop )
1343 #  define MT_CAP_ROOK       ( DRook      + DRook )
1344 #  define MT_CAP_PRO_PAWN   ( DProPawn   + DPawn )
1345 #  define MT_CAP_PRO_LANCE  ( DProLance  + DLance )
1346 #  define MT_CAP_PRO_KNIGHT ( DProKnight + DKnight )
1347 #  define MT_CAP_PRO_SILVER ( DProSilver + DSilver )
1348 #  define MT_CAP_HORSE      ( DHorse     + DBishop )
1349 #  define MT_CAP_DRAGON     ( DDragon    + DRook )
1350 #  define MT_CAP_KING       ( DKing      + DKing )
1351 #  define MT_PRO_PAWN       ( DProPawn   - DPawn )
1352 #  define MT_PRO_LANCE      ( DProLance  - DLance )
1353 #  define MT_PRO_KNIGHT     ( DProKnight - DKnight )
1354 #  define MT_PRO_SILVER     ( DProSilver - DSilver )
1355 #  define MT_PRO_BISHOP     ( DHorse     - DBishop )
1356 #  define MT_PRO_ROOK       ( DDragon    - DRook )
1357
1358 #else
1359
1360 #  define MT_CAP_PAWN       ( p_value_ex[ 15 + pawn ] )
1361 #  define MT_CAP_LANCE      ( p_value_ex[ 15 + lance ] )
1362 #  define MT_CAP_KNIGHT     ( p_value_ex[ 15 + knight ] )
1363 #  define MT_CAP_SILVER     ( p_value_ex[ 15 + silver ] )
1364 #  define MT_CAP_GOLD       ( p_value_ex[ 15 + gold ] )
1365 #  define MT_CAP_BISHOP     ( p_value_ex[ 15 + bishop ] )
1366 #  define MT_CAP_ROOK       ( p_value_ex[ 15 + rook ] )
1367 #  define MT_CAP_PRO_PAWN   ( p_value_ex[ 15 + pro_pawn ] )
1368 #  define MT_CAP_PRO_LANCE  ( p_value_ex[ 15 + pro_lance ] )
1369 #  define MT_CAP_PRO_KNIGHT ( p_value_ex[ 15 + pro_knight ] )
1370 #  define MT_CAP_PRO_SILVER ( p_value_ex[ 15 + pro_silver ] )
1371 #  define MT_CAP_HORSE      ( p_value_ex[ 15 + horse ] )
1372 #  define MT_CAP_DRAGON     ( p_value_ex[ 15 + dragon ] )
1373 #  define MT_CAP_KING       ( DKing + DKing )
1374 #  define MT_PRO_PAWN       ( p_value_pm[ 7 + pawn ] )
1375 #  define MT_PRO_LANCE      ( p_value_pm[ 7 + lance ] )
1376 #  define MT_PRO_KNIGHT     ( p_value_pm[ 7 + knight ] )
1377 #  define MT_PRO_SILVER     ( p_value_pm[ 7 + silver ] )
1378 #  define MT_PRO_BISHOP     ( p_value_pm[ 7 + bishop ] )
1379 #  define MT_PRO_ROOK       ( p_value_pm[ 7 + rook ] )
1380
1381 void fill_param_zero( void );
1382 void ini_param( param_t *p );
1383 void add_param( param_t *p1, const param_t *p2 );
1384 void inc_param( const tree_t * restrict ptree, param_t * restrict pd,
1385                 double dinc );
1386 void param_sym( param_t *p );
1387 void renovate_param( const param_t *pd );
1388 int learn( tree_t * restrict ptree, int is_ini, int nsteps,
1389            unsigned int max_games, int max_iterations,
1390            int nworker1, int nworker2 );
1391 int record_setpos( record_t *pr, const rpos_t *prpos );
1392 int record_getpos( record_t *pr, rpos_t *prpos );
1393 int record_rewind( record_t *pr );
1394 int CONV book_create( tree_t * restrict ptree );
1395 int out_param( void );
1396 double calc_penalty( void );
1397
1398 #endif /* no MINIMUM */
1399
1400 #if ( REP_HIST_LEN - PLY_MAX ) < 1
1401 #  error "REP_HIST_LEN - PLY_MAX is too small."
1402 #endif
1403
1404 #if defined(CSA_LAN) && '\n' != 0x0a
1405 #  error "'\n' is not the ASCII code of LF (0x0a)."
1406 #endif
1407
1408 #endif /* SHOGI_H */