52 for (i = 0; i < 16; ++i)
53 for (j = 0; j < 256; ++j) {
70 for (i = 0; i < 64; ++i)
71 for (j = 0; j < 60; ++j) {
90 for (n_way = 1; n_way <
HASH_N_WAY; n_way <<= 1);
92 assert(hash_table != NULL);
93 assert((n_way & -n_way) == n_way);
95 info(
"< init hashtable of %llu entries>\n", size);
96 if (hash_table->
hash != NULL) free(hash_table->
memory);
97 hash_table->
memory = malloc((size + n_way + 1) *
sizeof (
Hash));
98 if (hash_table->
memory == NULL) {
99 fatal_error(
"hash_init: cannot allocate the hash table\n");
103 const size_t alignment = n_way *
sizeof (
Hash) - 1;
104 hash_table->
hash = (
Hash*) (((
size_t) hash_table->
memory + alignment) & ~alignment);
115 hash_table->
n_lock += n_way + 1;
118 for (i = 0; i < hash_table->
n_lock; ++i) spin_init(hash_table->
lock + i);
129 register unsigned int i;
131 assert(hash_table != NULL && hash_table->
hash != NULL);
133 info(
"< cleaning hashtable >\n");
139 hash_table->
date = 0;
150 assert(hash_table != NULL);
154 info(
"< clearing hashtable -> date = %d>\n", hash_table->
date);
155 assert(hash_table->
date > 0 && hash_table->
date <= 127);
167 assert(hash_table != NULL && hash_table->
hash != NULL);
169 hash_table->
hash = NULL;
170 for (i = 0; i < hash_table->
n_lock; ++i) spin_free(hash_table->
lock + i);
171 free(hash_table->
lock);
183 return (*(
unsigned int*) data);
202static void data_update(
HashData *data,
const int cost,
const int alpha,
const int beta,
const int score,
const int move)
204 if (score < beta && score < data->upper) data->
upper = (
signed char) score;
205 if (score > alpha && score > data->
lower) data->
lower = (
signed char) score;
206 if ((score > alpha || score ==
SCORE_MIN) && data->
move[0] != move) {
208 data->
move[0] = (
unsigned char) move;
210 data->
cost = (
unsigned char)
MAX(cost, data->
cost);
229static void data_upgrade(
HashData *data,
const int depth,
const int selectivity,
const int cost,
const int alpha,
const int beta,
const int score,
const int move)
233 if ((score > alpha || score ==
SCORE_MIN) && data->
move[0] != move) {
235 data->
move[0] = (
unsigned char) move;
239 data->
cost = (
unsigned char)
MAX(cost, data->
cost);
258static void data_new(
HashData *data,
const int date,
const int depth,
const int selectivity,
const int cost,
const int alpha,
const int beta,
const int score,
const int move)
262 if (score > alpha || score ==
SCORE_MIN) data->
move[0] = (
unsigned char) move;
292#if (HASH_COLLISIONS(1)+0)
293static void hash_new(
Hash *hash,
HashLock *lock,
const unsigned long long hash_code,
const Board* board,
const int date,
const int depth,
const int selectivity,
const int cost,
const int alpha,
const int beta,
const int score,
const int move)
295static void hash_new(
Hash *hash,
HashLock *lock,
const Board* board,
const int date,
const int depth,
const int selectivity,
const int cost,
const int alpha,
const int beta,
const int score,
const int move)
302 hash->
board = *board;
303 data_new(&hash->
data, date, depth, selectivity, cost, alpha, beta, score, move);
326#if (HASH_COLLISIONS(1)+0)
327static void hash_set(
Hash *hash,
HashLock *lock,
const unsigned long long hash_code,
const Board *board,
const int date,
const int depth,
const int selectivity,
const int cost,
const int lower,
const int upper,
const int move)
329static void hash_set(
Hash *hash,
HashLock *lock,
const Board *board,
const int date,
const int depth,
const int selectivity,
const int cost,
const int lower,
const int upper,
const int move)
336 hash->
board = *board;
339 hash->
data.
move[0] = (
unsigned char) move;
341 hash->
data.
depth = (
unsigned char) depth;
343 hash->
data.
cost = (
unsigned char) cost;
344 hash->
data.
date = (
unsigned char) date;
371static bool hash_update(
Hash *hash,
HashLock *lock,
const Board *board,
const int date,
const int depth,
const int selectivity,
const int cost,
const int alpha,
const int beta,
const int score,
const int move)
381 else data_upgrade(data, depth, selectivity, cost, alpha, beta, score, move);
382 data->
date = (
unsigned char) date;
384 data_new(data, date, depth, selectivity, cost, alpha, beta, score, move);
414static bool hash_replace(
Hash *hash,
HashLock *lock,
const Board *board,
const int date,
const int depth,
const int selectivity,
const int cost,
const int alpha,
const int beta,
const int score,
const int move)
421 data_new(&hash->
data, date, depth, selectivity, cost, alpha, beta, score, move);
442static bool hash_reset(
Hash *hash,
HashLock *lock,
const Board *board,
const int date,
const int depth,
const int selectivity,
const int lower,
const int upper,
const int move)
452 if (data->
lower < lower) data->
lower = (
signed char) lower;
453 if (data->
upper > upper) data->
upper = (
signed char) upper;
455 data->
depth = (
unsigned char) depth;
457 data->
lower = (
signed char) lower;
458 data->
upper = (
signed char) upper;
461 data->
date = (
unsigned char) date;
463 if (data->
move[0] != move) {
465 data->
move[0] = (
unsigned char) move;
467 data->
move[1] = (
unsigned char) move;
489void hash_feed(
HashTable *hash_table,
const Board *board,
const unsigned long long hash_code,
const int depth,
const int selectivity,
const int lower,
const int upper,
const int move)
494 int date = hash_table->
date ? hash_table->
date : 1;
496 worst = hash = hash_table->
hash + (hash_code & hash_table->
hash_mask);
497 lock = hash_table->
lock + (hash_code & hash_table->
lock_mask);
498 if (
hash_reset(hash, lock, board, date, depth, selectivity, lower, upper, move))
return;
502 if (
hash_reset(hash, lock, board, date, depth, selectivity, lower, upper, move))
return;
509#if (HASH_COLLISIONS(1)+0)
510 hash_set(worst, lock, hash_code, board, date, depth, selectivity, 0, lower, upper, move);
512 hash_set(worst, lock, board, date, depth, selectivity, 0, lower, upper, move);
544void hash_store(
HashTable *hash_table,
const Board *board,
const unsigned long long hash_code,
const int depth,
const int selectivity,
const int cost,
const int alpha,
const int beta,
const int score,
const int move)
550 worst = hash = hash_table->
hash + (hash_code & hash_table->
hash_mask);
551 lock = hash_table->
lock + (hash_code & hash_table->
lock_mask);
552 if (
hash_update(hash, lock, board, hash_table->
date, depth, selectivity, cost, alpha, beta, score, move))
return;
556 if (
hash_update(hash, lock, board, hash_table->
date, depth, selectivity, cost, alpha, beta, score, move))
return;
561#if (HASH_COLLISIONS(1)+0)
562 hash_new(worst, lock, hash_code, board, hash_table->
date, depth, selectivity, cost, alpha, beta, score, move);
564 hash_new(worst, lock, board, hash_table->
date, depth, selectivity, cost, alpha, beta, score, move);
583void hash_force(
HashTable *hash_table,
const Board *board,
const unsigned long long hash_code,
const int depth,
const int selectivity,
const int cost,
const int alpha,
const int beta,
const int score,
const int move)
589 worst = hash = hash_table->
hash + (hash_code & hash_table->
hash_mask);
590 lock = hash_table->
lock + (hash_code & hash_table->
lock_mask);
591 if (
hash_replace(hash, lock, board, hash_table->
date, depth, selectivity, cost, alpha, beta, score, move))
return;
595 if (
hash_replace(hash, lock, board, hash_table->
date, depth, selectivity, cost, alpha, beta, score, move))
return;
601#if (HASH_COLLISIONS(1)+0)
602 hash_new(worst, lock, hash_code, board, hash_table->
date, depth, selectivity, cost, alpha, beta, score, move);
604 hash_new(worst, lock, board, hash_table->
date, depth, selectivity, cost, alpha, beta, score, move);
625 hash = hash_table->
hash + (hash_code & hash_table->
hash_mask);
626 lock = hash_table->
lock + (hash_code & hash_table->
lock_mask);
629 HASH_COLLISIONS( spin_lock(lock);)
630 HASH_COLLISIONS( if (hash->key == hash_code && (hash->board.player != board->player || hash->board.opponent != board->opponent)) {)
631 HASH_COLLISIONS( ++statistics.n_hash_collision;)
632 HASH_COLLISIONS( printf(
"key = %llu\n", hash_code);)
633 HASH_COLLISIONS( board_print(board, WHITE, stdout);)
634 HASH_COLLISIONS( board_print(&hash->board, WHITE, stdout);)
668 hash = hash_table->
hash + (hash_code & hash_table->
hash_mask);
669 lock = hash_table->
lock + (hash_code & hash_table->
lock_mask);
677 }
else if (hash->
data.
move[1] == move) {
697 register unsigned int i;
700 info(
"<hash copy>\n");
716 int p_selectivity[] = {72, 87, 95, 98, 99, 100};
720 fprintf(f,
"score = [%+02d, %+02d] ; ", data->
lower, data->
upper);
DLL_API int bit_count(unsigned long long b)
Count the number of bits set to one in an unsigned long long.
Definition bit.c:72
#define SCORE_MAX
Definition const.h:58
#define SCORE_INF
Definition const.h:52
@ NOMOVE
Definition const.h:37
@ WHITE
Definition const.h:43
#define SCORE_MIN
Definition const.h:55
void hash_exclude_move(HashTable *hash_table, const unsigned long long hash_code, const int move)
Erase an hash table entry.
Definition hash-lock-free.c:629
void hash_print(const HashData *data, FILE *f)
print HashData content.
Definition hash-lock-free.c:670
void hash_copy(const HashTable *src, HashTable *dest)
Copy an hastable to another one.
Definition hash-lock-free.c:652
static bool hash_reset(Hash *hash, HashLock *lock, const Board *board, const int date, const int depth, const int selectivity, const int lower, const int upper, const int move)
Reset an hash entry from new data values.
Definition hash.c:442
bool hash_get(HashTable *hash_table, const Board *board, const unsigned long long hash_code, HashData *data)
Find an hash table entry according to the evaluated board hash codes.
Definition hash.c:616
unsigned long long hash_rank[16][256]
Definition hash.c:35
static bool hash_update(Hash *hash, HashLock *lock, const Board *board, const int date, const int depth, const int selectivity, const int cost, const int alpha, const int beta, const int score, const int move)
update the hash entry
Definition hash.c:371
static void data_new(HashData *data, const int date, const int depth, const int selectivity, const int cost, const int alpha, const int beta, const int score, const int move)
Set an hash table data item.
Definition hash.c:258
static bool hash_replace(Hash *hash, HashLock *lock, const Board *board, const int date, const int depth, const int selectivity, const int cost, const int alpha, const int beta, const int score, const int move)
replace the hash entry.
Definition hash.c:414
static void data_update(HashData *data, const int cost, const int alpha, const int beta, const int score, const int move)
update an hash table item.
Definition hash.c:202
static void hash_set(Hash *hash, HashLock *lock, const Board *board, const int date, const int depth, const int selectivity, const int cost, const int lower, const int upper, const int move)
Set a new hash table item.
Definition hash.c:329
static void data_upgrade(HashData *data, const int depth, const int selectivity, const int cost, const int alpha, const int beta, const int score, const int move)
Upgrade an hash table data item.
Definition hash.c:229
void hash_clear(HashTable *hash_table)
Clear the hashtable.
Definition hash.c:148
void hash_store(HashTable *hash_table, const Board *board, const unsigned long long hash_code, const int depth, const int selectivity, const int cost, const int alpha, const int beta, const int score, const int move)
Store an hashtable item.
Definition hash.c:544
void hash_force(HashTable *hash_table, const Board *board, const unsigned long long hash_code, const int depth, const int selectivity, const int cost, const int alpha, const int beta, const int score, const int move)
Store an hashtable item.
Definition hash.c:583
void hash_init(HashTable *hash_table, const unsigned long long size)
Initialise the hashtable.
Definition hash.c:86
unsigned int writeable_level(HashData *data)
make a level from date, cost, depth & selectivity.
Definition hash.c:180
void hash_cleanup(HashTable *hash_table)
Clear the hashtable.
Definition hash.c:127
void hash_move_init(void)
Initialize global hash move data.
Definition hash.c:64
const HashData HASH_DATA_INIT
Definition hash.c:41
unsigned long long hash_move[64][60]
Definition hash.c:38
void hash_free(HashTable *hash_table)
Free the hashtable.
Definition hash.c:164
static void hash_new(Hash *hash, HashLock *lock, const Board *board, const int date, const int depth, const int selectivity, const int cost, const int alpha, const int beta, const int score, const int move)
Initialize a new hash table item.
Definition hash.c:295
void hash_feed(HashTable *hash_table, const Board *board, const unsigned long long hash_code, const int depth, const int selectivity, const int lower, const int upper, const int move)
feed hash table (from Cassio).
Definition hash.c:489
void hash_code_init(void)
Initialize global hash code data.
Definition hash.c:46
char * move_to_string(const int x, const int player, char *s)
Print out a move.
Definition move.c:76
#define USE_TYPE_PUNING
Definition settings.h:69
#define HASH_N_WAY
Definition settings.h:74
#define HASH_ALIGNED
Definition settings.h:77
Statistics statistics
Definition stats.c:21
#define HASH_COLLISIONS(x)
Definition stats.h:25
#define HASH_STATS(x)
Definition stats.h:23
unsigned long long player
Definition board.h:27
unsigned long long opponent
Definition board.h:27
unsigned char move[2]
Definition hash.h:31
signed char lower
Definition hash.h:29
unsigned char cost
Definition hash.h:27
unsigned char date
Definition hash.h:28
signed char upper
Definition hash.h:30
unsigned char selectivity
Definition hash.h:26
unsigned char depth
Definition hash.h:25
unsigned char date
Definition hash.h:55
unsigned int lock_mask
Definition hash.h:52
Hash * hash
Definition hash.h:49
unsigned long long hash_mask
Definition hash.h:51
HashLock * lock
Definition hash.h:50
void * memory
Definition hash.h:48
int n_lock
Definition hash.h:54
HashData data
Definition hash.h:38
Board board
Definition hash.h:37
unsigned long long n_hash_found
Definition stats.h:72
unsigned long long n_hash_upgrade
Definition stats.h:68
unsigned long long n_hash_n
Definition stats.h:74
unsigned long long n_hash_search
Definition stats.h:71
unsigned long long n_hash_update
Definition stats.h:67
unsigned long long n_hash_new
Definition stats.h:69
unsigned long long n_hash_remove
Definition stats.h:70
int get_cpu_number(void)
Get the number of cpus or cores on the machine.
Definition util.c:987
unsigned long long random_get(Random *random)
Pseudo-random number generator.
Definition util.c:1043
void random_seed(Random *random, const unsigned long long seed)
Pseudo-random number seed.
Definition util.c:1062
Miscellaneous utilities header.
#define fatal_error(...)
Display an error message as "FATAL_ERROR : file name : function name : line number : ....
Definition util.h:349
#define info(...)
Display a message.
Definition util.h:382
#define MAX(a, b)
Definition util.h:98