summaryrefslogtreecommitdiff
path: root/src/engine/ttable.c
blob: fc1b6f0cf21b40d46ae4bcf51db88ebe150e4941 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <stdlib.h>
#include <string.h>

#include "engine/ttable.h"
#include "random.h"

void zobrist_init() {
  for (int i = 0; i < PIECE_TYPE_COUNT; i++) {
    for (int j = 0; j < 64; j++) {
      zobrist_keys.piece[i][j] = random_u64();
    }
  }
  for (int i = 0; i < CASTLE_COUNT; i++) {
    zobrist_keys.castle[i] = random_u64();
  }
  for (int i = 0; i < TURN_COUNT; i++) {
    zobrist_keys.turn[i] = random_u64();
  }
  for (int i = 0; i < 64; i++) {
    zobrist_keys.en_passant[i] = random_u64();
  }
}

u64 zobrist_hash(position_t position) {
  u64 output = 0;
  for (int i = 0; i < 64; i++) {
    int piece_type = find_piece_on_square(&position, i);
    output ^= zobrist_keys.piece[piece_type][i];
  }

  if (position.castling & WHITE_SHORT_CASTLE) {
    output ^= zobrist_keys.castle[0];
  }
  if (position.castling & WHITE_LONG_CASTLE) {
    output ^= zobrist_keys.castle[1];
  }
  if (position.castling & BLACK_SHORT_CASTLE) {
    output ^= zobrist_keys.castle[2];
  }
  if (position.castling & BLACK_LONG_CASTLE) {
    output ^= zobrist_keys.castle[3];
  }

  output ^= zobrist_keys.turn[position.turn];
  output ^= zobrist_keys.en_passant[position.passantable_file];

  return output;
}

void ttable_init(ttable_t *table, size_t size_in_binary_log) {
  assert(size_in_binary_log < 64);
  int size = 1ULL << size_in_binary_log;
  table->buckets = calloc(size, sizeof(tbucket_t));
  table->mask = size - 1;
}

void ttable_deinit(ttable_t table) {
  free(table.buckets);
}

void __ttable_insert(ttable_t table, struct ttable_insert_args args) {
  tbucket_t *bucket = table.buckets + (args.key & table.mask);
  tentry_t *best = bucket->entries;

  int ci = 0;

  for (int i = 0; i < TT_BUCKET_SIZE; i++) {
    if (atomic_load(&bucket->entries[i].key) == args.key) {
      best = bucket->entries + i;
      break;
    }
    if (bucket->entries[i].depth >= best->depth) continue;
    best = bucket->entries + i;
  }

  tentry_t object;
  object.best_move = args.move;
  object.score = args.score;
  object.eval = args.eval;
  object.depth = args.depth;
  object.flag = args.flag;
  object.generation = args.generation;

  memcpy(best, &object, sizeof(object));
  atomic_store(&best->key, args.key);
}

tentry_t *ttable_find(ttable_t table, u64 key) {
  tbucket_t *bucket = table.buckets + (key & table.mask);

  for (int i = 0; i < TT_BUCKET_SIZE; i++) {
    if (atomic_load(&(bucket->entries + i)->key) != key) continue;
    return bucket->entries + i;
  }

  return NULL;
}

#ifdef TEST_MOD

bool test_ttable_empty() {
  ttable_t table;
  ttable_init(&table, 4);

  if (ttable_find(table, 123456789ULL) != NULL) return false;

  ttable_deinit(table);
  return true;
}

bool test_ttable_insert_and_find() {
  ttable_t table;
  ttable_init(&table, 4);

  u64 key = 0x123456789ABCDEF0ULL;
  ttable_insert(table, .key = key, .depth = 8, .flag = TT_EXACT);

  tentry_t *e = ttable_find(table, key);

  assert(e != NULL);
  assert(e->key == key);
  assert(e->depth == 8);
  assert(e->flag == TT_EXACT);

  ttable_deinit(table);

  return true;
}

bool test_ttable_wrong_key() {
  ttable_t table;
  ttable_init(&table, 4);

  u64 key = 0x123456789ABCDEF0ULL;
  ttable_insert(table, .key = key, .depth = 8, .flag = TT_EXACT);
  assert(ttable_find(table, --key) == NULL);

  ttable_deinit(table);
  return true;
}

bool test_ttable_update_existing() {
  ttable_t table;
  ttable_init(&table, 4);

  u64 key = 987654321ULL;
  ttable_insert(table, .key = key, .depth = 10, .flag = TT_LOWERBOUND);
  ttable_insert(table, .key = key, .depth = 11, .flag = TT_EXACT);

  tentry_t *e = ttable_find(table, key);
  assert(e != NULL);
  assert(e->key == key);
  assert(e->depth == 11);
  assert(e->flag == TT_EXACT);

  ttable_deinit(table);
  return true;
}

bool test_bucket_collision() {
  ttable_t table;
  ttable_init(&table, 2);

  ttable_insert(table, .key = 1, .depth = 10, .flag = TT_EXACT);
  ttable_insert(table, .key = 2, .depth = 20, .flag = TT_EXACT);
  ttable_insert(table, .key = 3, .depth = 30, .flag = TT_EXACT);
  ttable_insert(table, .key = 4, .depth = 40, .flag = TT_EXACT);

  if (ttable_find(table, 1) == NULL) return false;
  if (ttable_find(table, 2) == NULL) return false;
  if (ttable_find(table, 3) == NULL) return false;
  if (ttable_find(table, 4) == NULL) return false;

  ttable_deinit(table);
  return true;
}

bool test_ttable_replace_shallowest() {
  ttable_t table;
  ttable_init(&table, 0);

  ttable_insert(table, .key = 1, .depth = 10, .flag = TT_EXACT);
  ttable_insert(table, .key = 2, .depth = 20, .flag = TT_EXACT);
  ttable_insert(table, .key = 3, .depth = 30, .flag = TT_EXACT);
  ttable_insert(table, .key = 4, .depth = 40, .flag = TT_EXACT);

  // Bucket is full.
  ttable_insert(table, .key = 5, .depth = 25, .flag = TT_EXACT);

  if (ttable_find(table, 1) != NULL) return false;

  if (ttable_find(table, 2) == NULL) return false;
  if (ttable_find(table, 3) == NULL) return false;
  if (ttable_find(table, 4) == NULL) return false;
  if (ttable_find(table, 5) == NULL) return false;

  ttable_deinit(table);
  return true;
}

bool test_ttable_many_entries() {
  ttable_t table;
  ttable_init(&table, 16);

  for (u64 i = 0; i < 50000; i++) {
    ttable_insert(table, .key = i * 7919, .depth = i % 64, .flag = TT_EXACT);
  }

  for (u64 i = 0; i < 50000; i++) {
    tentry_t *e = ttable_find(table, i * 7919);

    if (e == NULL) continue;
    if (e->key != i * 7919) return false;
  }

  ttable_deinit(table);
  return true;
}

#endif