Subversion Repositories Games.Chess Giants

Rev

Rev 33 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  *******************************************************************************
  3.  *                                                                             *
  4.  *   configuration information:  the following variables need to be set to     *
  5.  *   indicate the machine configuration/capabilities.                          *
  6.  *                                                                             *
  7.  *   UNIX:  define this if the program is being run on a unix-based system,    *
  8.  *   which causes the executable to use unix-specific runtime utilities.       *
  9.  *                                                                             *
  10.  *   CPUS=N:  this sets up data structures to the appropriate size to support  *
  11.  *   up to N simultaneous search engines.  note that you can set this to a     *
  12.  *   value larger than the max processors you currently have, because the mt=n *
  13.  *   command (added to the command line or your crafty.rc/.craftyrc file) will *
  14.  *   control how many threads are actually spawned.                            *
  15.  *                                                                             *
  16.  *******************************************************************************
  17.  */
  18. /* *INDENT-OFF* */
  19.  
  20. #if defined(AFFINITY)
  21. #  define _GNU_SOURCE
  22. #  include <sched.h>
  23. #endif
  24. #if defined(UNIX)
  25. #  define _GNU_SOURCE
  26. #  if (CPUS > 1)
  27. #    include <pthread.h>
  28. #  endif
  29. #  include <unistd.h>
  30. #  include <sys/types.h>
  31. #  include <sys/stat.h>
  32. #endif
  33. #include <stdint.h>
  34. #include <inttypes.h>
  35. #include <time.h>
  36. #include <stdio.h>
  37. #include <assert.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #if !defined(TYPES_INCLUDED)
  41. #  define TYPES_INCLUDED
  42. #  if !defined (UNIX)
  43. #    define RESTRICT __restrict
  44. #  else
  45. #    define RESTRICT
  46. #  endif
  47. #  if !defined(CPUS)
  48. #    define CPUS 1
  49. #  endif
  50. #  if !defined(UNIX)
  51. #    include <windows.h>
  52. #    include <process.h>
  53. #  endif
  54. #  define CDECL
  55. #  define STDCALL
  56. /* Provide reasonable defaults for UNIX systems. */
  57. #  if !defined(UNIX)
  58. #    undef  STDCALL
  59. #    define STDCALL __stdcall
  60. #    ifdef  VC_INLINE32
  61. #      undef  CDECL
  62. #      define CDECL __cdecl
  63. #    endif
  64. #  endif
  65. #  if !defined(BOOKDIR)
  66. #    define   BOOKDIR      "."
  67. #  endif
  68. #  if !defined(LOGDIR)
  69. #    define   LOGDIR       "."
  70. #  endif
  71. #  if !defined(TBDIR)
  72. #    define   TBDIR        "./TB"
  73. #  endif
  74. #  if !defined(RCDIR)
  75. #    define   RCDIR        "."
  76. #  endif
  77. #  include "lock.h"
  78. #  define MAXPLY                                 129
  79. #  define MAX_TC_NODES                       3000000
  80. #  define MAX_BLOCKS                       64 * CPUS
  81. #  define BOOK_CLUSTER_SIZE                     8000
  82. #  define MERGE_BLOCK                           1000
  83. #  define SORT_BLOCK                         4000000
  84. #  define LEARN_INTERVAL                          10
  85. #  define LEARN_COUNTER_BAD                      -80
  86. #  define LEARN_COUNTER_GOOD                    +100
  87. #  define MATE                                 32768
  88. #  define PAWN_VALUE                             100
  89. #  define KNIGHT_VALUE                           305
  90. #  define BISHOP_VALUE                           305
  91. #  define ROOK_VALUE                             490
  92. #  define QUEEN_VALUE                           1000
  93. #  define KING_VALUE                           40000
  94. #  if !defined(CLOCKS_PER_SEC)
  95. #    define CLOCKS_PER_SEC 1000000
  96. #  endif
  97. typedef enum {
  98.   A1, B1, C1, D1, E1, F1, G1, H1,
  99.   A2, B2, C2, D2, E2, F2, G2, H2,
  100.   A3, B3, C3, D3, E3, F3, G3, H3,
  101.   A4, B4, C4, D4, E4, F4, G4, H4,
  102.   A5, B5, C5, D5, E5, F5, G5, H5,
  103.   A6, B6, C6, D6, E6, F6, G6, H6,
  104.   A7, B7, C7, D7, E7, F7, G7, H7,
  105.   A8, B8, C8, D8, E8, F8, G8, H8,
  106.   BAD_SQUARE
  107. } squares;
  108. typedef enum { FILEA, FILEB, FILEC, FILED, FILEE, FILEF, FILEG, FILEH } files;
  109. typedef enum { RANK1, RANK2, RANK3, RANK4, RANK5, RANK6, RANK7, RANK8 } ranks;
  110. typedef enum { empty = 0, occupied = 0, pawn = 1, knight = 2, bishop = 3,
  111.   rook = 4, queen = 5, king = 6
  112. } PIECE;
  113. typedef enum { black = 0, white = 1 } COLOR;
  114. typedef enum { mg = 0, eg = 1 } PHASE;
  115. typedef enum { empty_v = 0, pawn_v = 1, knight_v = 3,
  116.                bishop_v = 3, rook_v = 5, queen_v = 9, king_v = 99
  117. } PIECE_V;
  118. typedef enum { serial = 0, parallel = 1} SEARCH_MODE;
  119. typedef enum { think = 1, puzzle = 2, book = 3, annotate = 4 } SEARCH_TYPE;
  120. typedef enum { normal_mode, tournament_mode } PLAYING_MODE;
  121. typedef struct {
  122.   int8_t castle[2];
  123.   uint8_t enpassant_target;
  124.   uint8_t reversible;
  125. } SEARCH_POSITION;
  126. typedef struct {
  127.   int move1;
  128.   int move2;
  129. } KILLER;
  130. typedef struct {
  131.   uint64_t pieces[7];
  132. } BB_PIECES;
  133. typedef struct {
  134.   BB_PIECES color[2];
  135.   uint64_t hash_key;
  136.   uint64_t pawn_hash_key;
  137.   int material_evaluation;
  138.   int kingsq[2];
  139.   int8_t board[64];
  140.   char pieces[2][7];
  141.   char total_all_pieces;
  142. } POSITION;
  143. typedef struct {
  144.   uint64_t word1;
  145.   uint64_t word2;
  146. } HASH_ENTRY;
  147. typedef struct {
  148.   uint64_t key;
  149.   int32_t score_mg, score_eg;
  150.   unsigned char defects_k[2];
  151.   unsigned char defects_m[2];
  152.   unsigned char defects_q[2];
  153.   unsigned char passed[2];
  154. } PAWN_HASH_ENTRY;
  155. typedef struct {
  156.   uint64_t entry[3];
  157. } PXOR;
  158. typedef struct {
  159.   int path[MAXPLY];
  160.   int pathh;
  161.   int pathl;
  162.   int pathd;
  163.   int pathv;
  164. } PATH;
  165. typedef struct {
  166.   uint64_t path_sig;
  167.   int hash_pathl;
  168.   int hash_path_age;
  169.   int hash_path_moves[MAXPLY];
  170. } HPATH_ENTRY;
  171. typedef struct {
  172.   int phase;
  173.   int order;
  174.   int remaining;
  175.   unsigned *last;
  176.   unsigned done[10];
  177.   unsigned *exclude;
  178. } NEXT_MOVE;
  179. /*
  180.    root_move.status:
  181.  
  182.    xxx1 = failed low this iteration
  183.    xx1x = failed high this iteration
  184.    x1xx = don't search in parallel or reduce
  185.    1xxx = move has been searched
  186.  */
  187. typedef struct {
  188.   int move;
  189.   unsigned status;
  190.   int bm_age;
  191.   PATH path;
  192. } ROOT_MOVE;
  193. #  if !defined(UNIX)
  194. #    pragma pack(4)
  195. #  endif
  196. typedef struct {
  197.   uint64_t position;
  198.   unsigned status_played;
  199.   float learn;
  200. } BOOK_POSITION;
  201. #  if !defined(UNIX)
  202. #    pragma pack()
  203. #  endif
  204. typedef struct {
  205.   unsigned char position[8];
  206.   unsigned char status;
  207.   unsigned char percent_play;
  208. } BB_POSITION;
  209. struct personality_term {
  210.   char *description;
  211.   int type;
  212.   int size;
  213.   void *value;
  214. };
  215. struct autotune {
  216.   unsigned int min;
  217.   unsigned int max;
  218.   unsigned int increment;
  219.   char description[64];
  220.   char command[16];
  221.   unsigned int *parameter;
  222. };
  223. typedef struct tree {
  224. /* commonly used variables */
  225.   SEARCH_POSITION status[MAXPLY + 3];
  226.   NEXT_MOVE next_status[MAXPLY];
  227.   KILLER killers[MAXPLY];
  228.   POSITION position;
  229.   uint64_t save_hash_key[MAXPLY + 3];
  230.   uint64_t save_pawn_hash_key[MAXPLY + 3];
  231.   uint64_t rep_list[256];
  232.   int curmv[MAXPLY];
  233.   int phase[MAXPLY];
  234.   int hash_move[MAXPLY];
  235.   unsigned *last[MAXPLY];
  236.   unsigned move_list[5120];
  237.   PATH pv[MAXPLY];
  238. /* variables used by Evaluate() */
  239.   PAWN_HASH_ENTRY pawn_score;
  240.   int tropism[2];
  241.   int dangerous[2];
  242.   uint64_t all_pawns;
  243.   int score_mg, score_eg;
  244. /* statistical counters */
  245.   uint64_t nodes_searched;
  246.   uint64_t fail_highs;
  247.   uint64_t fail_high_first_move;
  248.   uint64_t evaluations;
  249.   uint64_t egtb_probes;
  250.   uint64_t egtb_hits;
  251.   uint64_t extensions_done;
  252.   uint64_t qchecks_done;
  253.   uint64_t moves_fpruned;
  254.   uint64_t moves_mpruned;
  255.   uint64_t LMR_done[16];
  256.   uint64_t null_done[16];
  257. /* thread stuff */
  258.   lock_t lock;
  259.   int thread_id;
  260.   volatile int joinable;
  261.   volatile int joined;
  262.   volatile int stop;
  263.   volatile int nprocs;
  264.   int alpha;
  265.   int beta;
  266.   volatile int value;
  267.   int wtm;
  268.   int depth;
  269.   int ply;
  270.   int in_check;
  271.   int *searched;
  272.   int cutmove;
  273.   struct tree *volatile siblings[CPUS], *parent;
  274. /* rarely accessed */
  275.   char root_move_text[16];
  276.   char remaining_moves_text[16];
  277. } TREE;
  278. typedef struct thread {
  279.   TREE *tree;
  280.   uint64_t blocks;
  281.   uint64_t max_blocks;
  282.   unsigned int idle;
  283.   volatile unsigned int terminate;
  284.   char filler[40];
  285. } THREAD;
  286. /*
  287.    DO NOT modify these.  these are constants, used in multiple modules.
  288.    modification may corrupt the search in any number of ways, all bad.
  289.  */
  290. #  define WORTHLESS                    0
  291. #  define UPPER                        1
  292. #  define LOWER                        2
  293. #  define EXACT                        3
  294. #  define HASH_MISS                    0
  295. #  define HASH_HIT                     1
  296. #  define AVOID_NULL_MOVE              2
  297. #  define NO_NULL                      0
  298. #  define DO_NULL                      1
  299. #  define NONE                         0
  300. #  define NULL_MOVE                    1
  301. #  define DO_NOT_REDUCE                1
  302. #  define HASH                         2
  303. #  define GENERATE_CAPTURES            3
  304. #  define CAPTURES                     4
  305. #  define KILLER1                      5
  306. #  define KILLER2                      6
  307. #  define KILLER3                      7
  308. #  define KILLER4                      8
  309. #  define COUNTER_MOVE1                9
  310. #  define COUNTER_MOVE2               10
  311. #  define MOVE_PAIR1                  11
  312. #  define MOVE_PAIR2                  12
  313. #  define GENERATE_QUIET              13
  314. #  define HISTORY                     14
  315. #  define REMAINING                   15
  316. #  define EVALUATION                  16
  317. #  define ILLEGAL                      0
  318. #  define LEGAL                        1
  319. #  define IN_WINDOW                    2
  320. #  define FAIL_HIGH                    3
  321. #if /*defined(UNIX) &&*/ !defined(INLINEASM) // Pierre-Marie Baty -- we also use them on Windows...
  322. int CDECL PopCnt(uint64_t);
  323. int CDECL MSB(uint64_t);
  324. int CDECL LSB(uint64_t);
  325. #endif
  326. void AlignedMalloc(void **, int, size_t);
  327. void AlignedRemalloc(void **, int, size_t);
  328. void Analyze(void);
  329. void Annotate(void);
  330. void AnnotateHeaderHTML(char *, FILE *);
  331. void AnnotateFooterHTML(FILE *);
  332. void AnnotatePositionHTML(TREE *RESTRICT, int, FILE *);
  333. char *AnnotateVtoNAG(int, int, int, int);
  334. void AnnotateHeaderTeX(FILE *);
  335. void AnnotateFooterTeX(FILE *);
  336. void AnnotatePositionTeX(TREE *, int, FILE *);
  337. uint64_t atoiKMB(char *);
  338. int Attacks(TREE *RESTRICT, int, int);
  339. uint64_t Attacked(TREE *RESTRICT, int, uint64_t);
  340. uint64_t AttacksFrom(TREE *RESTRICT, int, int);
  341. uint64_t AttacksTo(TREE *RESTRICT, int);
  342. void AutoTune(int, char **);
  343. int Bench(int, int);
  344. int Bench_PGO(int, int);
  345. int Book(TREE *RESTRICT, int);
  346. void BookClusterIn(FILE *, int, BOOK_POSITION *);
  347. void BookClusterOut(FILE *, int, BOOK_POSITION *);
  348. int BookIn32(unsigned char *);
  349. float BookIn32f(unsigned char *);
  350. uint64_t BookIn64(unsigned char *);
  351. int BookMask(char *);
  352. unsigned char *BookOut32(int);
  353. unsigned char *BookOut32f(float);
  354. unsigned char *BookOut64(uint64_t);
  355. int BookPonderMove(TREE *RESTRICT, int);
  356. void Bookup(TREE *RESTRICT, int, char **);
  357. void BookSort(BB_POSITION *, int, int);
  358. int BookupCompare(const void *, const void *);
  359. BB_POSITION BookupNextPosition(int, int);
  360. int CheckInput(void);
  361. void ClearHashTableScores(void);
  362. int ComputeDifficulty(int, int);
  363. void CopyFromParent(TREE *RESTRICT);
  364. void CopyToParent(TREE *RESTRICT, TREE *RESTRICT, int);
  365. void CraftyExit(int);
  366. void DisplayArray(int *, int);
  367. void DisplayArrayX2(int *, int *, int);
  368. void DisplayBitBoard(uint64_t);
  369. void Display2BitBoards(uint64_t, uint64_t);
  370. void DisplayChessBoard(FILE *, POSITION);
  371. char *DisplayEvaluation(int, int);
  372. char *DisplayEvaluationKibitz(int, int);
  373. void DisplayFT(int, int, int);
  374. char *DisplayHHMM(unsigned);
  375. char *DisplayHHMMSS(unsigned);
  376. char *DisplayKMB(uint64_t, int);
  377. void DisplayFail(TREE *RESTRICT, int, int, int, int, int, int, int);
  378. char *DisplayPath(TREE *RESTRICT, int, PATH *);
  379. void DisplayPV(TREE *RESTRICT, int, int, int, PATH *, int);
  380. char *DisplayTime(unsigned);
  381. char *Display2Times(unsigned);
  382. char *DisplayTimeKibitz(unsigned);
  383. void DisplayChessMove(char *, int);
  384. int Drawn(TREE *RESTRICT, int);
  385. void Edit(void);
  386. #  if !defined(NOEGTB)
  387. int EGTBProbe(TREE *RESTRICT, int, int, int *);
  388. void EGTBPV(TREE *RESTRICT, int);
  389. #  endif
  390. int Evaluate(TREE *RESTRICT, int, int, int, int);
  391. void EvaluateBishops(TREE *RESTRICT, int);
  392. void EvaluateCastling(TREE *RESTRICT, int, int);
  393. int EvaluateDraws(TREE *RESTRICT, int, int, int);
  394. int EvaluateHasOpposition(int, int, int);
  395. void EvaluateKing(TREE *RESTRICT, int, int);
  396. int EvaluateKingsFile(TREE * RESTRICT, int, int, int);
  397. void EvaluateKnights(TREE *RESTRICT, int);
  398. void EvaluateMate(TREE *RESTRICT, int);
  399. void EvaluateMaterial(TREE *RESTRICT, int);
  400. void EvaluatePassedPawns(TREE *RESTRICT, int, int);
  401. void EvaluatePassedPawnRaces(TREE *RESTRICT, int);
  402. void EvaluatePawns(TREE *RESTRICT, int);
  403. void EvaluateQueens(TREE *RESTRICT, int);
  404. void EvaluateRooks(TREE *RESTRICT, int);
  405. int EvaluateWinningChances(TREE *RESTRICT, int, int);
  406. void EVTest(char *);
  407. int Exclude(TREE *RESTRICT, int, int);
  408. int FindBlockID(TREE *RESTRICT);
  409. char *FormatPV(TREE *RESTRICT, int, PATH);
  410. int FTbSetCacheSize(void *, unsigned long);
  411. int GameOver(int);
  412. unsigned *GenerateCaptures(TREE *RESTRICT, int, int, unsigned *);
  413. unsigned *GenerateCheckEvasions(TREE *RESTRICT, int, int, unsigned *);
  414. unsigned *GenerateChecks(TREE *RESTRICT, int, unsigned *);
  415. unsigned *GenerateNoncaptures(TREE *RESTRICT, int, int, unsigned *);
  416. TREE *GetBlock(TREE *, int);
  417. void Initialize(void);
  418. void InitializeAttackBoards(void);
  419. void InitializeChessBoard(TREE *);
  420. int InitializeGetLogID();
  421. void InitializeHashTables(int);
  422. void InitializeKillers(void);
  423. void InitializeKingSafety(void);
  424. void InitializeMagic(void);
  425. uint64_t InitializeMagicBishop(int, uint64_t);
  426. uint64_t InitializeMagicRook(int, uint64_t);
  427. uint64_t InitializeMagicOccupied(int *, int, uint64_t);
  428. void InitializeMasks(void);
  429. void InitializePawnMasks(void);
  430. void InitializeReductions(void);
  431. void InitializeSMP(void);
  432. int IInitializeTb(char *);
  433. int InputMove(TREE *RESTRICT, int, int, int, int, char *);
  434. int InputMoveICS(TREE *RESTRICT, int, int, int, int, char *);
  435. uint64_t InterposeSquares(int, int, int);
  436. void Interrupt(int);
  437. int InvalidPosition(TREE *RESTRICT);
  438. int Iterate(int, int, int);
  439. int Join(int64_t);
  440. void Kibitz(int, int, int, int, int, uint64_t, int, int, char *);
  441. void History(TREE *RESTRICT, int, int, int, int, int*);
  442. int KingPawnSquare(int, int, int, int);
  443. int LearnAdjust(int);
  444. void LearnBook(void);
  445. int LearnFunction(int, int, int, int);
  446. void LearnValue(int, int);
  447. void MakeMove(TREE *RESTRICT, int, int, int);
  448. void MakeMoveRoot(TREE *RESTRICT, int, int);
  449. void NewGame(int);
  450. int NextMove(TREE *RESTRICT, int, int, int, int);
  451. int NextRootMove(TREE *RESTRICT, TREE *RESTRICT, int);
  452. int NextRootMoveParallel(void);
  453. void NextSort(TREE *RESTRICT, int);
  454. int Option(TREE *RESTRICT);
  455. int OptionMatch(char *, char *);
  456. void OptionPerft(TREE *RESTRICT, int, int, int);
  457. void Output(TREE *RESTRICT);
  458. char *OutputMove(TREE *RESTRICT, int, int, int);
  459. int ParseTime(char *);
  460. void Pass(void);
  461. int PinnedOnKing(TREE *RESTRICT, int, int);
  462. int Ponder(int);
  463. void Print(int, char *, ...);
  464. int HashProbe(TREE *RESTRICT, int, int, int, int, int, int*);
  465. void HashStore(TREE *RESTRICT, int, int, int, int, int, int);
  466. void HashStorePV(TREE *RESTRICT, int, int);
  467. int Quiesce(TREE *RESTRICT, int, int, int, int, int);
  468. int QuiesceEvasions(TREE *RESTRICT, int, int, int, int);
  469. unsigned Random32(void);
  470. uint64_t Random64(void);
  471. int Read(int, char *);
  472. int ReadChessMove(TREE *RESTRICT, FILE *, int, int);
  473. void ReadClear(void);
  474. unsigned ReadClock(void);
  475. int ReadPGN(FILE *, int);
  476. int ReadNextMove(TREE *RESTRICT, char *, int, int);
  477. int ReadParse(char *, char *args[], char *);
  478. int ReadInput(void);
  479. int Repeat(TREE *RESTRICT, int);
  480. int Repeat3x(TREE *RESTRICT);
  481. void ResignOrDraw(TREE *RESTRICT, int);
  482. void RestoreGame(void);
  483. void RootMoveList(int);
  484. int Search(TREE *RESTRICT, int, int, int, int, int, int, int);
  485. int SearchMove(TREE *RESTRICT, int, int, int, int, int, int, int, int, int);
  486. int SearchMoveList(TREE *RESTRICT, int, int, int, int, int, int *, int, int, int);
  487. int SearchNull(TREE * RESTRICT, int, int, int, int);
  488. void Trace(TREE *RESTRICT, int, int, int, int, int, const char *, int, int, int);
  489. void SetBoard(TREE *, int, char **, int);
  490. void SetChessBitBoards(TREE *);
  491. void SharedFree(void *address);
  492. void SortRootMoves(void);
  493. int Split(TREE *RESTRICT);
  494. int StrCnt(char *, char);
  495. int SEE(TREE *RESTRICT, int, int);
  496. int SEEO(TREE *RESTRICT, int, int);
  497. void Test(char *, FILE *, int, int);
  498. void TestEPD(char *, FILE *, int, int);
  499. void ThreadAffinity(int);
  500. void *STDCALL ThreadInit(void *);
  501. #  if !defined(UNIX)
  502. void ThreadMalloc(int64_t);
  503. #  endif
  504. int ThreadSplit(TREE *RESTRICT, int, int, int, int, int);
  505. void ThreadStop(TREE *RESTRICT);
  506. void ThreadTrace(TREE * RESTRICT, int, int);
  507. int ThreadWait(int, TREE *RESTRICT);
  508. int Threat(TREE *, int, int, int, int);
  509. void TimeAdjust(int, int);
  510. int TimeCheck(TREE *RESTRICT, int);
  511. void TimeSet(int);
  512. void UnmakeMove(TREE *RESTRICT, int, int, int);
  513. int ValidMove(TREE *RESTRICT, int, int, int);
  514. int VerifyMove(TREE *RESTRICT, int, int, int);
  515. void ValidatePosition(TREE *RESTRICT, int, int, char *);
  516. void WaitForAllThreadsInitialized(void);
  517. #  if !defined(UNIX)
  518. extern void *WinMallocInterleaved(size_t, int);
  519. extern void WinFreeInterleaved(void *, size_t);
  520. #    define MallocInterleaved(cBytes, cThreads)     \
  521.        WinMallocInterleaved(cBytes, cThreads)
  522. #    define FreeInterleaved(pMemory, cBytes)        \
  523.        WinFreeInterleaved(pMemory, cBytes)
  524. #  else
  525. #    if defined(NUMA)
  526. #      define MallocInterleaved(cBytes, cThreads) numa_alloc_interleaved(cBytes)
  527. #      define FreeInterleaved(pMemory, cBytes)          numa_free(pMemory, 1)
  528. #    else
  529. #      define MallocInterleaved(cBytes, cThreads) malloc(cBytes)
  530. #      define FreeInterleaved(pMemory, cBytes)          free(pMemory)
  531. #    endif
  532. #  endif
  533. #  define Abs(a)    (((a) > 0) ? (a) : -(a))
  534. #  define Max(a,b)  (((a) > (b)) ? (a) : (b))
  535. #  define Min(a,b)  (((a) < (b)) ? (a) : (b))
  536. #  define Sign(a)   ((a) < 0 ? -1 : +1)
  537. #  define FileDistance(a,b) abs(File(a) - File(b))
  538. #  define RankDistance(a,b) abs(Rank(a) - Rank(b))
  539. #  define Distance(a,b) Max(FileDistance(a,b), RankDistance(a,b))
  540. #  define DrawScore(side)                  (draw_score[side])
  541. #  define PopCnt8Bit(a) (pop_cnt_8bit[a])
  542. #  define MSB8Bit(a) (msb_8bit[a])
  543. #  define LSB8Bit(a) (lsb_8bit[a])
  544. #  define HistoryIndex(side, m) ((side << 9) + (Piece(m) << 6) + To(m))
  545. /*
  546.    side = side to move
  547.    mptr = pointer into move list
  548.    m = bit vector of to squares to unpack
  549.    t = pre-computed from + moving piece
  550.  */
  551. #  define Extract(side, mptr, m, t)                                         \
  552.   for ( ; m ; Clear(to, m)) {                                               \
  553.     to = MostAdvanced(side, m);                                             \
  554.     *mptr++ = t | (to << 6) | (Abs(PcOnSq(to)) << 15);                      \
  555.   }
  556. #  define Check(side) Attacks(tree, Flip(side), KingSQ(side))
  557. #  define Attack(from,to) (!(intervening[from][to] & OccupiedSquares))
  558. #  define BishopAttacks(square, occ) *(magic_bishop_indices[square]+((((occ)&magic_bishop_mask[square])*magic_bishop[square])>>magic_bishop_shift[square]))
  559. #  define BishopMobility(square, occ) *(magic_bishop_mobility_indices[square]+((((occ)&magic_bishop_mask[square])*magic_bishop[square])>>magic_bishop_shift[square]))
  560. #  define KingAttacks(square) king_attacks[square]
  561. #  define KnightAttacks(square) knight_attacks[square]
  562. #  define PawnAttacks(side, square)   pawn_attacks[side][square]
  563. #  define Reversible(p)               (tree->status[p].reversible)
  564. #  define RookAttacks(square, occ) *(magic_rook_indices[square]+((((occ)&magic_rook_mask[square])*magic_rook[square])>>magic_rook_shift[square]))
  565. #  define RookMobility(square, occ) *(magic_rook_mobility_indices[square]+((((occ)&magic_rook_mask[square])*magic_rook[square])>>magic_rook_shift[square]))
  566. #  define QueenAttacks(square, occ)   (BishopAttacks(square, occ)|RookAttacks(square, occ))
  567. #  define Rank(x)        ((x)>>3)
  568. #  define File(x)        ((x)&7)
  569. #  define Flip(x)        ((x)^1)
  570. #  define MostAdvanced(side, squares) ((side) ? MSB(squares) : LSB(squares))
  571. #  define LeastAdvanced(side, squares) ((side) ? LSB(squares) : MSB(squares))
  572. #  define MinMax(side, v1, v2) ((side) ? Min((v1), (v2)) : Max((v1), (v2)))
  573. #  define InFront(side, k, p) ((side) ? k > p : k < p)
  574. #  define Behind(side, k, p) ((side) ? k < p : k > p)
  575. #  define Passed(sq, wtm)        (!(mask_passed[wtm][sq] & Pawns(Flip(wtm))))
  576. #  define RankAttacks(a) (RookAttacks(a, OccupiedSquares) & rank_mask[Rank(a)])
  577. #  define FileAttacks(a) (RookAttacks(a, OccupiedSquares) & file_mask[File(a)])
  578. #  define Diaga1Attacks(a) (BishopAttacks(a, OccupiedSquares) & (plus9dir[a] | minus9dir[a]))
  579. #  define Diagh1Attacks(a) (BishopAttacks(a, OccupiedSquares) & (plus7dir[a] | minus7dir[a]))
  580. #  define InterposeSquares(kingsq, checksq) intervening[kingsq][checksq]
  581. /*
  582.    the following macros are used to extract the pieces of a move that are
  583.    kept compressed into the rightmost 21 bits of a simple integer.
  584.  */
  585. #  define From(a)               ((a) & 63)
  586. #  define To(a)                 (((a)>>6) & 63)
  587. #  define Piece(a)              (((a)>>12) & 7)
  588. #  define Captured(a)           (((a)>>15) & 7)
  589. #  define Promote(a)            (((a)>>18) & 7)
  590. #  define Move(a)               (a & 0x1fffff)
  591. #  define SortV(a)              (a >> 21)
  592. #  define CaptureOrPromote(a)   (((a)>>15) & 63)
  593. #  define PawnPush(c, a)        (Piece(a) == pawn &&                          \
  594.                                      rankflip[c][Rank(To(a))] >= RANK6        \
  595.                                      && !(mask_passed[c][To(a)] &             \
  596.                                           Pawns(Flip(c))))
  597. #  define CastleMove(c, a)       (Piece(a) == king && Abs(File(To(a)) -       \
  598.                                      File(From(a))) > 1)
  599. #  define SetMask(a)             (set_mask[a])
  600. #  define ClearMask(a)           (clear_mask[a])
  601. #  define Pawns(c)               (tree->position.color[c].pieces[pawn])
  602. #  define Knights(c)             (tree->position.color[c].pieces[knight])
  603. #  define Bishops(c)             (tree->position.color[c].pieces[bishop])
  604. #  define Rooks(c)               (tree->position.color[c].pieces[rook])
  605. #  define Queens(c)              (tree->position.color[c].pieces[queen])
  606. #  define Kings(c)               (tree->position.color[c].pieces[king])
  607. #  define KingSQ(c)              (tree->position.kingsq[c])
  608. #  define Occupied(c)            (tree->position.color[c].pieces[occupied])
  609. #  define Pieces(c, p)           (tree->position.color[c].pieces[p])
  610. #  define TotalPieces(c, p)      (tree->position.pieces[c][p])
  611. #  define PieceValues(c, p)      (piece_values[c][p])
  612. #  define TotalAllPieces         (tree->position.total_all_pieces)
  613. #  define Material               (tree->position.material_evaluation)
  614. #  define MaterialSTM(side)      ((side) ? Material : -Material)
  615. #  define MateScore(s)           (Abs(s) > 32000)
  616. #  define Castle(ply, c)         (tree->status[ply].castle[c])
  617. #  define HashKey                (tree->position.hash_key)
  618. #  define PawnHashKey            (tree->position.pawn_hash_key)
  619. #  define EnPassant(ply)         (tree->status[ply].enpassant_target)
  620. #  define EnPassantTarget(ply)   (EnPassant(ply) ? SetMask(EnPassant(ply)) : 0)
  621. #  define PcOnSq(sq)             (tree->position.board[sq])
  622. #  define OccupiedSquares        (Occupied(white) | Occupied(black))
  623. #  define Color(square)       (square_color[square] ? dark_squares : ~dark_squares)
  624. #  define SideToMove(c)          ((c) ? "White" : "Black")
  625. /*
  626.    the following macros are used to Set and Clear a specific bit in the
  627.    second argument.  this is done to make the code more readable, rather
  628.    than to make it faster.
  629.  */
  630. #  define ClearSet(a,b)         b=((a) ^ (b))
  631. #  define Clear(a,b)            b=ClearMask(a) & (b)
  632. #  define Set(a,b)              b=SetMask(a) | (b)
  633. /*
  634.    the following macros are used to update the hash signatures.
  635.  */
  636. #  define Hash(stm,piece,square)     (HashKey^=randoms[stm][piece][square])
  637. #  define HashP(stm,square)          (PawnHashKey^=randoms[stm][pawn][square])
  638. #  define HashCastle(stm,direction)  (HashKey^=castle_random[stm][direction])
  639. #  define HashEP(sq)                 (HashKey^=enpassant_random[sq])
  640. #  define SavePV(tree,ply,ph)        do {                                     \
  641.         tree->pv[ply-1].path[ply-1]=tree->curmv[ply-1];                       \
  642.         tree->pv[ply-1].pathl=ply;                                            \
  643.         tree->pv[ply-1].pathh=ph;                                             \
  644.         tree->pv[ply-1].pathd=iteration;} while(0)
  645. #  if defined(INLINEASM)
  646. #    include "inline.h"
  647. #  endif
  648. #endif
  649. /* *INDENT-ON* */
  650.