diff options
| author | Adam <adammegarules1@gmail.com> | 2026-08-19 21:19:32 +0200 |
|---|---|---|
| committer | Adam <adammegarules1@gmail.com> | 2026-08-19 21:19:32 +0200 |
| commit | d10a05eb92423777c6ff32edd14a1bea32214801 (patch) | |
| tree | c5b6868b7b2c8444dc42898bd569a0f7f3778726 /src/zobrist.cpp | |
| parent | 692029dd4614fd4b5f99538be0b46153eb8a19bc (diff) | |
perf(board): making hashing use incremental XORs instead of rebuild from scratch
Diffstat (limited to 'src/zobrist.cpp')
| -rw-r--r-- | src/zobrist.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/zobrist.cpp b/src/zobrist.cpp index c06f1a2..afef3e6 100644 --- a/src/zobrist.cpp +++ b/src/zobrist.cpp @@ -348,6 +348,39 @@ void InitZobrist() { SideKey = PolyglotRandom64[idx]; } +void xorCastleKey(bool color, bool IS_KING_SIDE, Game *g) { + const bool WHITE = true; + + if (color == WHITE && IS_KING_SIDE && g->whiteCastleKing) { + g->hash ^= CastleKeys[0]; + }; + if (color == WHITE && !IS_KING_SIDE && g->whiteCastleQueen) { + g->hash ^= CastleKeys[1]; + }; + if (color == !WHITE && IS_KING_SIDE && g->blackCastleKing) { + g->hash ^= CastleKeys[2]; + }; + if (color == !WHITE && !IS_KING_SIDE && g->blackCastleQueen) { + g->hash ^= CastleKeys[3]; + }; +} +void xorSidekey(Game *g) { g->hash ^= SideKey; } +void xorEnpassantKey(Game *g) { + if (g->canEnpassant && IsEnpassantLegal(g)) { + g->hash ^= EnPassantKeys[g->enPassant.file]; + } +} +void xorSquare(uint8_t square, Game *g) { + Piece p = g->pieces[square]; + if (p.type == NONEPIECE) { + return; + } + g->hash ^= PieceKeys[p.color][p.type][square]; +} + +/* + * should be only called at start of game + */ uint64_t GenerateZobristKey(Game *b) { uint64_t key = 0; |
