1 /* move ordering code */
2 #include "includes.h"
3 #include "knightcap.h"
4 
5 #define NUM_CUTOFFS 1
6 
7 static unsigned history[NUM_SQUARES][NUM_SQUARES];
8 static Move refutation[NUM_SQUARES][NUM_SQUARES];
9 
10 struct {
11 	Move k1, k2;
12 } killers[MAX_DEPTH+10];
13 
order_reset(void)14 void order_reset(void)
15 {
16 	memset(history, 0, sizeof(history));
17 	memset(killers, 0, sizeof(killers));
18 	memset(refutation, 0, sizeof(refutation));
19 	lprintf(0,"reset ordering info\n");
20 }
21 
order_clear(int move_num)22 void order_clear(int move_num)
23 {
24 	static int last_num;
25 	int x,y, delta;
26 
27 	delta = imax(move_num - last_num, 3);
28 	last_num = move_num;
29 
30 	memset(killers, 0, sizeof(killers));
31 	memset(refutation, 0, sizeof(refutation));
32 
33 	if (delta <= 0) return;
34 
35 	for (x=A1;x<=H8;x++)
36 		for (y=A1;y<=H8;y++)
37 			history[x][y] >>= delta;
38 }
39 
40 
cutoff_hint(Position * b,int m,int depth,int ply)41 void cutoff_hint(Position *b, int m, int depth, int ply)
42 {
43 	Move *move = &b->moves[m];
44 	history[move->from][move->to] += (1 << depth);
45 
46 	refutation[b->last_move.from][b->last_move.to] = (*move);
47 
48 	if (ply < MAX_DEPTH) {
49 		if (!same_move(move, &killers[ply].k1) &&
50 		    !same_move(move, &killers[ply].k2)) {
51 			if (b->board[move->to]) {
52 				killers[ply].k1 = (*move);
53 			} else {
54 				killers[ply].k2 = (*move);
55 			}
56 		}
57 	}
58 }
59 
60 
61 static Move hash_move;
62 
order_fn(Position * b,Move * m,int ply,Eval testv)63 static int order_fn(Position *b, Move *m, int ply, Eval testv)
64 {
65 	int ret = 0;
66 
67 	if (same_move(m, &hash_move)) {
68 		return 100000000;
69 	}
70 
71 	if (ply < MAX_DEPTH &&
72 	    (same_move(m, &killers[ply].k1) ||
73 	     same_move(m, &killers[ply].k2))) {
74 		ret += 10000;
75 	}
76 
77 	if (same_move(m, &b->best_capture)) {
78 		ret += 15000;
79 	}
80 
81 	if (same_move(m, &refutation[b->last_move.from][b->last_move.to])) {
82 		ret += 10000;
83 	}
84 
85 	ret += hash_ordering(b, m, testv) * 10000;
86 
87 	return ret + history[m->from][m->to];
88 }
89 
order_moves(Position * b,Move * moves,int n,int ply,Move * m1,Eval testv)90 void order_moves(Position *b, Move *moves, int n, int ply, Move *m1,Eval testv)
91 {
92 	int m;
93 
94 	if (m1)
95 		hash_move = (*m1);
96 	else {
97 #if USE_EVAL_TACTICS
98 		eval_tactics(b);
99 #endif
100 		zero_move(&hash_move);
101 	}
102 
103 	for (m=0;m<n;m++) {
104 		moves[m].v = order_fn(b, &moves[m], ply, testv);
105 	}
106 
107 
108 	sort_moves(moves, n);
109 }
110 
111 
112