forked from sysprog21/simrupt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zobrist.c
75 lines (64 loc) · 1.76 KB
/
zobrist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <linux/list.h>
#include <linux/slab.h>
#include "mt19937-64.h"
#include "zobrist.h"
uint64_t zobrist_table[N_GRIDS][2];
#define HASH(key) ((key) % HASH_TABLE_SIZE)
static struct hlist_head *hash_table;
void zobrist_init(void)
{
int i;
for (i = 0; i < N_GRIDS; i++) {
zobrist_table[i][0] = mt19937_rand();
zobrist_table[i][1] = mt19937_rand();
}
hash_table =
kmalloc(sizeof(struct hlist_head) * HASH_TABLE_SIZE, GFP_KERNEL);
// assert(hash_table);
for (i = 0; i < HASH_TABLE_SIZE; i++)
INIT_HLIST_HEAD(&hash_table[i]);
}
zobrist_entry_t *zobrist_get(uint64_t key)
{
unsigned long long hash_key = HASH(key);
if (hlist_empty(&hash_table[hash_key]))
return NULL;
zobrist_entry_t *entry = NULL;
#ifdef __HAVE_TYPEOF
hlist_for_each_entry (entry, &hash_table[hash_key], ht_list)
;
#endif
{
if (entry->key == key)
return entry;
}
return NULL;
}
void zobrist_put(uint64_t key, int score, int move)
{
unsigned long long hash_key = HASH(key);
zobrist_entry_t *new_entry = kmalloc(sizeof(zobrist_entry_t), GFP_KERNEL);
// assert(new_entry);
new_entry->key = key;
new_entry->move = move;
new_entry->score = score;
hlist_add_head(&new_entry->ht_list, &hash_table[hash_key]);
}
void zobrist_clear(void)
{
for (int i = 0; i < HASH_TABLE_SIZE; i++) {
while (!hlist_empty(&hash_table[i])) {
zobrist_entry_t *entry;
entry = hlist_entry(hash_table[i].first, zobrist_entry_t, ht_list);
hlist_del(&entry->ht_list);
kfree(entry);
}
INIT_HLIST_HEAD(&hash_table[i]);
}
}
void zobrist_destroy_table(void)
{
zobrist_clear();
kfree(hash_table);
hash_table = NULL;
}