+ // Offsets to the PolyGlotRandoms[] array of zobrist keys
+ const Key* ZobPiece = PolyGlotRandoms;
+ const Key* ZobCastle = ZobPiece + 12 * 64; // Pieces * squares
+ const Key* ZobEnPassant = ZobCastle + 4; // Castle flags
+ const Key* ZobTurn = ZobEnPassant + 8; // Number of files
+
+ // book_key() returns the PolyGlot hash key of the given position
+ uint64_t book_key(const Position& pos) {
+
+ uint64_t key = 0;
+ Bitboard b = pos.pieces();
+
+ while (b)
+ {
+ // In PolyGlotRandoms[] pieces are stored in the following sequence:
+ // BP = 0, WP = 1, BN = 2, WN = 3, ... BK = 10, WK = 11
+ Square s = pop_lsb(&b);
+ Piece p = pos.piece_on(s);
+ int pieceOfs = 2 * (type_of(p) - 1) + (color_of(p) == WHITE);
+ key ^= ZobPiece[64 * pieceOfs + s];
+ }