-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.cpp
47 lines (37 loc) · 1.11 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
#include <tuple>
#include <random>
#include "bitboard.h"
#include "hash.h"
// table #, square, piece
ull zhPieces[64][12];
// table #, castle data (0b11 = kingside and queenside rights, etc) color
ull zhCastle[4][2];
void hash::init() {
std::random_device rd;
std::mt19937_64 random(rd());
std::uniform_int_distribution<ull> distribution;
for (int j = 0; j < 64; j++) {
for (int k = 0; k < 12; k++)
zhPieces[j][k] = distribution(random);
}
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 2; k++)
zhCastle[j][k] = distribution(random);
}
}
ull hash::get_hash(const bitboard::Position &board) {
ull hash = 0;
ull pieces = board.allPieces;
while (pieces) {
int pos = __builtin_ctzll(pieces);
int piece = board.mailbox[pos];
if (piece != -1)
hash ^= zhPieces[pos][piece];
SET0(pieces, pos);
}
ull whiteCastle = (board.castle[WHITE_SHORT] << 1) | board.castle[WHITE_LONG];
ull blackCastle = (board.castle[BLACK_SHORT] << 1) | board.castle[BLACK_LONG];
hash ^= zhCastle[whiteCastle][WHITE];
hash ^= zhCastle[blackCastle][BLACK];
return hash;
}