- read(buf, size);
-
- // Numbers are stored on disk as a binary byte stream
- for (int i = 0; i < size; i++)
- n = (n << 8) + (unsigned char)buf[i];
-
- return n;
-}
-
-
-////
-//// Local definitions
-////
-
-namespace {
-
- 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 ^= book_piece_key(pos.piece_on(s), s);
- }
-
- result ^= book_castle_key(pos);
- result ^= book_ep_key(pos);
- result ^= book_color_key(pos);
- return result;
- }
-
-
- uint64_t book_piece_key(Piece p, Square s) {
-
- // Convert pieces to the range 0..11
- static const int PieceTo12[] = { 0, 0, 2, 4, 6, 8, 10, 0, 0, 1, 3, 5, 7, 9, 11 };
-
- return Random64[PieceIdx + (PieceTo12[int(p)]^1) * 64 + int(s)];
- }
-
-
- uint64_t book_castle_key(const Position& pos) {
-
- uint64_t result = 0;
-
- if (pos.can_castle_kingside(WHITE))
- result ^= Random64[CastleIdx + 0];
-
- if (pos.can_castle_queenside(WHITE))
- result ^= Random64[CastleIdx + 1];
-
- if (pos.can_castle_kingside(BLACK))
- result ^= Random64[CastleIdx + 2];
-
- if (pos.can_castle_queenside(BLACK))
- result ^= Random64[CastleIdx + 3];
-
- return result;
- }
-
-
- uint64_t book_ep_key(const Position& pos) {
-
- return pos.ep_square() == SQ_NONE ? 0 : Random64[EnPassantIdx + square_file(pos.ep_square())];
- }
-
-
- uint64_t book_color_key(const Position& pos) {
-
- return pos.side_to_move() == WHITE ? Random64[TurnIdx] : 0;
- }