1 #if LARGE_ETYPE
2 typedef int etype;
3 #if USE_STATIC_PAWN_VALUE
4 #define STATIC_PAWN_VALUE 1000
5 #else
6 #define STATIC_PAWN_VALUE 10000
7 #define PAWN_VALUE 10000
8 #endif
9 #else
10 typedef short etype;
11 #define STATIC_PAWN_VALUE 100
12 #define PAWN_VALUE 100
13 #endif
14 
15 #include "eval.h"
16 
17 #define DEFAULT_HASH_TABLE_SIZE 8 /* in Mb */
18 
19 #ifndef RAMP_FACTOR
20 #define RAMP_FACTOR 1.0
21 #endif
22 
23 #define CRAFTY 1
24 #define KNIGHTCAP 2
25 
26 #ifndef SHORT_GAME
27 #define SHORT_GAME 10
28 #endif
29 
30 #define STORE_ONLY_NEGATIVE 0
31 #define USE_APPLET 0
32 #define SEARCH_OPENING_VARIATIONS 0
33 
34 #define DUMPING_TD_UPDATES 1
35 
36 #define NO_STALEMATE_LEARN 1
37 
38 #define SEARCH_DEBUG 0
39 #define __COEFFS_PER_STAGE__ (__TOTAL_COEFFS__/(MATING+1))
40 #define USE_HASH_TABLES 1
41 #define USE_ETTC_HASH 1
42 #define USE_PAWN_DEEPENING 0
43 #define USE_CHECK_DEEPENING 1
44 #define USE_RECAPTURE_DEEPENING 1
45 #define USE_BEST_CAPTURE_DEEPENING 0
46 #define USE_HUNG_CAPTURE_DEEPENING 0
47 #define USE_SINGULAR_EXTENSION 1
48 #define USE_EVAL_TACTICS 1
49 #define USE_EGTB 1
50 
51 #define USE_NULL_MOVE 1
52 #define USE_NULLMOVE_FIXUP 1
53 #define CONFIRM_NULL_MOVES 1
54 
55 #define USE_OVERLOADED 1
56 #define USE_RAZORING 1
57 #define USE_ASYMMETRIC_RAZORING 0
58 #define USE_EVAL_SHORTCUT 1
59 #define USE_QUIESCE_SHORTCUT 0
60 #define USE_PESSIMISTIC_PRUNING 0
61 #define USE_ASYMMETRIC_EXTENSIONS 1
62 #define USE_ASYMMETRIC_EVAL 1
63 #define USE_ASYMMETRIC_NULLMOVE 1
64 
65 #ifndef USE_SLIDING_CONTROL
66 #define USE_SLIDING_CONTROL 1
67 #endif
68 
69 #define USE_FUTILITY 1
70 #define USE_FAST_QUIESCE 0
71 #define USE_PAWNPUSH_QUIESCE 0
72 
73 #define REJECT_LOW_BRAIN_MOVES 0
74 
75 #define TEST_QUIESCE 0
76 #define TEST_EVAL_SHORTCUT 0
77 #define TEST_NULL_MOVE 0
78 #define TEST_GRADIENT 0
79 #define TEST_MTD_FAST 0
80 #define SAVE_EXTENSIONS 0
81 
82 #define FUTILE_THRESHOLD (1.3*STATIC_PAWN_VALUE)
83 
84 #define USE_INTERNAL_PRUNING 0
85 #define USE_HALF_PRUNING 0
86 
87 #ifndef STORE_LEAF_POS
88 #define STORE_LEAF_POS 0
89 #endif
90 
91 #ifndef USE_PBRAIN
92 #define USE_PBRAIN 1
93 #endif
94 
95 #define BRAIN_FILE "brain.dat"
96 
97 #ifndef RESIGN_VALUE
98 #define RESIGN_VALUE (ROOK_VALUE)
99 #endif
100 
101 #define NULL_MOVE_PLY 3
102 #define PONDER_NULL_MOVE_PLY 3
103 
104 #define MAX_EXTENSIONS 20
105 
106 #define RAZOR_THRESHOLD (0.9*STATIC_PAWN_VALUE)
107 
108 #define MAX_MOVES_MULLED 3
109 
110 #define NULL_MOVE_THRESHOLD 6
111 
112 #define MTD_THRESHOLD (mulling?(0.01*STATIC_PAWN_VALUE):(0.07*STATIC_PAWN_VALUE))
113 #define MTD_DELTA (0.05*STATIC_PAWN_VALUE)
114 
115 #define MAX_CELLS 64
116 
117 #define TIMER_NODES 1000
118 
119 #define CAPTURE_SEARCH_DEPTH 0
120 
121 #define USE_FORWARD_PRUNING 0
122 #define PRUNE_THRESHOLD (STATIC_PAWN_VALUE/4)
123 
124 #define PAR_SPLIT_LEVEL 1
125 
126 #define TREE_PRINT_PLY 0
127 
128 #define MAX_DEPTH 50
129 
130 #define WALL_CLOCK 1
131 
132 #define HASH_LEVELS 2
133 
134 #define HASH_MISS 0
135 #define HASH_HIT 1
136 #define HASH_AVOID_NULL 2
137 
138 /* number of moves to buffer before writing gradients to disk */
139 #define BUFFERED_MOVES 10
140 #define GRAD_TEMP_SIZE (BUFFERED_MOVES*__TOTAL_COEFFS__)
141 
142 #define NUM_SQUARES (8*8)
143 #define MAX_MOVES (306) /* the maximum number of moves that can be
144 			   made in one move - probably a gross
145 			   overestimate */
146 #ifndef MAX_GAME_MOVES
147 #define MAX_GAME_MOVES 1000 /* the players should give up by then :-) */
148 #endif
149 #define PIECE_NONE 0
150 #define PIECE_WHITE 1
151 #define PIECE_BLACK -1
152 
153 #define IKING 0
154 #define IQUEEN 1
155 #define IKROOK 2
156 #define IQROOK 3
157 #define IKBISHOP 4
158 #define IQBISHOP 5
159 #define IKKNIGHT 6
160 #define IQKNIGHT 7
161 
162 #define IQRPAWN 8
163 #define IQNPAWN 9
164 #define IQBPAWN 10
165 #define IQPAWN 11
166 #define IKPAWN 12
167 #define IKBPAWN 13
168 #define IKNPAWN 14
169 #define IKRPAWN 15
170 
171 #define FPAWN 0
172 #define GPAWN 1
173 #define HPAWN 2
174 #define HGPAWN 3
175 #define GHPAWN 4
176 #define FGPAWN 5
177 #define APAWN 6
178 #define BPAWN 7
179 #define CPAWN 8
180 #define ABPAWN 9
181 #define BAPAWN 10
182 #define CBPAWN 11
183 
184 #define WHITE_MASK (0x0000FFFF)
185 #define BLACK_MASK (0xFFFF0000)
186 #define WKING_MASK (0x00000001)
187 #define BKING_MASK (0x00010000)
188 #define KING_MASK  (0x00010001)
189 #define WQUEEN_MASK (0x00000002)
190 #define BQUEEN_MASK (0x00020000)
191 #define QUEEN_MASK  (0x00020002)
192 #define WROOK_MASK (0x0000000C)
193 #define WKROOK_MASK (0x00000004)
194 #define WQROOK_MASK (0x00000008)
195 #define WBISHOP_MASK (0x00000030)
196 #define WKBISHOP_MASK (0x00000010)
197 #define WQBISHOP_MASK (0x00000020)
198 #define BBISHOP_MASK (0x00300000)
199 #define BKBISHOP_MASK (0x00100000)
200 #define BQBISHOP_MASK (0x00200000)
201 #define BISHOP_MASK   (0x00300030)
202 #define WKNIGHT_MASK (0x000000C0)
203 #define BKNIGHT_MASK (0x00C00000)
204 #define BROOK_MASK (0x000C0000)
205 #define BKROOK_MASK (0x00040000)
206 #define BQROOK_MASK (0x00080000)
207 #define WPAWN_MASK (0x0000FF00)
208 #define BPAWN_MASK (0xFF000000)
209 #define WPIECE_MASK (0x000000FF)
210 #define BPIECE_MASK (0x00FF0000)
211 
212 #define WMINOR_MASK (WKNIGHT_MASK | WBISHOP_MASK)
213 #define BMINOR_MASK (BKNIGHT_MASK | BBISHOP_MASK)
214 #define MINOR_MASK  (WMINOR_MASK | BMINOR_MASK)
215 #define WMAJOR_MASK (WPIECE_MASK & ~WMINOR_MASK)
216 #define BMAJOR_MASK (BPIECE_MASK & ~BMINOR_MASK)
217 #define MAJOR_MASK  (WMAJOR_MASK | BMAJOR_MASK)
218 
219 #ifdef INFINITY
220 #undef INFINITY
221 #endif
222 #define INFINITY (40.50*STATIC_PAWN_VALUE)
223 #define ILLEGAL (-40*STATIC_PAWN_VALUE)
224 #define WIN (-(ILLEGAL+1))
225 #define FORCED_WIN (WIN-127)
226 #define MAX_EVAL (FORCED_WIN-20)
227 #define MAX_MATERIAL (30.00*STATIC_PAWN_VALUE)
228 
229 #define STALEMATE 2
230 #define TIME_FORFEIT 3
231 #define UNKNOWN 4
232 
233 #define DEFAULT_MOVE_TIME 10
234 
235 #define POSN(x,y) ((y) + (x)*8)
236 #define XPOS(p) ((p)>>3)
237 #define YPOS(p) ((p)&7)
238 
239 #undef NORTH
240 #undef SOUTH
241 #undef EAST
242 #undef WEST
243 
244 #define NORTH (1)
245 #define SOUTH (-1)
246 #define EAST  (8)
247 #define WEST  (-8)
248 #define NORTH_EAST (NORTH+EAST)
249 #define NORTH_WEST (NORTH+WEST)
250 #define SOUTH_EAST (SOUTH+EAST)
251 #define SOUTH_WEST (SOUTH+WEST)
252 
253 #define WHITE_CASTLE_SHORT (1<<0)
254 #define WHITE_CASTLE_LONG (1<<1)
255 #define BLACK_CASTLE_SHORT (1<<2)
256 #define BLACK_CASTLE_LONG (1<<3)
257 #define WHITE_CASTLED (1<<4)
258 #define BLACK_CASTLED (1<<5)
259 #define FLAG_CHECK (1<<7)
260 #define FLAG_PREV_CHECK (1<<8)
261 #define FLAG_WHITE_PROMOTE (1<<11)
262 #define FLAG_BLACK_PROMOTE (1<<12)
263 #define FLAG_PROMOTE (FLAG_WHITE_PROMOTE | FLAG_BLACK_PROMOTE)
264 #define FLAG_ACCEPT_DRAW (1<<13)
265 #define FLAG_EVAL_DONE   (1<<14)
266 #define FLAG_WHITE_KINGSIDE  (1<<15)
267 #define FLAG_WHITE_QUEENSIDE  (1<<16)
268 #define FLAG_BLACK_KINGSIDE  (1<<17)
269 #define FLAG_BLACK_QUEENSIDE  (1<<18)
270 #define FLAG_NEED_PART2       (1<<20)
271 #define FLAG_FORCED_DRAW      (1<<21)
272 #define FLAG_BAD_EVAL         (1<<22)
273 #define FLAG_EGTB_EVAL        (1<<23)
274 #define FLAG_GAME_OVER        (1<<24)
275 #define FLAG_LOST             (1<<25)
276 #define FLAG_EXTENDED         (1<<26)
277 #define FLAG_COMPUTER_WHITE   (1<<27)
278 #define FLAG_DONE_TACTICS     (1<<28)
279 #define FLAG_FUTILE           (1<<29)
280 #define FLAG_BLUNDER          (1<<30)
281 #define FLAG_KQ_SIDE (FLAG_BLACK_QUEENSIDE | FLAG_BLACK_KINGSIDE | FLAG_WHITE_QUEENSIDE | FLAG_WHITE_KINGSIDE)
282 
283 #define FLAG_WHITE_CAN_CASTLE (WHITE_CASTLE_SHORT | WHITE_CASTLE_LONG)
284 #define FLAG_BLACK_CAN_CASTLE (BLACK_CASTLE_SHORT | BLACK_CASTLE_LONG)
285 #define FLAG_CAN_CASTLE (FLAG_WHITE_CAN_CASTLE | FLAG_BLACK_CAN_CASTLE)
286 
287 #define REPITITION_LENGTH 101
288 
289 #define mat_value(x) new_coefficients[IPIECE_VALUES + (x)]
290 
291 #if USE_STATIC_PAWN_VALUE
292 #define PAWN_VALUE mat_value(PAWN)
293 #endif
294 #define KNIGHT_VALUE mat_value(KNIGHT)
295 #define BISHOP_VALUE mat_value(BISHOP)
296 #define ROOK_VALUE mat_value(ROOK)
297 #define QUEEN_VALUE mat_value(QUEEN)
298 #define KING_VALUE mat_value(KING)
299 
300 #define INITIAL_MATERIAL (KING_VALUE + QUEEN_VALUE + 2*ROOK_VALUE + 2*BISHOP_VALUE + 2*KNIGHT_VALUE + 8*PAWN_VALUE)
301 
302 typedef short int16;
303 typedef unsigned short uint16;
304 typedef unsigned uint32;
305 typedef unsigned long long uint64;
306 
307 typedef enum {B_KING = -6,B_QUEEN = -5,B_ROOK = -4,B_BISHOP = -3,
308 	      B_KNIGHT = -2,B_PAWN = -1,
309 	      NONE=0,
310 	      PAWN=1, KNIGHT=2, BISHOP=3, ROOK=4, QUEEN=5, KING=6} PieceT;
311 
312 typedef enum {A1=0, A2, A3, A4, A5, A6, A7, A8,
313 	      B1, B2, B3, B4, B5, B6, B7, B8,
314 	      C1, C2, C3, C4, C5, C6, C7, C8,
315 	      D1, D2, D3, D4, D5, D6, D7, D8,
316 	      E1, E2, E3, E4, E5, E6, E7, E8,
317 	      F1, F2, F3, F4, F5, F6, F7, F8,
318 	      G1, G2, G3, G4, G5, G6, G7, G8,
319 	      H1, H2, H3, H4, H5, H6, H7, H8} SquareT;
320 
321 #ifndef DEBUG_FRIENDLY
322 #define DEBUG_FRIENDLY 1
323 #endif
324 #if DEBUG_FRIENDLY
325 typedef SquareT Square;
326 typedef PieceT Piece;
327 #else
328 typedef char Square;
329 typedef char Piece;
330 #endif
331 
332 typedef enum {OPENING=0, MIDDLE, ENDING, MATING} GameStage;
333 
334 typedef struct {
335 	Square from, to;
336 	int v;
337 } Move;
338 
339 typedef struct {
340 	Piece board[NUM_SQUARES];
341 	uint32 flags;
342 	int stage;
343 	short move_num;
344 	Square enpassent;
345 	unsigned char fifty_count;
346 } SmallPos;
347 
348 typedef struct {
349 	/* use a short to hold evaluations */
350 #if LARGE_ETYPE
351 	etype v;
352 #else
353 	int16 v;
354 #endif
355 #if STORE_LEAF_POS
356 	SmallPos pos;
357 #endif
358 } Eval;
359 
360 #if LEARN_EVAL
361 typedef struct {
362 	etype v;
363 	uint32 flags;
364 } LeafEval;
365 #endif
366 
367 
368 typedef struct {
369 	Piece p;
370 	Square pos;
371 } PieceStruct;
372 
373 struct hash_entry {
374 	uint32 hash1;
375 	uint32 hash2;
376 	Eval low;
377 	Eval high;
378 	unsigned char depth_low;
379 	unsigned char depth_high;
380 	unsigned from:6;
381 	unsigned to:6;
382 	unsigned tag:4;
383 	unsigned char move_num;
384 };
385 
386 typedef struct Position {
387 	/* this section needs to be copied in do_move */
388 	uint32 hash1, hash2;
389 	uint32 sliding_mask; /* mask of pieces that can "slide"
390 				  (bishops, rooks, queens) */
391 	uint32 pawns7th;     /* mask of pawns on the 7th rank */
392 	uint32 piece_mask;   /* mask of which pieces are not pawns */
393 	uint32 material_mask; /* mask of all pieces on the board */
394 	Piece board[NUM_SQUARES]; /* the board itself */
395 	char pboard[NUM_SQUARES]; /* pointers into the piece list */
396 	PieceStruct pieces[32];   /* the piece list - this is never
397 				     reordered as otherwise the masks
398 				     would become invalid */
399 	etype control[NUM_SQUARES]; /* the board control value for
400 				      each square */
401 	int cont[NUM_SQUARES]; /* who controls each square */
402 	char safe_mobility[32]; /* mobility value for each piece */
403 	char mobility[32]; /* mobility value for each piece */
404 	int white_moves;            /* whites total mobility */
405 	int black_moves;            /* blacks total mobility */
406 	etype piece_values[32];      /* the value of each piece at the last
407 				       eval call */
408 	int stage;             /* stage of the game */
409 	uint32 piece_change; /* a bitmap of which pieces need to be
410 				  recalculated */
411 	uint32 hung_mask;    /* a bitmap of pieces which are hung. This means
412 				  they are on a square that the opponent
413 				  controls */
414 	uint32 pinned_mask;    /* a bitmap of pieces which are pinned. */
415 	uint32 attacking_mask; /* a bitmap of pieces which are attacking
416 				    some enemy piece, and are thus being
417 				    useful */
418 	etype w_material;       /* whites material value */
419 	etype b_material;       /* blacks material value */
420 	etype board_control;   /* the previous board control value */
421 	etype expensive;       /* the expensive eval components from
422 				 the last eval */
423 	etype evaluation;      /* evaluation after a search */
424 	uint32 flight[NUM_SQUARES]; /* a bitmap for each square
425 				       which says what pieces can use
426 				       this square as a safe retreat */
427 	uint32 wpassed_pawn_mask; /* a mask of all the white passed pawns */
428 	uint32 bpassed_pawn_mask; /* a mask of all the black passed pawns */
429 	uint32 null_stoppable_pawn; /* a mask of the currently
430 				       unstoppable pawns that would
431 				       become stoppable if we null
432 				       move */
433 	uint32 null_unstoppable_pawn; /* a mask of the currently
434 				       stoppable pawns that would
435 				       become unstoppable if we null
436 				       move */
437 	uint64 white_holes;           /* holes for white and black on ranks 2-5 */
438 	uint64 black_holes;
439 
440 	/* and this section doesn't get copied by do_move */
441 	unsigned dont_copy; /* dummy variable */
442 	uint32 topieces[NUM_SQUARES]; /* a bitmap for each square
443 					   which says what pieces are
444 					   supporting/attacking the
445 					   square. This is the core of
446 					   the move generator and the
447 					   board control function */
448 #if USE_SLIDING_CONTROL
449 	uint32 xray_topieces[NUM_SQUARES]; /* a bitmap for each square
450 					   which says what pieces are
451 					   xray-supporting/attacking the
452 					   square. */
453 #endif
454 	Move *moves;
455 	int num_moves;
456 	Move last_move;
457 	Move best_capture;
458 	etype tactics;         /* the tactical eval component */
459 	uint32 flags;
460 	uint32 flags2;
461 	int enpassent;
462 	int promotion;
463 	int fifty_count;
464 	int move_num;
465 	unsigned winner;
466 	struct hash_entry h_entry;
467 	struct Position *oldb;
468 	etype eval_result;
469 } Position;
470 
471 struct state {
472 	Position position;
473 	int computer;
474 	int moved;
475 	float curquat[4];
476 	float scale;
477 	int quit;
478 	int stop_search;
479 	float move_time;
480 	float time_limit;
481 	int always_think;
482 	int use_mulling;
483 	int use_pbrain;
484 	int flip_view;
485 	int ics_robot;
486 	int autoplay;
487 	int hash_table_size;
488 	int need_reset;
489 	Move game_record[MAX_GAME_MOVES];
490 	Move converged_record[MAX_GAME_MOVES];
491 	uint32 hash_list[MAX_GAME_MOVES]; /* for repitition */
492 	int won;
493 	int colour;
494 	int rating_change;
495 	int demo_mode;
496 	int open;
497 	int display_position;
498 	int auto_exit;
499 	int brain_inserting;
500 	char white_player[100];
501 	char black_player[100];
502 	int moves_mulled;
503 	int analysed;
504 	int krating;
505 	int orating;
506 	int wtime;
507 	int btime;
508 	int game_time;
509 	int increment;
510 	int mulling;
511 	int current_depth;
512 	int last_book_move;
513 	int notified;
514 	int td_comp;
515 	int converged;
516 	int first_book_move;
517 	int first_book_move_index;
518 	int total_rounds;
519 	char notifyee[256];
520 	char game_type[20];
521 #if STORE_LEAF_POS
522 	LeafEval leaf_eval[MAX_GAME_MOVES];
523 	Position leaf_pos[MAX_GAME_MOVES];
524 	short predicted_move[MAX_GAME_MOVES];
525 	int stored_move_num;
526 #endif
527 };
528 
529 #define WHITEPIECES(b) ((b)->pieces)
530 #define BLACKPIECES(b) ((b)->pieces + 16)
531 #define PIECES(b,player) ((player)>0?WHITEPIECES(b):BLACKPIECES(b))
532 
533 
534 #include "proto.h"
535 #include "epdglue.h"
536 
537 #ifdef i386
ff_one(uint32 word)538 static inline int ff_one(uint32 word)
539 {
540 	__asm__("bsfl %1,%0"
541 		:"=r" (word)
542 		:"r" (word));
543 	return word;
544 }
545 
fl_one(uint32 word)546 static inline int fl_one(uint32 word)
547 {
548 	__asm__("bsrl %1,%0"
549 		:"=r" (word)
550 		:"r" (word));
551 	return word;
552 }
553 #else
ff_one(uint32 mask)554 static inline int ff_one(uint32 mask)
555 {
556 	static const int first[16] = {-1, 0, 1, 0, 2, 0, 1, 0, 3,
557 				      0, 1, 0, 2, 0, 1, 0};
558 	int ret=0;
559 	if (!(mask & 0xFFFF)) {
560 		mask >>= 16;
561 		ret += 16;
562 	}
563 
564 	if (!(mask & 0xFF)) {
565 		mask >>= 8;
566 		ret += 8;
567 	}
568 
569 	if (!(mask & 0xF)) {
570 		mask >>= 4;
571 		ret += 4;
572 	}
573 
574 	return ret + first[mask & 0xF];
575 }
576 
fl_one(uint32 mask)577 static inline int fl_one(uint32 mask)
578 {
579 	static const int last[16] = {-1, 0, 1, 1, 2, 2, 2, 2, 3,
580 				     3, 3, 3, 3, 3, 3, 3};
581 	int ret=0;
582 	if (mask & 0xFFFF0000) {
583 		mask >>= 16;
584 		ret += 16;
585 	}
586 
587 	if (mask & 0xFF00) {
588 		mask >>= 8;
589 		ret += 8;
590 	}
591 
592 	if (mask & 0xF0) {
593 		mask >>= 4;
594 		ret += 4;
595 	}
596 
597 	return ret + last[mask & 0xF];
598 }
599 #endif
600 
601 /* determine if two pieces or players are the same color */
same_color(Piece p1,Piece p2)602 static inline int same_color(Piece p1, Piece p2)
603 {
604 	return ((p1 ^ p2) >= 0);
605 }
606 
is_white(Piece p1)607 static inline int is_white(Piece p1)
608 {
609 	return p1 > 0;
610 }
611 
is_black(Piece p1)612 static inline int is_black(Piece p1)
613 {
614 	return p1 < 0;
615 }
616 
mirror_square(Square sq)617 static inline Square mirror_square(Square sq)
618 {
619 	return sq ^ 7;
620 }
621 
622 #define ABS(x) ((x)<0?-(x):(x))
623 
624 #define SIGN(x) ((x)<0?-1:1)
625 
626 #define RAMP(x) ((x)<0?(x):RAMP_FACTOR*(x))
627 
imin(int i1,int i2)628 static inline int imin(int i1, int i2)
629 {
630 	return i1 > i2? i2 : i1;
631 }
632 
imax(int i1,int i2)633 static inline int imax(int i1, int i2)
634 {
635 	return i1 > i2? i1 : i2;
636 }
637 
emin(etype i1,etype i2)638 static inline etype emin(etype i1, etype i2)
639 {
640 	return i1 > i2? i2 : i1;
641 }
642 
emax(etype i1,etype i2)643 static inline etype emax(etype i1, etype i2)
644 {
645 	return i1 > i2? i1 : i2;
646 }
647 
shiftr1(etype x)648 static inline etype shiftr1(etype x)
649 {
650 #if FLOAT_ETYPE
651 	return x*0.5;
652 #else
653 	return x >> 1;
654 #endif
655 }
656 
shiftr2(etype x)657 static inline etype shiftr2(etype x)
658 {
659 #if FLOAT_ETYPE
660 	return x*0.25;
661 #else
662 	return x >> 2;
663 #endif
664 }
665 
get_pboard(Position * b,Square pos)666 static inline PieceStruct *get_pboard(Position *b, Square pos)
667 {
668 	if (b->pboard[pos] == -1) return NULL;
669 	return b->pieces + b->pboard[pos];
670 }
671 
set_pboard(Position * b,Square pos,PieceStruct * piece)672 static inline void set_pboard(Position *b, Square pos, PieceStruct *piece)
673 {
674 	if (piece == NULL)
675 		b->pboard[pos] = -1;
676 	else
677 		b->pboard[pos] = piece - b->pieces;
678 }
679 
680 
is_zero_move(Move * move)681 static inline int is_zero_move(Move *move)
682 {
683 	return (move->from == A1 && move->to == A1);
684 }
685 
same_move(Move * m1,Move * m2)686 static inline int same_move(Move *m1, Move *m2)
687 {
688 	return (m1->from == m2->from && m1->to == m2->to);
689 }
690 
zero_move(Move * move)691 static inline void zero_move(Move *move)
692 {
693 	move->from = move->to = A1;
694 }
695 
bad_square(Square sq)696 static inline int bad_square(Square sq)
697 {
698 	return ((sq & ~63) != 0);
699 }
700 
white_square(Square sq)701 static inline int white_square(Square sq)
702 {
703 	return ((sq ^ (sq>>3)) & 1);
704 }
705 
black_square(Square sq)706 static inline int black_square(Square sq)
707 {
708 	return !white_square(sq);
709 }
710 
blacks_move(Position * b)711 static inline int blacks_move(Position *b)
712 {
713 	return (b->move_num & 1);
714 }
715 
whites_move(Position * b)716 static inline int whites_move(Position *b)
717 {
718 	return (!blacks_move(b));
719 }
720 
next_to_play(Position * b)721 static inline int next_to_play(Position *b)
722 {
723 	if (whites_move(b)) return 1;
724 	return -1;
725 }
726 
player_mask(Position * b)727 static inline uint32 player_mask(Position *b)
728 {
729 	if (whites_move(b)) return WHITE_MASK;
730 	return BLACK_MASK;
731 }
732 
flip(Eval v)733 static inline Eval flip(Eval v)
734 {
735 	v.v = -(v.v - 1);
736 	return v;
737 }
738 
seteval(Eval v,etype value)739 static inline Eval seteval(Eval v, etype value)
740 {
741 	v.v = value;
742 	return v;
743 }
744 
makeeval(Position * b,etype value)745 static inline Eval makeeval(Position *b, etype value)
746 {
747 	Eval v;
748 	v.v = value;
749 #if STORE_LEAF_POS
750 	memcpy(v.pos.board, b->board, sizeof(v.pos.board));
751 	v.pos.flags = b->flags;
752 	v.pos.move_num = b->move_num;
753 	v.pos.enpassent = b->enpassent;
754 	v.pos.fifty_count = b->fifty_count;
755 	v.pos.stage = b->stage;
756 #endif
757 	return v;
758 }
759 
bit_count(uint32 x)760 static inline int bit_count(uint32 x)
761 {
762 	int count;
763 	for (count=0; x; count++)
764 		x &= (x-1);
765 	return count;
766 }
767 
hash_ptr(uint32 hash1)768 static inline struct hash_entry *hash_ptr(uint32 hash1)
769 {
770 	extern struct hash_entry *hash_table;
771 	extern unsigned hash_table_size;
772 	uint32 hashindex = hash1 % hash_table_size;
773 
774 #if HASH_LEVELS
775 	hashindex  &= ~(HASH_LEVELS - 1);
776 #endif
777 
778 	return &hash_table[hashindex];
779 }
780 
invert(etype * p1,etype * p2)781 static inline void invert(etype *p1, etype *p2)
782 {
783 	Square i;
784 
785 	for (i=A1; i<=H8; i++)
786 		p1[i] = p2[mirror_square(i)];
787 }
788 
789 /* this determines if it is the computers move. This function is only
790    valid during a search as the flag is setup at the start of the search
791    code. This function is used to implement asymmetric search and eval
792    factors */
computers_move(Position * b)793 static inline int computers_move(Position *b)
794 {
795 	if (next_to_play(b) == 1)
796 		return ((b->flags & FLAG_COMPUTER_WHITE) != 0);
797 
798 	return ((b->flags & FLAG_COMPUTER_WHITE) == 0);
799 }
800 
801 extern char capture_map[2*KING+1][NUM_SQUARES][NUM_SQUARES];
802 extern char sliding_map[NUM_SQUARES][NUM_SQUARES];
803 extern char edge_square[NUM_SQUARES];
804 extern uint64 same_line_mask[NUM_SQUARES][NUM_SQUARES];
805 
806 /* this checks whether 3 squares are in a line. Note that the order is
807    important. same_line(A1, B2, C3) is true whereas same_line(B2, C3,
808    A1) isn't. The answer is also always false if two of the squares
809    are the same */
same_line(Square p1,Square p2,Square p3)810 static inline int same_line(Square p1, Square p2, Square p3)
811 {
812 	return sliding_map[p1][p2] &&
813 		sliding_map[p1][p2] == sliding_map[p2][p3];
814 }
815 
816 
817 /* given a direction of travel, will we move off the board by going
818    one square in that direction. This relies on the legal values that
819    a direction can take. */
off_board(Square sq,int dir)820 static inline int off_board(Square sq, int dir)
821 {
822 	dir = abs(edge_square[sq] + dir);
823 	return dir != 7 && dir & ~9;
824 }
825 
826 /* this gives the distance from A1 or H8, whichever
827    is closer */
corner_distance(Square sq)828 static inline int corner_distance(Square sq)
829 {
830 	static const char dist[64] = {
831 		0, 1, 2, 3, 4, 5, 6, 7,
832 		1, 1, 2, 3, 4, 5, 6, 6,
833 		2, 2, 2, 3, 4, 5, 5, 5,
834 		3, 3, 3, 3, 4, 4, 4, 4,
835 		4, 4, 4, 4, 3, 3, 3, 3,
836 		5, 5, 5, 4, 3, 2, 2, 2,
837 		6, 6, 5, 4, 3, 2, 1, 1,
838 		7, 6, 5, 4, 3, 2, 1, 0};
839 	return dist[sq];
840 }
841 
842 
843 struct hashvalue_entry {
844 	uint32 v1[2*KING+1][NUM_SQUARES];
845 	uint32 v2[2*KING+1][NUM_SQUARES];
846 };
847 
848 extern struct hashvalue_entry hash_values;
849 
add_hash(Position * b,Square pos,Piece p)850 static inline void add_hash(Position *b, Square pos, Piece p)
851 {
852 	b->hash1 ^= hash_values.v1[p+KING][pos];
853 	b->hash2 ^= hash_values.v2[p+KING][pos];
854 }
855 
remove_hash(Position * b,Square pos,Piece p)856 static inline void remove_hash(Position *b, Square pos, Piece p)
857 {
858 	b->hash1 ^= hash_values.v1[p+KING][pos];
859 	b->hash2 ^= hash_values.v2[p+KING][pos];
860 }
861 
distance(Square sq1,Square sq2)862 static inline int distance(Square sq1, Square sq2)
863 {
864 	int xdist = abs(XPOS(sq1) - XPOS(sq2));
865 	int ydist = abs(YPOS(sq1) - YPOS(sq2));
866 	return imax(xdist, ydist);
867 }
868 
869 extern int pop_count[256];
pop_count16(uint32 x)870 static inline int pop_count16(uint32 x)
871 {
872 	return pop_count[(x)&0xFF] + pop_count[((x)>>8)&0xFF];
873 }
874 
pop_count32(uint32 x)875 static inline int pop_count32(uint32 x)
876 {
877 	return pop_count16((x) & 0xFFFF) + pop_count16((x) >> 16);
878 }
879 
880 
881