Small fix in knight attacks king code
[owl.git] / zobrist.c
blobceeff49cb0d698e24b30469e7a26b103d4faae84
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 #include "common.h"
17 #include "board.h"
19 uint64_t hash_wck;
20 uint64_t hash_wcq;
21 uint64_t hash_bck;
22 uint64_t hash_bcq;
23 uint64_t hash_code[2][7][64];
24 uint64_t hash_ep[8];
25 uint64_t hash_side;
27 void
28 initialize_zobrist()
30 int p;
31 int color, piece, square;
33 for (color = WHITE; color <= BLACK; color++)
34 for (piece = PAWN; piece <= KING; piece++)
35 for (square = 0; square < 64; square++) {
36 p = piece * 2 - color - 1;
37 hash_code[color][piece][square] = \
38 p_hash_piece[64 * p + _RANK(square) * 8 + _FILE(square)];
41 for (square = 0; square < 8; square++)
42 hash_ep[square] = p_hash_ep[square];
44 hash_wck = p_hash_castle[0];
45 hash_wcq = p_hash_castle[1];
46 hash_bck = p_hash_castle[2];
47 hash_bcq = p_hash_castle[3];
48 hash_side = p_hash_side[0];
51 void
52 calc_zobrist(uint64_t *hash_key, uint64_t *hash_pawn_key)
54 int sq, piece, color;
55 uint64_t b;
57 *hash_key = *hash_pawn_key = 0ULL;
59 for (color = WHITE; color <= BLACK; color++) {
60 for (piece = PAWN; piece <= KING; piece++) {
61 b = piece_boards[color][piece];
62 while (b) {
63 sq = bit_scan(b);
64 CLEAR (b, sq);
65 *hash_key ^= hash_code[color][piece][sq];
66 if (piece == PAWN)
67 *hash_pawn_key ^= hash_code[color][piece][sq];
71 if (brd.castle_mask & WHITE_CASTLE_KINGSIDE)
72 *hash_key ^= hash_wck;
73 if (brd.castle_mask & WHITE_CASTLE_QUEENSIDE)
74 *hash_key ^= hash_wcq;
75 if (brd.castle_mask & BLACK_CASTLE_KINGSIDE)
76 *hash_key ^= hash_bck;
77 if (brd.castle_mask & BLACK_CASTLE_QUEENSIDE)
78 *hash_key ^= hash_bcq;
80 if (brd.ep != -1)
81 *hash_key ^= hash_ep[_FILE(brd.ep)];
82 if (brd.wtm == WHITE)
83 *hash_key ^= hash_side;