/
hash.cpp
115 lines (98 loc) · 1.75 KB
/
hash.cpp
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
#include "globals.h"
BITBOARD hash[4][32];
BITBOARD lock[4][32];
BITBOARD currentkey,currentlock;
BITBOARD collisions;
BITBOARD bit_units[2];
BITBOARD mask[64];
const BITBOARD MAXHASH = 5000000;
const BITBOARD HASHSIZE = 5000000;
int hash_start,hash_dest;
struct hashp
{
BITBOARD hashlock;
int start;
int dest;
int num;
};
hashp *hashpos[2];
int Random(const int x);
void RandomizeHash()
{
int p,x;
for(p=0;p<4;p++)
for(x=0;x<32;x++)
{
hash[p][x] = Random(HASHSIZE);
hash[p][x] = Random(HASHSIZE);
lock[p][x] = Random(HASHSIZE);
lock[p][x] = Random(HASHSIZE);
}
hashpos[0] = new hashp[MAXHASH];
hashpos[1] = new hashp[MAXHASH];
}
int Random(const int x)
{
return rand() % x;
}
void Free()
{
delete hashpos[0];
delete hashpos[1];
}
void AddHash(const int s, const move m)
{
hashp* ptr = &hashpos[s][currentkey];
ptr->hashlock = currentlock;
ptr->start = m.from;
ptr->dest = m.to;
}
void AddKey(const int s,const int p,const int x)
{
currentkey ^= hash[p][x];
currentlock ^= lock[p][x];
}
BITBOARD GetLock()
{
BITBOARD loc=0;
int c;
for(int x=0;x<32;x++)
{
if(board[x]!=0)
{
if(board[x]==BM) c = 1;
if(board[x]==BK) c = 2;
if(board[x]==RM) c = 3;
if(board[x]==RK) c = 4;
loc ^= lock[c][x];
}
}
return loc;
}
BITBOARD GetKey()
{
BITBOARD key=0;
int c;
for(int x=0;x<32;x++)
{
if(board[x]!=0)
{
if(board[x]==BM) c = 1;
if(board[x]==BK) c = 2;
if(board[x]==RM) c = 3;
if(board[x]==RK) c = 4;
key ^= hash[c][x];
}
}
return key;
}
bool LookUp(const int s)
{
if(hashpos[s][currentkey].hashlock != currentlock)
{
return false;
}
hash_start = hashpos[s][currentkey].start;
hash_dest = hashpos[s][currentkey].dest;
return true;
}