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