- // Indices to the Random64[] array
- const int PieceIdx = 0;
- const int CastleIdx = 768;
- const int EnPassantIdx = 772;
- const int TurnIdx = 780;
-
- // Local functions
- uint64_t book_key(const Position& pos);
- uint64_t book_piece_key(Piece p, Square s);
- uint64_t book_castle_key(const Position& pos);
- uint64_t book_ep_key(const Position& pos);
- uint64_t book_color_key(const Position& pos);
-}
+ // Offsets to the PolyGlotRandoms[] array of zobrist keys
+ const Key* ZobPiece = PolyGlotRandoms + 0;
+ const Key* ZobCastle = PolyGlotRandoms + 768;
+ const Key* ZobEnPassant = PolyGlotRandoms + 772;
+ const Key* ZobTurn = PolyGlotRandoms + 780;
+
+ // Piece offset is calculated as 64 * (PolyPiece ^ 1) where
+ // PolyPiece is: BP = 0, WP = 1, BN = 2, WN = 3 ... BK = 10, WK = 11
+ const int PieceOfs[] = { 0, 64, 192, 320, 448, 576, 704, 0,
+ 0, 0, 128, 256, 384, 512, 640 };
+
+ // book_key() builds up a PolyGlot hash key out of a position
+ uint64_t book_key(const Position& pos) {
+
+ uint64_t result = 0;
+ Bitboard b = pos.occupied_squares();
+
+ while (b)
+ {
+ Square s = pop_1st_bit(&b);
+ result ^= ZobPiece[PieceOfs[pos.piece_on(s)] + s];
+ }
+
+ if (pos.can_castle(WHITE_OO))
+ result ^= ZobCastle[0];
+
+ if (pos.can_castle(WHITE_OOO))
+ result ^= ZobCastle[1];
+
+ if (pos.can_castle(BLACK_OO))
+ result ^= ZobCastle[2];
+
+ if (pos.can_castle(BLACK_OOO))
+ result ^= ZobCastle[3];
+
+ if (pos.ep_square() != SQ_NONE)
+ result ^= ZobEnPassant[square_file(pos.ep_square())];
+
+ if (pos.side_to_move() == WHITE)
+ result ^= ZobTurn[0];