Files
Eleanor/source/tt.cpp
T

76 lines
1.6 KiB
C++

#include "tt.h"
static inline U64 initialSeed = 0x60919C48E57863B9;
// 2[colors] * 6[pieces] * 64[squares]
static U64 zKeys[2][6][64];
// There are 8 files
static U64 zEnPassant[8];
static U64 zCastle[16];
static U64 zSide;
static U64 RandomNum() {
initialSeed ^= initialSeed << 13;
initialSeed ^= initialSeed >> 17;
initialSeed ^= initialSeed << 5;
return initialSeed;
}
void InitZobrist() {
// init piece square keys
for (int i = White; i <= Black; i++) {
for (int j = Pawn; j <= King; j++) {
for (int k = a1; k <= h8; k++) {
zKeys[i][j][k] = RandomNum();
}
}
}
// init en passant file keys
for (int i = 0; i < 8; i++) {
zEnPassant[i] = RandomNum();
}
// init castling rights keys
for (int i = 0; i < 16; i++) {
zCastle[i] = RandomNum();
}
// init side key
zSide = RandomNum();
}
U64 GetHashKey(Board &board) {
U64 key;
// XORing pieces
for (int color = White; color <= Black; color++) {
for (int piece = Pawn; piece <= King; piece++) {
Bitboard pieceSet = board.pieces[piece];
while (pieceSet) {
int square = pieceSet.getLS1BIndex();
key ^= zKeys[color][piece][square];
pieceSet.PopBit(square);
}
}
}
// XORing en passant
if (board.enPassantTarget != -1) {
key ^= zEnPassant[board.enPassantTarget % 8];
}
// XORing castling rights
key ^= zCastle[board.castlingRights];
// XORing side to move
if (board.sideToMove) key ^= zSide;
return key;
}