Hash_table *construct_sweep_hash_table(Sweep *s) { Hash_table *hash_table; int i, iazim; Ray *ray; float res; if (s == NULL) return NULL; hash_table = (Hash_table *) calloc(1, sizeof(Hash_table)); hash_table->nindexes = s->h.nrays; if (hash_table->nindexes < 0) { fprintf(stderr, "Unable to construct sweep hash table because nrays = %d\n", s->h.nrays); fprintf(stderr, "FATAL error... unable to continue.\n"); exit(-1); } res = 360.0 / hash_table->nindexes; /* Check that this makes sense with beam width. */ if ((res > 2*s->h.beam_width) && (s->h.beam_width != 0)) { /* Problem. Too few rays so force a * reasonable hash table size */ hash_table->nindexes = 360.0 / s->h.beam_width; res = s->h.beam_width; } hash_table->indexes = (Azimuth_hash **)calloc(hash_table->nindexes, sizeof(Azimuth_hash *)); if (hash_table->indexes == NULL) { if (radar_verbose_flag) perror("construct_sweep_hash_table"); return hash_table; } for (i=0; i<s->h.nrays; i++) { ray = s->ray[i]; if (ray == NULL) continue; iazim = (int)(ray->h.azimuth/res + res/2.0); /* Centered on bin. */ if (iazim >= hash_table->nindexes) iazim -= hash_table->nindexes ; /* fprintf(stderr,"ray# %d, azim %f, iazim %d\n", ray->h.ray_num, ray->h.azimuth, iazim); */ if (iazim > hash_table->nindexes || iazim < 0) { if (radar_verbose_flag){ fprintf(stderr,"ERROR: "); fprintf(stderr,"ray# %d, azim %f, iazim %d, nrays %d, nindexes %d\n", ray->h.ray_num, ray->h.azimuth, iazim, s->h.nrays, hash_table->nindexes); } } else hash_table->indexes[iazim] = hash_add_node(hash_table->indexes[iazim], ray); } set_high_and_low_pointers(hash_table); return hash_table; }
uint64_t test_perft_rec(state_t *state, int depth, int verbose) { /* Given a position, it will recursivly apply every possible * move for a given depth and count the leaf nodes. */ if (depth == 0) { return 1; } hash_node_t *hash_node = hash_get_node(state->zobrist); /* Check if hashing is enabled */ if (hash_node) { if (hash_node->hash == state->zobrist && hash_node->depth == depth) { ++_cache_hits; return hash_node->score; } else { ++_cache_misses; } } int moves[100]; int count = 0, count2 = 0; move_generate_moves(state, moves, &count); move_generate_tactical(state, moves + count, &count2); count += count2; /* Check for invalid position or board with no pieces :) */ if (count <= 0) { return 0; } uint64_t nodes = 0; int i; for (i = 0; i < count; ++i) { make_move(state, moves[i], depth); if (move_is_attacked(state, state->king_idx[Flip(state->turn)], state->turn)) { unmake_move(state, moves[i], depth); continue; } uint64_t res = test_perft_rec(state, depth - 1, 0); nodes += res; if (verbose && res > 0) { char move_str[16]; util_move_to_lan(moves[i], move_str); printf("%s: %lld\n", move_str, res); } unmake_move(state, moves[i], depth); } hash_add_node(state->zobrist, nodes, depth, 0, 0); return nodes; }