Table of Contents

Chess - Programming - Pawn Hash Table

A Pawn Hash Table is often used inside a chess programs to cache purely pawn related pattern and evaluation stuff.

While the content of the pawn hash table entry varies a lot between different chess programs and their implementation, it is not uncommon that one entry exceeds 1/2 K-Byte.


Pawn Hash Index

Most simple, common and recommended is to keep an incremental updated, dedicated Zobrist or BCH keys similar to the main transposition table, initialized from all squares occupied by pawns only, side to move does not matter.

Alternatively, considering hits from the transposition table, or a dedicated evaluation hash table, one may use a fast hash function to compute an index from the white and black pawn bitboards on the fly, i.e. either a modulo or a multiplication and shift a la magic bitboards by the difference of the disjoint pawn sets.

A sharper bound on the number of Pawn constellations than 3^48 (which also counts cases like having 48 black Pawns) is 2^64.

This can be further reduced by mirroring the board, considering symmetric positions or detecting illegal positions.


Potential Pawn Hash Table

mod(49981), which is an appropriate size for a pawn hash table:

unsigned int mod49981 (unsigned __int64 a)
{
  // (2**64 + 49981 - 1) / 49981
  // is not exact divisible without remainder
  // 369075130023601.0003001140433 ~ 0x14FAC00053EB1
  unsigned __int64 m = (0x00014FAC00053EB1 * (a >> 32)
                      + 0x00014FAC * (a & 0xffffffff));
  int modulus = (int)(a - (__int64)(m>>32)* 49981);
  if ( modulus < 0 )  // correct negative results
    modulus += 49981; // due to the remainder of 0.0003
  return (unsigned int) modulus;
}

Information to store include:

Without king positions in pseudo-C

hash_t RandomIndex[64][2];  // Initialize by Pseudo random numbers.

  PawnHashIndex = 0;
  for (pos = A1; pos <= H8; pos++)
    if (figure[pos] == pawn)
      PawnHashIndex ^= RandomIndex[pos][color[pos]];

Probably do not do this after every move.

If you move a pawn , you can update the PawnHashIndex by

// Update the pawn hash for "normal" pawn move.
PawnHashIndex ^= RandomIndex[from][side]; // Clear the old pawn position.
PawnHashIndex ^= RandomIndex[to][side];   // Put in the new pawn position.

Similar code is needed for captures of a pawn and pawn promotions.