1
/* GNU Chess 5.0 - common.h - common symbolic definitions
2
Copyright (c) 1999-2002 Free Software Foundation, Inc.
4
GNU Chess is based on the two research programs
5
Cobalt by Chua Kong-Sian and Gazebo by Stuart Cracraft.
7
GNU Chess is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2, or (at your option)
12
GNU Chess is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with GNU Chess; see the file COPYING. If not, write to
19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20
Boston, MA 02111-1307, USA.
24
cracraft@ai.mit.edu, cracraft@stanfordalumni.org, cracraft@earthlink.net
33
# define __attribute__(x)
37
* Include "uint64_t" and similar types using the ac_need_stdint_h ac macro
42
#include <sys/types.h>
45
* Define time structures to get timeval for Timer
51
* Define macro for declaring 64bit constants for compilers not using ULL
55
#define ULL(x) ((uint64_t)(x))
57
#define ULL(x) x ## ULL
61
* BitBoard is a key data type. It is a 64-bit value, in which each
62
* bit represents a square on the board as defined by "enum Square".
63
* Thus, bit position 0 represents a fact about square a1, and bit
64
* position 63 represents a fact about square h8. For example, the
65
* bitboard representing "white rook" positions will have a bit set
66
* for every position occupied on the board by a white rook.
69
typedef uint64_t BitBoard;
70
typedef uint64_t HashType;
71
typedef uint32_t KeyType;
74
* Board represents an entire board's state, and is structured to
80
BitBoard b[2][7]; /* piece/pawn positions by side (0=white, 1=black)
81
and then by piece (1=pawn..6=king). For example,
82
b[white][knight] has bits set for every board
83
position occupied by a White Knight. */
84
BitBoard friends[2]; /* Friendly (this side's) pieces */
86
BitBoard blockerr90; /* rotated 90 degrees */
87
BitBoard blockerr45; /* rotated 45 degrees */
88
BitBoard blockerr315; /* rotated 315 degrees */
89
short ep; /* Location of en passant square */
90
short flag; /* Flags related to castle privileges */
91
short side; /* Color of side on move: 0=white, 1=black */
92
short material[2]; /* Total material by side not inc. king */
93
short pmaterial[2]; /* Total pawn material by side not inc. king */
94
short castled[2]; /* True (1) if side is castled */
95
short king[2]; /* Location of king 0 - a1 .. 63 - h8 */
98
/* leaf describes a leaf-level analysis result */
102
int move; /* the move that produced this particular board */
103
int score; /* the scored value of this leaf */
107
* GameRec records an individual move made in the game; an entire
108
* Game is a set of GameRec's:
111
#define SANSZ 8 /* longest move is "exf8=Q+" so 7+1 */
115
int move; /* The actual move made; this is NOT the move count! */
116
short epsq; /* en passant square */
117
short bflag; /* Flags for castle privs, see Board.flag */
118
short Game50; /* The last value of GameCnt (a count of half-moves)
119
that castled, captured, or moved a pawn */
121
float et; /* elapsed time */
124
char SANmv[SANSZ]; /* The move in SAN notation */
130
HashType key; /* Full 64 bit hash */
131
int move; /* Best move */
132
int score; /* Estimated score, may be lower or upper bound */
133
uint8_t flag; /* Is this alpha, beta, quiescent or exact? */
134
uint8_t depth; /* Search depth */
147
/* MACRO definitions */
150
#define MAX(a,b) ((a) > (b) ? (a) : (b))
153
#define MIN(a,b) ((a) < (b) ? (a) : (b))
158
dbg_printf("Set 0x%x\n", (b)); \
163
dbg_printf("Clear 0x%x\n", (b)); \
166
/* Draw score can be used to penalise draws if desired */
168
#define MATERIAL (board.material[side] - board.material[1^side])
169
#define PHASE (8 - (board.material[white]+board.material[black]) / 1150)
170
#define KEY(a) (a >> 32)
174
#define BishopAttack(sq) \
175
(Bishop45Atak[sq][(board.blockerr45 >> Shift45[sq]) & Mask45[sq]] | \
176
Bishop315Atak[sq][(board.blockerr315 >> Shift315[sq]) & Mask315[sq]])
177
#define RookAttack(sq) \
178
(Rook00Atak[sq][(board.blocker >> Shift00[sq]) & 0xFF] | \
179
Rook90Atak[sq][(board.blockerr90 >> Shift90[sq]) & 0xFF])
180
#define QueenAttack(sq) \
181
(BishopAttack(sq) | RookAttack(sq))
184
/* Some bit macros */
187
* gcc 2.95.4 completely screws up the macros with lookup tables
188
* with -O2 on PPC, maybe this check has to be refined. (I don't know
189
* whether other architectures also suffer from this gcc bug.) However,
190
* with gcc 3.0, the lookup tables are _much_ faster than this direct
193
#if defined(__GNUC__) && defined(__PPC__) && __GNUC__ < 3
194
# define SETBIT(b,i) ((b) |= ((ULL(1)<<63)>>(i)))
195
# define CLEARBIT(b,i) ((b) &= ~((ULL(1)<<63)>>(i)))
197
# define SETBIT(b,i) ((b) |= BitPosArray[i])
198
# define CLEARBIT(b,i) ((b) &= NotBitPosArray[i])
201
#define RANK(i) ((i) >> 3)
202
#define ROW(i) ((i) & 7)
203
#define trailz(b) (leadz ((b) & ((~b) + 1)))
205
/* Move Descriptions (moves) are represented internally as integers.
206
* The lowest 6 bits define the destination ("TO") square, and
207
* the next lowest 6 bits define the source ("FROM") square,
208
* using the values defined by "enum Square" (0=a1, 63=h8).
209
* Upper bits are used to identify other move information such as
210
* a promotion (and to what), a capture (and of what),
211
* CASTLING moves, and ENPASSANT moves; see the "constants for
212
* move description" below for more information on these upper bits.
214
#define PROMOTEPIECE(a) ((a >> 12) & 0x0007)
215
#define CAPTUREPIECE(a) ((a >> 15) & 0x0007)
216
#define TOSQ(a) ((a) & 0x003F)
217
#define FROMSQ(a) ((a >> 6) & 0x003F)
218
#define MOVE(a,b) (((a) << 6) | (b))
220
/* constants for move description */
221
#define KNIGHTPRM 0x00002000
222
#define BISHOPPRM 0x00003000
223
#define ROOKPRM 0x00004000
224
#define QUEENPRM 0x00005000
225
#define PROMOTION 0x00007000
226
#define PAWNCAP 0x00008000
227
#define KNIGHTCAP 0x00010000
228
#define BISHOPCAP 0x00018000
229
#define ROOKCAP 0x00020000
230
#define QUEENCAP 0x00028000
231
#define CAPTURE 0x00038000
232
#define NULLMOVE 0x00100000
233
#define CASTLING 0x00200000
234
#define ENPASSANT 0x00400000
235
#define MOVEMASK (CASTLING | ENPASSANT | PROMOTION | 0x0FFF)
243
#define INFINITY 32767
245
#define MATESCORE(a) ((a) > MATE-255 || (a) < -MATE+255)
247
/* constants for Board */
248
#define WKINGCASTLE 0x0001
249
#define WQUEENCASTLE 0x0002
250
#define BKINGCASTLE 0x0004
251
#define BQUEENCASTLE 0x0008
252
#define WCASTLE (WKINGCASTLE | WQUEENCASTLE)
253
#define BCASTLE (BKINGCASTLE | BQUEENCASTLE)
255
/* Material values */
264
/* Some special BitBoards */
265
#define NULLBITBOARD ( ULL(0x0000000000000000))
266
#define WHITESQUARES ( ULL(0x55AA55AA55AA55AA))
267
#define BLACKSQUARES ( ULL(0xAA55AA55AA55AA55))
268
#define CENTRESQUARES ( ULL(0x0000001818000000))
269
#define COMPUTERHALF ( ULL(0xFFFFFFFF00000000))
270
#define OPPONENTHALF ( ULL(0x00000000FFFFFFFF))
276
#define MANUAL 0x0008
277
#define TIMEOUT 0x0010
278
#define SPARE1 0x0020
280
#define USEHASH 0x0080
282
#define USENULL 0x0200
283
#define XBOARD 0x0400
284
#define TIMECTL 0x0800
286
#define PONDER 0x2000 /* We are in pondering (during search) */
287
#define HARD 0x4000 /* Pondering is turned on */
288
#define ANALYZE 0x8000 /* In ANALYZE mode */
295
/* Transposition table flags */
310
/* The various phases during move selection */
319
#define PICKCOUNTER 9
321
#define MAXTREEDEPTH 2000
322
#define MAXPLYDEPTH 65
323
#define MAXGAMEDEPTH 600
326
Smaller HASHSLOT defaults 20011017 to improve blitz play
327
and make it easier to run on old machines
329
#define HASHSLOTS 1024
330
#define PAWNSLOTS 512
332
extern short distance[64][64];
333
extern short taxicab[64][64];
334
extern unsigned char lzArray[65536];
335
extern short Shift00[64];
336
extern short Shift90[64];
337
extern short Shift45[64];
338
extern short Shift315[64];
339
extern BitBoard DistMap[64][8];
340
extern BitBoard BitPosArray[64];
341
extern BitBoard NotBitPosArray[64];
342
extern BitBoard MoveArray[8][64];
343
extern BitBoard Ray[64][8];
344
extern BitBoard FromToRay[64][64];
345
extern BitBoard RankBit[8];
346
extern BitBoard FileBit[8];
347
extern BitBoard Ataks[2][7];
348
extern BitBoard PassedPawnMask[2][64];
349
extern BitBoard IsolaniMask[8];
350
extern BitBoard SquarePawnMask[2][64];
351
extern BitBoard Rook00Atak[64][256];
352
extern BitBoard Rook90Atak[64][256];
353
extern BitBoard Bishop45Atak[64][256];
354
extern BitBoard Bishop315Atak[64][256];
355
extern BitBoard pinned;
356
extern BitBoard rings[4];
357
extern BitBoard boxes[2];
358
extern BitBoard stonewall[2];
359
extern BitBoard pieces[2];
360
extern BitBoard mask_kr_trapped_w[3];
361
extern BitBoard mask_kr_trapped_b[3];
362
extern BitBoard mask_qr_trapped_w[3];
363
extern BitBoard mask_qr_trapped_b[3];
364
extern BitBoard boardhalf[2];
365
extern BitBoard boardside[2];
366
extern short directions[64][64];
367
extern unsigned char BitCount[65536];
368
extern leaf Tree[MAXTREEDEPTH];
369
extern leaf *TreePtr[MAXPLYDEPTH];
371
extern GameRec Game[MAXGAMEDEPTH];
372
extern int RealGameCnt;
373
extern short RealSide;
376
extern unsigned int flags;
377
extern unsigned int preanalyze_flags;
379
extern int cboard[64];
380
extern int Mvboard[64];
381
extern HashType hashcode[2][7][64];
382
extern HashType ephash[64];
383
extern HashType WKCastlehash;
384
extern HashType WQCastlehash;
385
extern HashType BKCastlehash;
386
extern HashType BQCastlehash;
387
extern HashType Sidehash;
388
extern HashType HashKey;
389
extern HashType PawnHashKey;
390
extern HashSlot *HashTab[2];
391
extern PawnSlot *PawnTab[2];
395
extern int lazyscore[2];
396
extern int maxposnscore[2];
397
extern int rootscore;
398
extern int lastrootscore;
399
extern unsigned long GenCnt;
400
extern unsigned long NodeCnt;
401
extern unsigned long QuiesCnt;
402
extern unsigned long EvalCnt;
403
extern unsigned long EvalCall;
404
extern unsigned long ChkExtCnt;
405
extern unsigned long OneRepCnt;
406
extern unsigned long RcpExtCnt;
407
extern unsigned long PawnExtCnt;
408
extern unsigned long HorzExtCnt;
409
extern unsigned long ThrtExtCnt;
410
extern unsigned long KingExtCnt;
411
extern unsigned long NullCutCnt;
412
extern unsigned long FutlCutCnt;
413
extern unsigned long RazrCutCnt;
414
extern unsigned long TotalGetHashCnt;
415
extern unsigned long GoodGetHashCnt;
416
extern unsigned long TotalPutHashCnt;
417
extern unsigned long CollHashCnt;
418
extern unsigned long TotalPawnHashCnt;
419
extern unsigned long GoodPawnHashCnt;
420
extern unsigned long RepeatCnt;
421
extern unsigned HashSize;
422
extern unsigned long TTHashMask;
423
extern unsigned long PHashMask;
424
extern int slider[8];
426
extern char SANmv[SANSZ];
427
extern unsigned long history[2][4096];
428
extern int killer1[MAXPLYDEPTH];
429
extern int killer2[MAXPLYDEPTH];
430
extern int ChkCnt[MAXPLYDEPTH];
431
extern int ThrtCnt[MAXPLYDEPTH];
433
extern char solution[64];
434
extern float SearchTime;
435
extern int SearchDepth;
436
extern int MoveLimit[2];
437
extern float TimeLimit[2];
441
extern int hunged[2];
443
extern int Hashmv[MAXPLYDEPTH];
444
extern short RootPieces;
445
extern short RootPawns;
446
extern short RootMaterial;
447
extern short RootAlpha;
448
extern short RootBeta;
449
extern short pickphase[MAXPLYDEPTH];
450
extern short InChk[MAXPLYDEPTH];
451
extern short KingThrt[2][MAXPLYDEPTH];
452
extern short KingSafety[2];
453
extern short pscore[64];
455
extern short bookmode;
456
extern short bookfirstlast;
460
extern char algbr[64][3];
461
extern char algbrfile[9];
462
extern char algbrrank[9];
463
extern char notation[8];
464
extern char lnotation[8];
468
extern int Mask45[64];
469
extern int Mask315[64];
475
extern char *progname;
477
extern int myrating, opprating, suddendeath;
480
extern char name[MAXNAMESZ];
481
extern int computerplays;
482
extern int wasbookmove;
483
extern int nmovesfrombook;
484
extern float maxtime;
485
extern int n; /* Last mobility returned by CTL */
486
extern int ExchCnt[2];
487
extern int newpos, existpos; /* For book statistics */
488
extern int bookloaded;
490
enum Piece { empty, pawn, knight, bishop, rook, queen, king, bpawn };
492
enum Square { A1, B1, C1, D1, E1, F1, G1, H1,
493
A2, B2, C2, D2, E2, F2, G2, H2,
494
A3, B3, C3, D3, E3, F3, G3, H3,
495
A4, B4, C4, D4, E4, F4, G4, H4,
496
A5, B5, C5, D5, E5, F5, G5, H5,
497
A6, B6, C6, D6, E6, F6, G6, H6,
498
A7, B7, C7, D7, E7, F7, G7, H7,
499
A8, B8, C8, D8, E8, F8, G8, H8 };
501
enum File { A_FILE, B_FILE, C_FILE, D_FILE, E_FILE, F_FILE, G_FILE, H_FILE };
503
/****************************************************************************
505
* The various function prototypes. They are group into the *.c files
506
* in which they are defined.
508
****************************************************************************/
511
* Explanation of the #ifdef NO_INLINE conditionals:
513
* Define NO_INLINE only if you really must, implementations will be
514
* provided by the corresponding *.c files. The better solution is to
515
* not define it, in which case inlines.h will be included which
516
* provides static inline version of these functions.
519
/* The initialization routines */
520
void Initialize (void);
521
void InitLzArray (void);
522
void InitBitPosArray (void);
523
void InitMoveArray (void);
525
void InitFromToRay (void);
526
void InitRankFileBit (void);
527
void InitBitCount (void);
528
void InitPassedPawnMask (void);
529
void InitIsolaniMask (void);
530
void InitSquarePawnMask (void);
531
void InitRandomMasks (void);
532
void InitRotAtak (void);
533
void InitDistance (void);
534
void InitVars (void);
535
void InitHashCode (void);
536
void InitHashTable (void);
537
void CalcHashSize (int);
538
void NewPosition (void);
539
void InitFICS (void);
540
void InitInput (void);
542
/* Cleanup routines */
543
void CleanupInput(void);
545
/* The book routines */
547
int BookBuilderOpen(void);
548
int BookBuilder (short result, uint8_t side);
549
int BookBuilderClose(void);
552
* Return values (errorcodes) for the book routines,
553
* maybe one should have a global enum of errorcodes
557
BOOK_EFORMAT, /* Wrong format (e.g. produced by older version) */
558
BOOK_EMIDGAME, /* Move is past the opening book's move limit */
559
BOOK_EIO, /* I/O error, e.g. caused by wrong permissions */
560
BOOK_EFULL, /* Book hash is full, new position was not added. */
561
BOOK_ENOBOOK, /* No book present */
562
BOOK_ENOMOVES, /* No moves found (in BookQuery() only) */
563
BOOK_ENOMEM /* Memory allocation failed */
566
/* The move generation routines */
567
void GenMoves (short);
568
void GenCaptures (short);
569
void GenNonCaptures (short);
570
void GenCheckEscapes (short);
571
void FilterIllegalMoves (short);
573
/* The move routines */
574
void MakeMove (int, int *);
575
void UnmakeMove (int, int *);
576
void MakeNullMove (int);
577
void UnmakeNullMove (int);
578
void SANMove (int, int);
579
leaf *ValidateMove (char *);
580
leaf *IsInMoveList (int, int, int, char);
581
int IsLegalMove (int);
582
char *AlgbrMove (int);
584
/* The attack routines */
585
short SqAtakd (short sq, short side);
586
void GenAtaks (void);
587
BitBoard AttackTo (int, int);
588
BitBoard AttackXTo (int, int);
589
BitBoard AttackFrom (int, int, int);
590
BitBoard AttackXFrom (int, int);
591
int PinnedOnKing (int, int);
592
void FindPins (BitBoard *);
595
/* The swap routines */
597
void AddXrayPiece (int, int, int, BitBoard *, BitBoard *);
599
/* The EPD routines */
600
short ReadEPDFile (const char *, short);
601
int ParseEPD (char *);
602
void LoadEPD (char *);
603
void SaveEPD (char *);
605
/* Error codes for ParseEPD */
611
void UpdateFriends (void);
612
void UpdateCBoard (void);
613
void UpdateMvboard (void);
614
void EndSearch (int);
615
short ValidateBoard (void);
618
void PGNSaveToFile (const char *, const char *);
619
void PGNReadFromFile (const char *);
620
void BookPGNReadFromFile (const char *);
621
int IsTrustedPlayer(const char *name);
624
/* The hash routines */
625
void CalcHashKey (void);
626
void ShowHashKey (HashType);
628
/* The evaluation routines */
635
int ScoreDev (short);
636
int Evaluate (int, int);
637
short EvaluateDraw (void);
642
/* The search routines */
644
int Search (uint8_t ply, short depth, int alpha, int beta, short nodetype);
645
int SearchRoot (short depth, int alpha, int beta);
646
int Quiesce (uint8_t ply, int alpha, int beta);
647
void pick (leaf *, short);
649
void ShowLine (int, int, char);
652
* Set up a timer by first calling StartTiming(), saving
653
* the return value and feeding it to GetElapsed() for
654
* timings in seconds.
656
typedef struct timeval Timer;
657
extern double ElapsedTime;
658
extern Timer StartTime;
659
Timer StartTiming (void);
660
double GetElapsed (Timer start);
662
/* The transposition table routies */
663
void TTPut (uint8_t side, uint8_t depth, uint8_t ply,
664
int alpha, int beta, int score, int move);
665
/* Returns flag if it finds an entry, 0 otherwise */
666
uint8_t TTGet (uint8_t side, uint8_t depth, uint8_t ply,
667
int *score, int *move);
668
short TTGetPV (uint8_t side, uint8_t ply, int score, int *move);
672
/* Sorting routines */
673
void SortCaptures (int);
674
void SortMoves (int);
675
void SortRoot (void);
676
int PhasePick (leaf **, int);
677
int PhasePick1 (leaf **, int);
679
/* Some output routines */
680
void ShowMoveList (int);
681
void ShowSmallBoard (void);
682
void ShowBoard (void);
683
void ShowBitBoard (BitBoard *);
684
void ShowCBoard (void);
685
void ShowMvboard (void);
687
void ShowGame (void);
688
void ShowTime (void);
690
/* Random numbers routines */
691
uint32_t Rand32 (void);
692
HashType Rand64 (void);
694
/* Solver routines */
698
void TestMoveGenSpeed (void);
699
void TestNonCaptureGenSpeed (void);
700
void TestCaptureGenSpeed (void);
701
void TestMoveList (void);
702
void TestNonCaptureList (void);
703
void TestCaptureList (void);
704
void TestEvalSpeed (void);
705
void TestEval (void);
707
/* Player database */
708
void DBSortPlayer (const char *style);
709
void DBListPlayer (const char *style);
710
void DBReadPlayer (void);
711
void DBWritePlayer (void);
712
int DBSearchPlayer (const char *player);
713
void DBUpdatePlayer (const char *player, const char *resultstr);
716
/* Input thread and thread function */
718
extern pthread_t input_thread;
719
void *input_func(void *);
722
* Status variable used by the input thread to signal
723
* pending input. Thought about using flags for this
724
* but this can be refined and is conceptually different
731
extern volatile int input_status;
734
* Function to wake up the input thread, should be called after
735
* input has been parsed.
737
void input_wakeup(void);
739
/* Wait for input. */
741
void wait_for_input(void);
744
* Input routine, initialized to one of the specific
745
* input routines. The given argument is the prompt.
747
void (*getline_intrl) (char *);
750
extern char inputstr[MAXSTR];
753
void parse_input(void);
758
/* The command subroutines */
759
void ShowCmd (char *);
760
void BookCmd (char *);
761
void TestCmd (char *);
763
/* Commands from the input engine */
764
void cmd_accepted(void);
765
void cmd_activate(void);
766
void cmd_analyze(void);
768
void cmd_black(void);
770
void cmd_computer(void);
771
void cmd_depth(void);
777
void cmd_force(void);
781
void cmd_hashsize(void);
782
void cmd_help (void);
784
void cmd_level(void);
787
void cmd_manual(void);
788
void cmd_movenow(void);
791
void cmd_nopost(void);
794
void cmd_pgnload(void);
795
void cmd_pgnsave(void);
798
void cmd_protover(void);
800
void cmd_random(void);
801
void cmd_rating(void);
802
void cmd_rejected(void);
803
void cmd_remove(void);
804
void cmd_result(void);
806
void cmd_setboard(void);
807
void cmd_show (void);
808
void cmd_solve(void);
810
void cmd_switch(void);
811
void cmd_test (void);
814
void cmd_usage(void);
815
void cmd_variant(void);
816
void cmd_version(void);
817
void cmd_white(void);
818
void cmd_xboard(void);
821
* Define NO_INLINE only if you really must, implementations will be
822
* provided by the corresponding *.c files. The better solution is to
823
* not define it, in which case inlines.h will be included which
824
* provides static inline version of these functions.
827
/* Some utility routines */
829
unsigned char leadz (BitBoard);
830
unsigned char nbits (BitBoard);
832
# include "inlines.h"
835
/* More elaborate debugging output to logfile */
837
/* All the following functions are no-ops if DEBUG is not defined */
840
* dbg_open() can be called with NULL as argument, using a default
841
* filename, defined in debug.c, for the debug log. Otherwise the
842
* argument is the filename. If dbg_open() fails or is not called at
843
* all, debugging output goes to stderr by default.
845
int dbg_open(const char *name);
847
/* Same format rules as printf() */
848
int dbg_printf(const char *fmt, ...);
850
/* Closes the debugging log, if it is not stderr */
855
# define ASSERT(x) assert(x)
860
#endif /* !COMMON_H */