libscid snapshot+1833ab5ad39d
Chess applications made easy.
Source on GitHub License GPL v2
Loading...
Searching...
No Matches
matsig.h File Reference

Material and home-pawn signatures used by database search prefilters. More...

#include "scid/core/board.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <string>
#include <utility>
+ Include dependency graph for matsig.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

namespace  scid
 Public namespace for libscid.
 
namespace  scid::database
 Database sessions, metadata, names, filters, trees and storage.
 

Macros

#define MATSIG_FlipColor(x)   ((x) >> 12) | (((x) & 0x00000FFF) << 12)
 Returns x with White and Black material fields exchanged.
 
#define MATSIG_Has_WQ(x)   ((x) & MASK_WQ)
 Returns non-zero when x contains at least one White queen.
 
#define MATSIG_Has_BQ(x)   ((x) & MASK_BQ)
 Returns non-zero when x contains at least one Black queen.
 
#define MATSIG_Has_WR(x)   ((x) & MASK_WR)
 Returns non-zero when x contains at least one White rook.
 
#define MATSIG_Has_BR(x)   ((x) & MASK_BR)
 Returns non-zero when x contains at least one Black rook.
 
#define MATSIG_Has_WB(x)   ((x) & MASK_WB)
 Returns non-zero when x contains at least one White bishop.
 
#define MATSIG_Has_BB(x)   ((x) & MASK_BB)
 Returns non-zero when x contains at least one Black bishop.
 
#define MATSIG_Has_WN(x)   ((x) & MASK_WN)
 Returns non-zero when x contains at least one White knight.
 
#define MATSIG_Has_BN(x)   ((x) & MASK_BN)
 Returns non-zero when x contains at least one Black knight.
 
#define MATSIG_Has_WP(x)   ((x) & MASK_WP)
 Returns non-zero when x contains at least one White pawn.
 
#define MATSIG_Has_BP(x)   ((x) & MASK_BP)
 Returns non-zero when x contains at least one Black pawn.
 
#define MATSIG_HasQueens(x)   ((x) & (MASK_WQ | MASK_BQ))
 Returns non-zero when either side has a queen.
 
#define MATSIG_HasRooks(x)   ((x) & (MASK_WR | MASK_BR))
 Returns non-zero when either side has a rook.
 
#define MATSIG_HasBishops(x)   ((x) & (MASK_WB | MASK_BB))
 Returns non-zero when either side has a bishop.
 
#define MATSIG_HasKnights(x)   ((x) & (MASK_WN | MASK_BN))
 Returns non-zero when either side has a knight.
 
#define MATSIG_HasPawns(x)   ((x) & (MASK_WP | MASK_BP))
 Returns non-zero when either side has a pawn.
 
#define MATSIG_Count_WQ(x)   (((x) & MASK_WQ) >> SHIFT_WQ)
 Returns the White queen count from x.
 
#define MATSIG_Count_BQ(x)   (((x) & MASK_BQ) >> SHIFT_BQ)
 Returns the Black queen count from x.
 
#define MATSIG_Count_WR(x)   (((x) & MASK_WR) >> SHIFT_WR)
 Returns the White rook count from x.
 
#define MATSIG_Count_BR(x)   (((x) & MASK_BR) >> SHIFT_BR)
 Returns the Black rook count from x.
 
#define MATSIG_Count_WB(x)   (((x) & MASK_WB) >> SHIFT_WB)
 Returns the White bishop count from x.
 
#define MATSIG_Count_BB(x)   (((x) & MASK_BB) >> SHIFT_BB)
 Returns the Black bishop count from x.
 
#define MATSIG_Count_WN(x)   (((x) & MASK_WN) >> SHIFT_WN)
 Returns the White knight count from x.
 
#define MATSIG_Count_BN(x)   (((x) & MASK_BN) >> SHIFT_BN)
 Returns the Black knight count from x.
 
#define MATSIG_Count_WP(x)   (((x) & MASK_WP) >> SHIFT_WP)
 Returns the White pawn count from x.
 
#define MATSIG_Count_BP(x)   (((x) & MASK_BP) >> SHIFT_BP)
 Returns the Black pawn count from x.
 
Material Signature Layout

From high to low bits, the layout is:

  • bits 22-23: White queens
  • bits 20-21: White rooks
  • bits 18-19: White bishops
  • bits 16-17: White knights
  • bits 12-15: White pawns
  • bits 10-11: Black queens
  • bits 08-09: Black rooks
  • bits 06-07: Black bishops
  • bits 04-05: Black knights
  • bits 00-03: Black pawns

Pawns can represent ordinary counts from zero to eight. Other piece counts are saturated at three when signatures are made from a position.

#define SHIFT_BP   0
 
#define SHIFT_BN   4
 
#define SHIFT_BB   6
 
#define SHIFT_BR   8
 
#define SHIFT_BQ   10
 
#define SHIFT_WP   12
 
#define SHIFT_WN   16
 
#define SHIFT_WB   18
 
#define SHIFT_WR   20
 
#define SHIFT_WQ   22
 
#define MASK_BP   0x0000000F
 Mask for black pawns.
 
#define MASK_BN   0x00000030
 Mask for black knights.
 
#define MASK_BB   0x000000C0
 Mask for black bishops.
 
#define MASK_BR   0x00000300
 Mask for black rooks.
 
#define MASK_BQ   0x00000C00
 Mask for black queens.
 
#define MASK_WP   0x0000F000
 Mask for white pawns.
 
#define MASK_WN   0x00030000
 Mask for white knights.
 
#define MASK_WB   0x000C0000
 Mask for white bishops.
 
#define MASK_WR   0x00300000
 Mask for white rooks.
 
#define MASK_WQ   0x00C00000
 Mask for white queens.
 

Typedefs

typedef std::uint32_t scid::database::matSigT
 Compact material signature used for fast database search prefilters.
 

Functions

scid::core::uint scid::database::matsig_getCount (matSigT m, scid::core::pieceT p)
 Returns the count of piece p stored in material signature m.
 
matSigT scid::database::matsig_setCount (matSigT m, scid::core::pieceT p, scid::core::uint count)
 Returns m with the count for piece p replaced by count.
 
std::string scid::database::matsig_makeString (matSigT matsig)
 Returns a compact textual representation of matsig.
 
bool scid::database::matsig_isReachable (matSigT mStart, matSigT mTarget, bool promos, bool upromo)
 Returns true when mStart could legally decay into mTarget.
 
bool scid::database::matsig_isReachablePawns (matSigT mStart, matSigT mTarget)
 Returns true when mStart could decay into mTarget considering pawns only.
 
matSigT scid::database::matsig_Make (const scid::core::byte *materialCounts)
 Builds a material signature from a scid::core::Position material array.
 
bool scid::database::hpSig_PossibleMatch (scid::core::uint hpSig, const scid::core::byte *changeList)
 Returns true when a game's home-pawn change list could reach hpSig.
 
bool scid::database::hpSig_Prefix (const scid::core::byte *changeListA, const scid::core::byte *changeListB)
 Returns true when either home-pawn change list is a prefix of the other.
 
scid::core::uint scid::database::hpSig_Final (const scid::core::byte *changeList)
 Returns the final home-pawn signature reached after applying changeList.
 
scid::core::uint scid::database::hpSig_AddPawn (scid::core::uint hpSig, scid::core::colorT color, scid::core::fyleT fyle)
 Returns hpSig with the home-pawn bit for color and fyle set.
 
scid::core::uint scid::database::hpSig_ClearPawn (scid::core::uint hpSig, scid::core::colorT color, scid::core::fyleT fyle)
 Returns hpSig with the home-pawn bit for color and fyle cleared.
 
std::pair< std::uint16_t, std::uint16_t > scid::database::hpSig_make (const scid::core::pieceT *board)
 Creates a 16-bit bitmap of the pawns missing from their home ranks.
 
bool scid::database::hpSig_match (int hpSig, int nMoved, const scid::core::byte *changeList)
 Returns true when changeList can explain the requested missing-pawn bitmap and moved-pawn count.
 

Variables

const matSigT scid::database::MASK_BY_PIECE [16]
 Piece-code indexed masks for extracting or replacing a count.
 
const scid::core::uint scid::database::SHIFT_BY_PIECE [16]
 Piece-code indexed bit shifts for extracting or replacing a count.
 
const matSigT scid::database::MATSIG_Empty = 0
 Material signature for an empty board.
 
const matSigT scid::database::MATSIG_StdStart
 Material signature for the standard chess starting position.
 
const scid::core::uint scid::database::HPSIG_Empty = 0x0
 Home-pawn signature with no pawns still on their original home squares.
 
const scid::core::uint scid::database::HPSIG_StdStart = 0xFFFF
 Home-pawn signature for the starting position.
 

Detailed Description

Material and home-pawn signatures used by database search prefilters.

These signatures are deliberately lossy. They let the database reject impossible candidate games before decoding movetext, but they do not prove that a game contains a searched position. Material signatures answer "could these piece counts still be present?", while home-pawn signatures answer "could these original pawns have moved?". The exact board search still has to decode candidate games that pass these cheap tests.