aboutsummaryrefslogtreecommitdiff
path: root/include/zobrist.c
diff options
context:
space:
mode:
authortslil <tslil@posteo.de>2026-09-08 11:55:28 +0100
committertslil <tslil@posteo.de>2026-09-09 21:45:17 +0100
commit1c9e266ae25f7f6067806d139e5971ce8513d2f6 (patch)
tree9b935954310a56ee7fdba07db5f03888fc5af7b0 /include/zobrist.c
parent7ef68374e8a58684b299b0f46f7ed850d5a87190 (diff)
~25% speedup in selfplay due to: incremental zobrist hashing, smarter road checking, re-using the input computed for the NN
Diffstat (limited to 'include/zobrist.c')
-rw-r--r--include/zobrist.c21
1 files changed, 11 insertions, 10 deletions
diff --git a/include/zobrist.c b/include/zobrist.c
index 2922abf..5076fe1 100644
--- a/include/zobrist.c
+++ b/include/zobrist.c
@@ -50,18 +50,19 @@ void zobrist_free(void) {
}
-// a position's value is from the perspective of the player to move, which flips
-// with parity, so we must track this
-#define PLY_PARITY_MAGIC 0x9E3779B97F4A7C15ULL
+uint64_t zobrist_cell(tak_state_p state, const uint8_t l) {
+ colour_stack_t c = state->colours[l];
+ const uint8_t count = COUNT_AT(state, l);
+ enum STONE_VARIANT s = STONE_AT(state, l);
+ uint64_t h = 0;
+ for (uint8_t hh = 0; hh < count; hh++, c >>= 1)
+ h ^= zobrist[l * (15 * 2 * 3) + hh * 2 * 3 + (c & 1) * 3 + s];
+ return h;
+}
uint64_t zobrist_compute(tak_state_p state) {
uint64_t hash = 0;
- for (uint8_t l = 0; l < state->board_size * state->board_size; l++) {
- colour_stack_t c = state->colours[l];
- const uint8_t count = COUNT_AT(state, l);
- enum STONE_VARIANT s = STONE_AT(state, l);
- for (uint8_t h = 0; h < count; h++, c >>= 1)
- hash ^= zobrist[l * (15 * 2 * 3) + h * 2 * 3 + (c & 1) * 3 + s];
- }
+ for (uint8_t l = 0; l < state->board_size * state->board_size; l++)
+ hash ^= zobrist_cell(state, l);
return hash ^ ((state->ply & 1) ? PLY_PARITY_MAGIC : 0);
}