Subversion Repositories Games.Chess Giants

Rev

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