protect hash_init() against multiple calls

This commit is contained in:
2024-05-21 07:49:49 +02:00
parent 77695574aa
commit 07a545adae

View File

@@ -32,10 +32,14 @@ hasht_t hash_tt; /* main transposition table */
/** /**
* zobrist_init() - initialize zobrist tables. * zobrist_init() - initialize zobrist tables.
* *
* Initialize all zobrist random bitmasks. * Initialize all zobrist random bitmasks. Must be called before any other
* zobrist function, and can be called once only (further calls will be ignored).
*/ */
void zobrist_init(void) void zobrist_init(void)
{ {
static bool called = false;
if (!called) {
called = true;
for (color_t c = WHITE; c <= BLACK; ++c) { for (color_t c = WHITE; c <= BLACK; ++c) {
for (piece_type_t p = PAWN; p <= KING; ++p) for (piece_type_t p = PAWN; p <= KING; ++p)
for (square_t sq = A1; sq <= H8; ++sq) for (square_t sq = A1; sq <= H8; ++sq)
@@ -45,9 +49,10 @@ void zobrist_init(void)
zobrist_castling[c] = rand64(); zobrist_castling[c] = rand64();
for (file_t f = FILE_A; f <= FILE_H; ++f) for (file_t f = FILE_A; f <= FILE_H; ++f)
zobrist_ep[f] = rand64(); zobrist_ep[f] = rand64();
zobrist_ep[8] = 0; zobrist_ep[8] = 0; /* see EP_ZOBRIST_IDX macro */
zobrist_turn = rand64(); zobrist_turn = rand64();
} }
}
/** /**
* zobrist_calc() - calculate a position zobrist hash. * zobrist_calc() - calculate a position zobrist hash.
@@ -89,7 +94,7 @@ key_t zobrist_calc(pos_t *pos)
* @pos: &position * @pos: &position
* *
* Verify that position Zobrist key matches a full Zobrist calculation. * Verify that position Zobrist key matches a full Zobrist calculation.
* This function cannot be called if DEBUG_HASH is not set. * This function cannot be called if ZOBRIST_VERIFY is not set.
* *
* @return: True if Zobrist key is OK. * @return: True if Zobrist key is OK.
*/ */
@@ -132,7 +137,7 @@ bool zobrist_verify(pos_t *pos)
} }
warn(true, "zobrist diff %lx is unknown\n", diff); warn(true, "zobrist diff %lx is unknown\n", diff);
end: end:
return false; bug_on(false);
} }
#endif #endif