Board representation(s): 0x88 and bitboards
One of the things I did after picking the old engine back up in 2026 was convert it to use bitboards. It had been using a 0x88 board representation since it was written, but everyone says "bitboards are faster," and I was puzzling over why Crafty, on my AMD Zen2, would run at 5.5Mnps while my code only ran at 2.0Mnps. But before I get to how that conversion turned out, let me explain the two representations themselves.
0x88
I first heard of the 0x88 idea on the Computer Chess Club bulletin board, reading a post by Bruce Moreland (Ferret / Gerbil). In the 0x88 representation, the chess board is contained in an array of 128 elements (instead of the normal 64 — one per square). Half of the board space is therefore "wasted," in that it is not used for storing piece information.
There are several nice things about this representation, though. First, indexes into the board array are simple unsigned characters (one byte in length). It turns out to be trivial (and fast) to test whether an index points to a legal square on the board: simply AND it with 0x88 (hence the name). If the result of the AND is zero, the square is on the board; otherwise it is not. This fact can make writing a relatively fast move generator pretty simple (see my generator or Gerbil for an example).
Unlike 2D array-based board representations, accessing the contents of a square with the 0x88 representation uses only one index, which reduces the amount of work required to determine the offset of your square in the array. Finally, the "deltas" between related squares on the board are constant. Moving +/- 16 moves up or down one rank. Moving +/- 1 moves left or right one file. And the diagonals are +/-15 and +/-17.
Another explanation of the 0x88 board format (and other chess board formats) can be found here.
| A | B | C | D | E | F | G | H | unused | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 8 | 0x00 0 | 0x01 1 | 0x02 2 | 0x03 3 | 0x04 4 | 0x05 5 | 0x06 6 | 0x07 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 7 | 0x10 16 | 0x11 17 | 0x12 18 | 0x13 19 | 0x14 20 | 0x15 21 | 0x16 22 | 0x17 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
| 6 | 0x20 32 | 0x21 33 | 0x22 34 | 0x23 35 | 0x24 36 | 0x25 37 | 0x26 38 | 0x27 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 |
| 5 | 0x30 48 | 0x31 49 | 0x32 50 | 0x33 51 | 0x34 52 | 0x35 53 | 0x36 54 | 0x37 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |
| 4 | 0x40 64 | 0x41 65 | 0x42 66 | 0x43 67 | 0x44 68 | 0x45 69 | 0x46 70 | 0x47 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 |
| 3 | 0x50 80 | 0x51 81 | 0x52 82 | 0x53 83 | 0x54 84 | 0x55 85 | 0x56 86 | 0x57 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 |
| 2 | 0x60 96 | 0x61 97 | 0x62 98 | 0x63 99 | 0x64 100 | 0x65 101 | 0x66 102 | 0x67 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 |
| 1 | 0x70 112 | 0x71 113 | 0x72 114 | 0x73 115 | 0x74 116 | 0x75 117 | 0x76 118 | 0x77 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 |
Bitboards and "magic" bitboards
When Typhoon was started, 64-bit processors were not the default (I guess the DEC Alpha was around?). Today they are. A 64-bit CPU has 64-bit wide registers, can operate on 64-bit integers quickly, and can address a 64-bit virtual address space. Hm... 64 squares on the board, 64 bits in a register... what if we used one bit to represent a piece's location? That's the idea behind bitboards.
One bit isn't enough to denote "what kind of piece is here," only
"there is some piece here (or not)." So more than one bitboard is
needed. Typhoon uses these (from POSITION in
chess.h):
// Per-color, per-piece-type location bitboards -- "where are my
// knights/bishops/rooks/queens" -- indexed by PIECE_TYPE exactly
// like uNonPawnCount above (slots 0/1/PAWN/KING unused, same
// convention). Maintained incrementally in move.c at every site
// that already updates uNonPawnCount[]
BITBOARD bbPieces[2][8];
// Per-color pawn location bitboard -- same incremental-maintenance
// idea as bbPieces above, but for pawns (which bbPieces
// deliberately excludes).
BITBOARD bbPawns[2];
// Full-board occupancy (both colors, every piece including pawns
// and kings) -- same incremental-maintenance idea as bbPieces/
// bbPawns above.
BITBOARD bbOccupied;
// Per-side occupancy, same incremental-maintenance treatment as
// bbOccupied above.
BITBOARD bbOccupiedSide[2];
If you want all the white rooks, you look at
pos->bbPieces[WHITE][ROOK], etc.
Because 64-bit processors can operate on 64-bit ints in one cycle,
bitboards also let you do "interesting things" quickly. For example,
here's some code from the old HCE's EvalRook function,
with some extra annotation comments added:
// c is the COOR of the rook being evaluated, i.e. its square on
// the board.
// This is a bitboard of all 1's on the vertical file of c.
BITBOARD bbFile = BBFILE[FILE(c)];
// Masking friendly/enemy pawn bitboards with this bbFile will
// select only the friend/enemy pawns on the same file as the
// rook we're evaluating.
BITBOARD bbFriendPawns = pos->bbPawns[uColor] & bbFile;
BITBOARD bbEnemyPawns = pos->bbPawns[xColor] & bbFile;
// In eval.c especially (and also other engine code), we try to
// avoid if-statements. In x86/x64 assembly, those are often
// CMPs which can be mis-predicted by the CPU and slow down the
// code. Instead, (bbFriendPawns != 0) turns into either a 0 or
// a 1. Likewise (bbEnemyPawns != 0). Then a simple lookup table
// gets a bonus...
SCORE bonus = ROOK_FULL_HALF_OPEN_BONUS[bbFriendPawns != 0][bbEnemyPawns != 0];
// Extra bonus if the friendly or enemy pawn is a passer the rook
// is protecting or attacking? More bitboards -- bbPasserLocations
// is already computed and hashed in the pawn hash table, so a
// couple more ANDs (1 CPU cycle each) does the trick:
bonus += (SCORE)((bbEnemyPawns & pHash->bbPasserLocations[xColor]) != 0) *
ROOK_WITH_ENEMY_PASSER;
bonus += (SCORE)((bbFriendPawns & pHash->bbPasserLocations[uColor]) != 0) *
ROOK_WITH_FRIEND_PASSER;
// Is the rook on the 7th rank (or 2nd, if it's a black rook)?
bonus += (SCORE)((COOR_TO_BB(c) & BBROOK_7TH_RANK[uColor]) != 0) *
ROOK_ON_SEVENTH_BONUS;
All of this is nice, but where bitboards really shine is in move generation and mobility computation. This is also where they get tricky. For these two jobs, "magic" bitboards are used. It works like this:
void
_GenerateBishopBB(IN MOVE_STACK *pStack,
IN POSITION *pos,
IN COOR cBishop)
{
ULONG uMagicIndex;
BITBOARD bbDest;
ULONG uBitIndex;
COOR c;
PIECE p;
// Given the "occupied" bitboard which tracks all squares on
// the board that have some piece (either color) in them and
// the current location of the piece being moved (a bishop),
// do some magic bit fiddling to return a new bitboard with
// 1s in every position where the bishop can move!
uMagicIndex = (ULONG)
(((pStack->bbOccupied & g_BishopOccupancyMask[cBishop]) *
g_BishopMagic[cBishop]) >> g_BishopMagicShift[cBishop]);
// Mask off bits that are friendly pieces we can't capture.
bbDest = g_BishopAttackTable[cBishop][uMagicIndex] &
~pStack->bbFriendlyOccupied;
// Loop over the bitboard and consider each potential move.
while (bbDest)
{
uBitIndex = FastFirstBit(bbDest) - 1;
bbDest &= (bbDest - 1);
c = BIT_NUMBER_TO_COOR(uBitIndex);
p = pos->rgSquare[c].pPiece;
_AddNormalMove(pStack, pos, cBishop, c, p);
}
}
So, which is faster?
I got interested in bitboards when I noticed how much faster Crafty ran on my AMD Zen2 than Typhoon did: ~5.5Mnps on a single core vs. my own code's ~2Mnps at the time. So I ported over but kept the 0x88 array around, since some code still uses it, and I was kinda expecting a big speed payoff.
I was disappointed when I (and Claude Code) did the work, though:
it was simply not that much faster. You can see why by building with
TEST=1 and running the self-test code:
Testing GetAttacks...
Benchmarking _GetAttacksBB (200000 calls/position)...
opening : _GetAttacksBB 43 cycles/call
middlegame: _GetAttacksBB 43 cycles/call
endgame : _GetAttacksBB 49 cycles/call
Testing IsAttacked...
Benchmarking IsAttacked: mailbox vs IsAttackedBB (interleaved, 200000 calls/position)...
opening : mailbox 85 cycles/call, IsAttackedBB 36 cycles/call (BB is 0.43x mailbox)
middlegame: mailbox 79 cycles/call, IsAttackedBB 36 cycles/call (BB is 0.46x mailbox)
endgame : mailbox 40 cycles/call, IsAttackedBB 37 cycles/call (BB is 0.92x mailbox)
Testing _WhoAttacksSquareMagicBB (magic vs ray-walk)...
Benchmarking _WhoAttacksSquareBB: ray-walk vs magic (interleaved, 200000 calls/position)...
opening : ray-walk 36 cycles/call, magic 36 cycles/call (magic is 1.00x ray-walk)
middlegame: ray-walk 35 cycles/call, magic 36 cycles/call (magic is 1.02x ray-walk)
endgame : ray-walk 35 cycles/call, magic 35 cycles/call (magic is 1.00x ray-walk)
Benchmarking knight move generation: mailbox vs _GenerateKnightBB (interleaved, 200000 calls/position)...
opening : mailbox 41 cycles/call, _GenerateKnightBB 44 cycles/call (BB is 1.08x mailbox)
middlegame: mailbox 79 cycles/call, _GenerateKnightBB 82 cycles/call (BB is 1.03x mailbox)
endgame : mailbox 101 cycles/call, _GenerateKnightBB 98 cycles/call (BB is 0.96x mailbox)
Benchmarking king move generation: mailbox vs _GenerateKingBB (interleaved, 200000 calls/position)...
opening : mailbox 40 cycles/call, _GenerateKingBB 38 cycles/call (BB is 0.95x mailbox)
middlegame: mailbox 43 cycles/call, _GenerateKingBB 39 cycles/call (BB is 0.90x mailbox)
endgame : mailbox 96 cycles/call, _GenerateKingBB 105 cycles/call (BB is 1.09x mailbox)
Benchmarking rook move generation: mailbox vs _GenerateRookBB (interleaved, 200000 calls/position)...
opening : mailbox 38 cycles/call, _GenerateRookBB 39 cycles/call (BB is 1.03x mailbox)
middlegame: mailbox 38 cycles/call, _GenerateRookBB 39 cycles/call (BB is 1.03x mailbox)
endgame : mailbox 117 cycles/call, _GenerateRookBB 140 cycles/call (BB is 1.19x mailbox)
Benchmarking bishop move generation: mailbox vs _GenerateBishopBB (interleaved, 200000 calls/position)...
opening : mailbox 40 cycles/call, _GenerateBishopBB 37 cycles/call (BB is 0.94x mailbox)
middlegame: mailbox 122 cycles/call, _GenerateBishopBB 114 cycles/call (BB is 0.94x mailbox)
endgame : mailbox 78 cycles/call, _GenerateBishopBB 91 cycles/call (BB is 1.17x mailbox)
Benchmarking queen move generation: mailbox vs _GenerateQueenBB (interleaved, 200000 calls/position)...
opening : mailbox 68 cycles/call, _GenerateQueenBB 38 cycles/call (BB is 0.57x mailbox)
middlegame: mailbox 144 cycles/call, _GenerateQueenBB 119 cycles/call (BB is 0.83x mailbox)
endgame : mailbox 210 cycles/call, _GenerateQueenBB 194 cycles/call (BB is 0.93x mailbox)
Benchmarking whole-node move generation dispatch: _GenerateAllMoves (mailbox JumpTable) vs _GenerateAllMovesBB (direct bbPieces dispatch), interleaved, 200000 calls/position -- only meaningful in a toggle-free build...
opening : mailbox 396 cycles/call, BB dispatch 349 cycles/call (BB is 0.88x mailbox)
middlegame: mailbox 566 cycles/call, BB dispatch 527 cycles/call (BB is 0.93x mailbox)
endgame : mailbox 533 cycles/call, BB dispatch 519 cycles/call (BB is 0.97x mailbox)
Above, we're directly comparing the number of cycles it takes to generate moves, generate moves per-piece, and check whether a square is attacked (needed for check detection) with both bitboards and the "mailbox" (0x88) code they replaced. If you look closely, bitboards are not a pure win.
Bitboard-based movegen is slower, generally, in endgames where pieces have a lot of squares that it's legal for them to move to and thus they have no real advantage over simple loop-based 0x88 movegen. They tend to do better for pieces "boxed in" in the early game though, like bishops/queens.
Where bitboards are a clear win is code
like the eval examples above — those ideas are much harder to
implement with fast 0x88 code. And, as you can see above, the
isAttacked code and SEE are way faster
with bitboards. The SEE was so much faster with
bitboards that I deleted the hand-coded assembly version of it
after switching over and measuring.
Pieces
In typhoon, pieces on the 0x88 board are represented as simple bit patterns stored in an array. One bit signifies color and three bits signify piece type, so only half a byte ("a nibble") is required to store a piece on the board. I used to pack other information into the other nibble (a quick lookup index) but gave that up when bitboards came in heavily.
// ----------------------------------------------------------------------
//
// PIECE:
//
// a piece (4 bits) = type (3 bits) + color (1 bit)
//
// 3 1 0
// . . . .
// | | |
// type C
//
#define KING (6) // 110X
#define QUEEN (5) // 101X
#define ROOK (4) // 100X
#define BISHOP (3) // 011X
#define KNIGHT (2) // 010X
#define PAWN (1) // 001X
#define EMPTY (0) // 000X
// (WHATEVER << 1)
#define BLACK_PAWN (2) // 0010
#define BLACK_KNIGHT (4) // 0100
#define BLACK_BISHOP (6) // 0110
#define BLACK_ROOK (8) // 1000
#define BLACK_QUEEN (10) // 1010
#define BLACK_KING (12) // 1100
// (WHATEVER << 1) | WHITE
#define WHITE_PAWN (3) // 0011
#define WHITE_KNIGHT (5) // 0101
#define WHITE_BISHOP (7) // 0111
#define WHITE_ROOK (9) // 1001
#define WHITE_QUEEN (11) // 1011
#define WHITE_KING (13) // 1101
POSITION
In addition to knowing where all the pieces are, the engine must track whose turn it is, what castling options remain legal, where en-passant can be played, how close to a 50-move draw it is, etc. All of these are aspects of a position structure (from chess.h):
typedef struct _POSITION
{
SQUARE rgSquare[128]; // where the pieces are (0x88 layout)
UINT64 u64NonPawnSig; // hash signature
UINT64 u64PawnSig; // pawn hash signature
ULONG uToMove; // whose turn?
ULONG uFifty; // 50 moves w/o progress = draw
FLAG fCastled[2]; // record when sides have castled
BITV bvCastleInfo; // who can castle how?
COOR cEpSquare; // en-passant capture square
ULONG uNonPawnMaterial[2]; // piece material of each side
COOR cKingSq[2]; // king location, per color
BITBOARD bbPieces[2][8]; // per-color/piece-type location bbs
BITBOARD bbPawns[2]; // per-color pawn location bb
BITBOARD bbOccupied; // full-board occupancy
BITBOARD bbOccupiedSide[2]; // per-side occupancy
// Only constructed / used by the HCE.
BITBOARD bbPawnAttacks[2];
BITBOARD bbMinorAttacks[2];
BITBOARD bbMinorXrayAttacks[2];
BITBOARD bbRookAttacks[2];
BITBOARD bbRookXrayAttacks[2];
BITBOARD bbQueenAttacks[2];
BITBOARD bbQueenXrayAttacks[2];
BITBOARD bbKingAttacks[2];
SCORE iMaterialBalance[2]; // material balance
}
POSITION;