hobbieschess

Move Generation

Well, we already covered this a little when talking about the board representation... but the movegen code in generate.c is responsible for generating the legal chess moves given a POSITION. And right now it has both the mailbox and bitboard move generator implementations side by side. That's how the speed comparison works in TEST=1 self-test builds. So it's a good place to look to see both.

The bitboard-based move generators come in two flavors: regular ones (for pieces like knights and kings, that do not "slide" and can't be blocked) and magic ones (that use "magic" numbers which, given a piece's location and the occupied squares on the board, return a bitboard with 1s in every square to which the piece can legally move).

Here is an example of the former, for knights:


// cKnight is where the knight is now.

// g_KnightAttacksBB[c] is the the bitboard for every square a knight on c
// can move to in one hop.

// pStack->bbFriendlyOccupied is the bitboard for friendly pieces the knight
// cannot capture.
BITBOARD bbDest = g_KnightAttacksBB[cKnight] & ~pStack->bbFriendlyOccupied;

ULONG uBitIndex;
COOR c;
PIECE p;
while (bbDest)
{
    // Get the first bit in the bbDest.
    uBitIndex = FastFirstBit(bbDest) - 1;

    // Clear that bit.
    bbDest &= (bbDest - 1);

    // Convert the bit to a COOR...
    c = BIT_NUMBER_TO_COOR(uBitIndex);

    // What piece (if any) is there?
    p = pos->rgSquare[c].pPiece;

    // Add the move.
    _AddNormalMove(pStack, pos, cKnight, c, p);
}

And here is an example of the latter, for bishops:


ULONG uMagicIndex;
BITBOARD bbDest;
ULONG uBitIndex;
COOR c;
PIECE p;

// Debug code, ignore.
ASSERT(IS_BISHOP(pos->rgSquare[cBishop].pPiece));
ASSERT(GET_COLOR(pos->rgSquare[cBishop].pPiece) == pos->uToMove);
ASSERT(pStack->bbFriendlyOccupied == _BuildFriendlySideBB(pos, pos->uToMove));
ASSERT(pStack->bbOccupied == _BuildFullOccupiedBB(pos));

// pStack->bbOccupied has a 1 for any square with a piece in it.
// g_BishopOccupancyMask[c] is the squares a bishop on c can move directly.
// g_BishopMagic[c] is this magic bitboard number.
// g_BishopAttackTable[c][magic] is: given a bishop on c with the magic
// number, return a bitboard of the squares the bishop can move directly.
// Then, use bbFriendlyOccupied to remove friends the bishop can't capture.
uMagicIndex = (ULONG)
    (((pStack->bbOccupied & g_BishopOccupancyMask[cBishop]) *
      g_BishopMagic[cBishop]) >> g_BishopMagicShift[cBishop]);
bbDest = g_BishopAttackTable[cBishop][uMagicIndex] &
    ~pStack->bbFriendlyOccupied;

// Loop over each bit, just like the knight did.
while (bbDest)
{
    uBitIndex = FastFirstBit(bb: bbDest) - 1;
    bbDest &= (bbDest - 1);
    c = BIT_NUMBER_TO_COOR(uBitIndex);
    p = pos->rgSquare[c].pPiece;
    _AddNormalMove(pStack, pos, cFrom: cBishop, cTo: c, pCap: p);
}

Benchmarking (with TEST=1) shows bitboards aren't a universal slam dunk for movegen itself, but they're, overall, slightly better in the average case. The real win comes from quicker computation of attack tables, detection of hung material, etc.

Move Data Structure

My first chess engine used a (somewhat large) struct to represent a move, passed around by pointer. When I started monsoon/typhoon, I I decided that a struct to represent a move was stupid and, instead made moves plain 32-bit numbers instead — passed directly on the stack by value, with no pointer dereferencing in the generator code.

Here's the definition (from chess.h):

typedef union _MOVE
{
    UINT move;
    struct
    {
        UCHAR cFrom        : 8;
        UCHAR cTo          : 8;
        UCHAR pMoved       : 4;
        UCHAR pCaptured    : 4;
        UCHAR pPromoted    : 4;
        UCHAR bvFlags      : 4;
    };
} MOVE;

The : 8 and : 4 constructs are C "bitfields," requesting that the compiler use eight or four bits to represent a struct member. When you use bitfields it's always good to confirm the compiler actually listens to your size requirements; it can take some fiddling to make it work. They're not the most portable part of C, but they're handy.

The move struct has from and to squares, the pieces moved, captured (if applicable) and promotion target (again, if applicable). And it has a bvFlags bitvector to store "move flags". The move flags bits I use are: special, checking, escaping check. "Special" is just a bit to turn an "ordinary" move into a special one... e.g. a special king move is castling, a special pawn move (with a capture) is an en passant, a special pawn move without a capture is a double jump, etc...

move-generation.html was last updated 2 December 2026 and is Copyright © 2002–2026 by Scott Gasch (scott.gasch@gmail.com).