-namespace {
-
- uint64_t book_key(const Position &pos) {
- uint64_t result = 0ULL;
-
- for(Color c = WHITE; c <= BLACK; c++) {
- Bitboard b = pos.pieces_of_color(c);
- Square s;
- Piece p;
- while(b != EmptyBoardBB) {
- s = pop_1st_bit(&b);
- p = pos.piece_on(s);
- assert(piece_is_ok(p));
- assert(color_of_piece(p) == c);
-
- result ^= book_piece_key(p, s);
- }
- }
+ if (!move)
+ return MOVE_NONE;
+
+ // A PolyGlot book move is encoded as follows:
+ //
+ // bit 0- 5: destination square (from 0 to 63)
+ // bit 6-11: origin square (from 0 to 63)
+ // bit 12-14: promotion piece (from KNIGHT == 1 to QUEEN == 4)
+ //
+ // Castling moves follow "king captures rook" representation. So in case book
+ // move is a promotion we have to convert to our representation, in all the
+ // other cases we can directly compare with a Move after having masked out
+ // the special Move's flags (bit 14-15) that are not supported by PolyGlot.
+ int pt = (move >> 12) & 7;
+ if (pt)
+ move = make<PROMOTION>(from_sq(move), to_sq(move), PieceType(pt + 1));
+
+ // Add 'special move' flags and verify it is legal
+ for (MoveList<LEGAL> it(pos); *it; ++it)
+ if (move == (*it ^ type_of(*it)))
+ return *it;