Skip to content

Commit

Permalink
refactor: zobrist cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Disservin committed Jul 17, 2023
1 parent ae758c5 commit 7b74f5b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 51 deletions.
36 changes: 13 additions & 23 deletions src/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void Board::setFen(const std::string &fen, bool update_acc) {
occupancy_bb_ = 0ULL;

auto square = Square(56);
for (char curr: position) {
for (char curr : position) {
if (CHAR_TO_PIECE.find(curr) != CHAR_TO_PIECE.end()) {
const Piece piece = CHAR_TO_PIECE[curr];
placePiece<false>(piece, square);
Expand All @@ -158,7 +158,7 @@ void Board::setFen(const std::string &fen, bool update_acc) {

castling_rights_.clearAllCastlingRights();

for (char i: castling) {
for (char i : castling) {
if (!chess960) {
if (i == 'K')
castling_rights_.setCastlingRight<WHITE, CastleSide::KING_SIDE, File::FILE_H>();
Expand Down Expand Up @@ -329,8 +329,9 @@ void Board::makeNullMove() {
state_history_.emplace_back(en_passant_square_, castling_rights_, half_move_clock_, NONE);

// Update the hash key
hash_key_ ^= updateKeySideToMove();
if (en_passant_square_ != NO_SQ) hash_key_ ^= updateKeyEnPassant(en_passant_square_);
hash_key_ ^= zobrist::sideToMove();
if (en_passant_square_ != NO_SQ)
hash_key_ ^= zobrist::enpassant(squareFile(en_passant_square_));

TTable.prefetch(hash_key_);

Expand All @@ -346,8 +347,9 @@ void Board::unmakeNullMove() {

en_passant_square_ = restore.en_passant;

hash_key_ ^= updateKeySideToMove();
if (en_passant_square_ != NO_SQ) hash_key_ ^= updateKeyEnPassant(en_passant_square_);
hash_key_ ^= zobrist::sideToMove();
if (en_passant_square_ != NO_SQ)
hash_key_ ^= zobrist::enpassant(squareFile(en_passant_square_));

castling_rights_ = restore.castling;
half_move_clock_ = restore.half_move;
Expand Down Expand Up @@ -390,35 +392,23 @@ U64 Board::zobrist() const {
// Piece hashes
while (w_pieces) {
Square sq = builtin::poplsb(w_pieces);
hash ^= updateKeyPiece(at(sq), sq);
hash ^= zobrist::piece(at(sq), sq);
}

while (b_pieces) {
Square sq = builtin::poplsb(b_pieces);
hash ^= updateKeyPiece(at(sq), sq);
hash ^= zobrist::piece(at(sq), sq);
}

// Ep hash
U64 ep_hash = 0ULL;

if (en_passant_square_ != NO_SQ) {
ep_hash = updateKeyEnPassant(en_passant_square_);
ep_hash = zobrist::enpassant(squareFile(en_passant_square_));
}

U64 turn_hash = side_to_move_ == WHITE ? RANDOM_ARRAY[780] : 0;
U64 cast_hash = updateKeyCastling();
U64 turn_hash = side_to_move_ == WHITE ? zobrist::sideToMove() : 0;
U64 cast_hash = zobrist::castling(castling_rights_.getHashIndex());

return hash ^ cast_hash ^ turn_hash ^ ep_hash;
}

U64 Board::updateKeyPiece(Piece piece, Square sq) const {
assert(piece < NONE);
assert(sq < NO_SQ);
return RANDOM_ARRAY[64 * hash_piece[piece] + sq];
}

U64 Board::updateKeyEnPassant(Square sq) const { return RANDOM_ARRAY[772 + squareFile(sq)]; }

U64 Board::updateKeyCastling() const { return castlingKey[castling_rights_.getHashIndex()]; }

U64 Board::updateKeySideToMove() const { return RANDOM_ARRAY[780]; }
46 changes: 19 additions & 27 deletions src/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "nnue.h"
#include "tt.h"
#include "types.h"
#include "zobrist.h"

extern TranspositionTable TTable;

Expand Down Expand Up @@ -143,16 +144,6 @@ class Board {
bool chess960 = false;

private:
// update the hash

[[nodiscard]] U64 updateKeyPiece(Piece piece, Square sq) const;

[[nodiscard]] U64 updateKeyCastling() const;

[[nodiscard]] U64 updateKeyEnPassant(Square sq) const;

[[nodiscard]] U64 updateKeySideToMove() const;

std::unique_ptr<Accumulators> accumulators_ = std::make_unique<Accumulators>();

std::vector<State> state_history_;
Expand Down Expand Up @@ -237,10 +228,11 @@ inline void Board::updateHash(Move move) {

hash_history_.emplace_back(hash_key_);

if (en_passant_square_ != NO_SQ) hash_key_ ^= updateKeyEnPassant(en_passant_square_);
if (en_passant_square_ != NO_SQ)
hash_key_ ^= zobrist::enpassant(squareFile(en_passant_square_));
en_passant_square_ = NO_SQ;

hash_key_ ^= updateKeyCastling();
hash_key_ ^= zobrist::castling(castling_rights_.getHashIndex());

if (piece_type == KING) {
castling_rights_.clearCastlingRight(side_to_move_);
Expand All @@ -252,13 +244,13 @@ inline void Board::updateHash(Move move) {
const Square rook_sq = rookCastleSquare(to_sq, from_sq);
const Square king_to_sq = kingCastleSquare(to_sq, from_sq);

hash_key_ ^= updateKeyPiece(rook, to_sq);
hash_key_ ^= updateKeyPiece(rook, rook_sq);
hash_key_ ^= updateKeyPiece(piece, from_sq);
hash_key_ ^= updateKeyPiece(piece, king_to_sq);
hash_key_ ^= zobrist::piece(rook, to_sq);
hash_key_ ^= zobrist::piece(rook, rook_sq);
hash_key_ ^= zobrist::piece(piece, from_sq);
hash_key_ ^= zobrist::piece(piece, king_to_sq);

hash_key_ ^= updateKeySideToMove();
hash_key_ ^= updateKeyCastling();
hash_key_ ^= zobrist::sideToMove();
hash_key_ ^= zobrist::castling(castling_rights_.getHashIndex());

return;
}
Expand All @@ -277,13 +269,13 @@ inline void Board::updateHash(Move move) {
half_move_clock_ = 0;

if (typeOf(move) == ENPASSANT) {
hash_key_ ^= updateKeyPiece(makePiece(PAWN, ~side_to_move_), ep_sq);
hash_key_ ^= zobrist::piece(makePiece(PAWN, ~side_to_move_), ep_sq);
} else if (std::abs(from_sq - to_sq) == 16) {
Bitboard ep_mask = attacks::pawn(ep_sq, side_to_move_);

if (ep_mask & pieces(PAWN, ~side_to_move_)) {
en_passant_square_ = ep_sq;
hash_key_ ^= updateKeyEnPassant(en_passant_square_);
hash_key_ ^= zobrist::enpassant(squareFile(en_passant_square_));

assert(at(en_passant_square_) == NONE);
}
Expand All @@ -293,7 +285,7 @@ inline void Board::updateHash(Move move) {
if (capture != Piece::NONE) {
half_move_clock_ = 0;

hash_key_ ^= updateKeyPiece(capture, to_sq);
hash_key_ ^= zobrist::piece(capture, to_sq);

if (typeOfPiece(capture) == ROOK && ((rank == Rank::RANK_1 && side_to_move_ == BLACK) ||
(rank == Rank::RANK_8 && side_to_move_ == WHITE))) {
Expand All @@ -307,15 +299,15 @@ inline void Board::updateHash(Move move) {
}

if (typeOf(move) == PROMOTION) {
hash_key_ ^= updateKeyPiece(makePiece(PAWN, side_to_move_), from_sq);
hash_key_ ^= updateKeyPiece(makePiece(promotionType(move), side_to_move_), to_sq);
hash_key_ ^= zobrist::piece(makePiece(PAWN, side_to_move_), from_sq);
hash_key_ ^= zobrist::piece(makePiece(promotionType(move), side_to_move_), to_sq);
} else {
hash_key_ ^= updateKeyPiece(piece, from_sq);
hash_key_ ^= updateKeyPiece(piece, to_sq);
hash_key_ ^= zobrist::piece(piece, from_sq);
hash_key_ ^= zobrist::piece(piece, to_sq);
}

hash_key_ ^= updateKeySideToMove();
hash_key_ ^= updateKeyCastling();
hash_key_ ^= zobrist::sideToMove();
hash_key_ ^= zobrist::castling(castling_rights_.getHashIndex());
}

/// @brief
Expand Down
19 changes: 18 additions & 1 deletion src/zobrist.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#pragma once
#include "types.h"

/****************************************************************************\
* Polyglot Zobrist Hash *
\****************************************************************************/
namespace zobrist {

static constexpr U64 RANDOM_ARRAY[781] = {
0x9D39247E33776D41, 0x2AF7398005AAA5C7, 0x44DB015024623547, 0x9C15F73E62A76AE2,
0x75834465489C0C89, 0x3290AC3A203001BF, 0x0FBBAD1F61042279, 0xE83A908FF2FB60CA,
Expand Down Expand Up @@ -217,4 +222,16 @@ static constexpr U64 castlingKey[16] = {
RANDOM_ARRAY[768 + 1] ^ RANDOM_ARRAY[768 + 2] ^ RANDOM_ARRAY[768 + 3],
RANDOM_ARRAY[768 + 1] ^ RANDOM_ARRAY[768 + 2] ^ RANDOM_ARRAY[768 + 3] ^ RANDOM_ARRAY[768]};

static constexpr int hash_piece[12] = {1, 3, 5, 7, 9, 11, 0, 2, 4, 6, 8, 10};
static constexpr int MAP_HASH_PIECE[12] = {1, 3, 5, 7, 9, 11, 0, 2, 4, 6, 8, 10};

[[nodiscard]] inline U64 piece(Piece piece, Square square) {
return RANDOM_ARRAY[64 * MAP_HASH_PIECE[static_cast<int>(piece)] + square];
}

[[nodiscard]] inline U64 enpassant(File file) { return RANDOM_ARRAY[772 + static_cast<int>(file)]; }

[[nodiscard]] inline U64 castling(int castling) { return castlingKey[castling]; }

[[nodiscard]] inline U64 sideToMove() { return RANDOM_ARRAY[780]; }

} // namespace zobrist

0 comments on commit 7b74f5b

Please sign in to comment.