-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhash.c
128 lines (76 loc) · 1.91 KB
/
hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// hash.c
// includes
#include "board.h"
#include "hash.h"
#include "piece.h"
#include "random.h"
#include "square.h"
#include "util.h"
// variables
static uint64 Castle64[16];
// prototypes
static uint64 hash_castle_key_debug (int flags);
// functions
// hash_init()
void hash_init() {
int i;
for (i = 0; i < 16; i++) Castle64[i] = hash_castle_key_debug(i);
}
// hash_key()
uint64 hash_key(const board_t * board) {
uint64 key;
int colour;
const uint8 * ptr;
int sq, piece;
ASSERT(board_is_ok(board));
// init
key = 0;
// pieces
for (colour = 1; colour <= 2; colour++) { // HACK
for (ptr = board->list[colour]; (sq=*ptr) != SquareNone; ptr++) {
piece = board->square[sq];
key ^= hash_piece_key(piece,sq);
}
}
// castle flags
key ^= hash_castle_key(board_flags(board));
// en-passant square
sq = board->ep_square;
if (sq != SquareNone) key ^= hash_ep_key(sq);
// turn
key ^= hash_turn_key(board->turn);
return key;
}
// hash_piece_key()
uint64 hash_piece_key(int piece, int square) {
ASSERT(piece_is_ok(piece));
ASSERT(square_is_ok(square));
return random_64(RandomPiece+piece_to_12(piece)*64+square_to_64(square));
}
// hash_castle_key()
uint64 hash_castle_key(int flags) {
ASSERT((flags&~0xF)==0);
return Castle64[flags];
}
// hash_castle_key_debug()
static uint64 hash_castle_key_debug(int flags) {
uint64 key;
int i;
ASSERT((flags&~0xF)==0);
key = 0;
for (i = 0; i < 4; i++) {
if ((flags & (1<<i)) != 0) key ^= random_64(RandomCastle+i);
}
return key;
}
// hash_ep_key()
uint64 hash_ep_key(int square) {
ASSERT(square_is_ok(square));
return random_64(RandomEnPassant+square_file(square));
}
// hash_turn_key()
uint64 hash_turn_key(int colour) {
ASSERT(colour_is_ok(colour));
return (colour_is_white(colour)) ? random_64(RandomTurn) : 0;
}
// end of hash.cpp