/* GNU Chess 5.0 - hash.c - hash function code Copyright (c) 1999-2002 Free Software Foundation, Inc. GNU Chess is based on the two research programs Cobalt by Chua Kong-Sian and Gazebo by Stuart Cracraft. GNU Chess is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Chess is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Chess; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Contact Info: bug-gnu-chess@gnu.org cracraft@ai.mit.edu, cracraft@stanfordalumni.org, cracraft@earthlink.net */ #include "common.h" void CalcHashKey (void) /*************************************************************************** * * Calculates the hashkey for the current board position. We sometimes * need to do this especially when loading an EPD position. * Note: The hashkey is a 64 bit unsigned integer number. * Added in pawnhashkey calculation. * ***************************************************************************/ { int sq, piece, color; BitBoard b; PawnHashKey = HashKey = (HashType) 0; for (color = white; color <= black; color++) { for (piece = pawn; piece <= king; piece++) { b = board.b[color][piece]; while (b) { BitBoard h; sq = leadz (b); CLEARBIT (b, sq); h=hashcode[color][piece][sq]; HashKey ^= h; if (piece == pawn) PawnHashKey ^= h; } } } /* Take into account castling status & en passant */ if (board.ep > -1) HashKey ^= ephash[board.ep]; if (board.flag & WKINGCASTLE) HashKey ^= WKCastlehash; if (board.flag & WQUEENCASTLE) HashKey ^= WQCastlehash; if (board.flag & BKINGCASTLE) HashKey ^= BKCastlehash; if (board.flag & BQUEENCASTLE) HashKey ^= BQCastlehash; /* Take into account side to move */ if (board.side == black) HashKey ^= Sidehash; }