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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
/**
Copyright (c) Scott Gasch
Module Name:
sig.c
Abstract:
Position signature code... the algorithm is a Zobrist hash scheme.
Every piece has a different 64-bit "seed" for every different
square. Plus some non-piece/location features of a position (such
as legal castling rights or the en passant location) have 64-bit
"seed" keys. The position's signature is the xor sum of all these
seeds. A position's pawn hash signature is the xor sum of all
pawn-related seeds. These signatures, once computed, can be
incrementally maintained by just xoring off the seed of a moving
piece's from location and xoring on the seed of its to location.
These signatures are used as the basis for the pawn hash and main
transposition table code.
Author:
Scott Gasch ([email protected]) 09 May 2004
Revision History:
$Id: sig.c 345 2007-12-02 22:56:42Z scott $
**/
#include "chess.h"
UINT64 g_u64SigSeeds[128][7][2];
UINT64 g_u64PawnSigSeeds[128][2];
UINT64 g_u64CastleSigSeeds[16];
UINT64 g_u64EpSigSeeds[9];
void
InitializeSigSystem(void)
/**
Routine description:
Populate the arrays of signature keys (see above) used to generate
normal and pawn signatures for POSITIONs.
Parameters:
void
Return value:
void
**/
{
ULONG x, y, z;
UINT uChecksum = 0;
ULONG uRandom;
memset(g_u64SigSeeds, 0, sizeof(g_u64SigSeeds));
memset(g_u64PawnSigSeeds, 0, sizeof(g_u64PawnSigSeeds));
memset(g_u64CastleSigSeeds, 0, sizeof(g_u64CastleSigSeeds));
memset(g_u64EpSigSeeds, 0, sizeof(g_u64EpSigSeeds));
seedMT(2222);
for (x = A; x <= H+1; x++)
{
//
// Do en-passant sig
//
uRandom = randomMT();
uChecksum ^= uRandom;
g_u64EpSigSeeds[x] = (UINT64)uRandom << 32;
uRandom = randomMT();
uChecksum ^= uRandom;
g_u64EpSigSeeds[x] |= uRandom;
g_u64EpSigSeeds[x] &= ~1;
}
//
// Foreach square on the board.
//
for (x = 0; x < 128; x++)
{
//
// For both colors
//
FOREACH_COLOR(z)
{
//
// Do the pawn sigs
//
uRandom = randomMT();
uChecksum ^= uRandom;
g_u64PawnSigSeeds[x][z] = (UINT64)uRandom << 32;
uRandom = randomMT();
uChecksum ^= uRandom;
g_u64PawnSigSeeds[x][z] |= uRandom;
g_u64PawnSigSeeds[x][z] &= ~1;
//
// Do the piece sigs
//
for (y = 0; y < 7; y++)
{
uRandom = randomMT();
uChecksum ^= uRandom;
g_u64SigSeeds[x][y][z] = (UINT64)uRandom << 32;
uRandom = randomMT();
uChecksum ^= uRandom;
g_u64SigSeeds[x][y][z] |= uRandom;
g_u64SigSeeds[x][y][z] &= ~1;
}
}
}
//
// For all castle permissions
//
for (x = 0; x < 16; x++)
{
uRandom = randomMT();
uChecksum ^= uRandom;
g_u64CastleSigSeeds[x] = (UINT64)uRandom << 32;
uRandom = randomMT();
uChecksum ^= uRandom;
g_u64CastleSigSeeds[x] |= uRandom;
g_u64CastleSigSeeds[x] &= ~1;
}
//
// Make sure we got what we expected.
//
if (uChecksum != 0xac19ab2b)
{
UtilPanic(DETECTED_INCORRECT_INITIALIZATION,
NULL,
"signature system",
(void *)uChecksum,
(void *)0xac19ab2b,
__FILE__, __LINE__);
}
}
UINT64
ComputePawnSig(POSITION *pos)
/**
Routine description:
Given a board, recompute the pawn signature from scratch.
Parameters:
POSITION *pos
Return value:
UINT64
**/
{
register COOR c;
PIECE p;
UINT64 u64Sum = 0;
FOREACH_SQUARE(c)
{
if (!IS_ON_BOARD(c)) continue;
p = pos->rgSquare[c].pPiece;
if (IS_PAWN(p))
{
u64Sum ^= g_u64PawnSigSeeds[c][GET_COLOR(p)];
}
}
return(u64Sum);
}
UINT64
ComputeSig(POSITION *pos)
/**
Routine description:
Given a board, recompute the main signature from scratch.
Parameters:
POSITION *pos
Return value:
UINT64
**/
{
register COOR c;
UINT64 u64Sum = 0;
PIECE p;
FOREACH_SQUARE(c)
{
if (!IS_ON_BOARD(c)) continue;
p = pos->rgSquare[c].pPiece;
if (!IS_EMPTY(p) && !IS_PAWN(p))
{
ASSERT(IS_VALID_PIECE(p));
u64Sum ^= g_u64SigSeeds[c][PIECE_TYPE(p)][GET_COLOR(p)];
}
}
//
// Update the signature to reflect the castle permissions and ep
// square at this point.
//
u64Sum ^= g_u64CastleSigSeeds[pos->bvCastleInfo];
u64Sum ^= g_u64EpSigSeeds[FILE(pos->cEpSquare)];
//
// Update the low order bit to reflect the side to move.
//
if (pos->uToMove == WHITE)
{
u64Sum |= 1;
}
else
{
u64Sum &= ~1;
}
ASSERT((u64Sum & 1) == pos->uToMove);
return(u64Sum);
}
|