diff options
Diffstat (limited to 'include/nn.c')
| -rw-r--r-- | include/nn.c | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/include/nn.c b/include/nn.c index c1a806d..f228297 100644 --- a/include/nn.c +++ b/include/nn.c @@ -15,6 +15,8 @@ along with ctak. If not, see <https://www.gnu.org/licenses/>. */ +#include <string.h> + #include "nn.h" #include "weights_5.h" @@ -53,7 +55,7 @@ inline uint8_t cell_idx_at(const tak_state_p state, const uint8_t loc) { default: { // |{flat, standing, cap} * {black, white}| = 6 // index 1 .. 6 - return (uint8_t)(1u + 3u * (state->colours[loc] & 1u) + (d & STONE_MASK)); + return (uint8_t)(1u + 3u * TOP_COLOUR_AT(state, loc) + (d & STONE_MASK)); } } } @@ -72,14 +74,30 @@ inline void nn_prepare_head(float out_head[3], tak_state_p state) { out_head[2] = (float)(state->black_count & 127) * inv_max_flats; } +// The cell indices computed by the most recent nn_prepare_input, and the hash +// of the state that produced them. +static uint8_t prepared_idxs[36]; // max board is 6x6 +static uint64_t prepared_hash = 0; + void nn_prepare_input(float *cur_board, tak_state_p state, const float embedding[NN_N_EMB]) { const unsigned int n_cells = state->board_size * state->board_size; nn_prepare_head(cur_board, state); - uint8_t idxs[36]; - nn_cell_idxs(idxs, state); + nn_cell_idxs(prepared_idxs, state); + prepared_hash = state->hash; for (unsigned int loc = 0; loc < n_cells; loc++) - cur_board[3 + loc] = embedding[idxs[loc]]; + cur_board[3 + loc] = embedding[prepared_idxs[loc]]; +} + +// Copy the cell indices produced by the most recent nn_prepare_input into out +// and return 1, but only if that call was made on exactly this state, otherwise +// return 0 and leave out unfilled. +int nn_last_cell_idxs(uint8_t *out, tak_state_p state) { + const unsigned int n_cells = state->board_size * state->board_size; + if (prepared_hash != state->hash) + return 0; + memcpy(out, prepared_idxs, n_cells); + return 1; } // =================================================================== |
