-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.c
408 lines (306 loc) · 12.1 KB
/
core.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**
* @file core.c
* @brief Implementation of the `core` struct and its associated functions.
*
* This file contains the implementation of the `core` struct, which is used to
* represent a sequence of encoded bits for string data. The stuct supports
* operations such as compression, comparison, and writing/reading to files.
*
* Key operations include:
* - Encoding strings into bit arrays using coefficient-based encoding.
* - Constructing `core` objects from strings or other `core` objects.
* - Compressing bit representations to optimize memory usage.
* - Writing and reading `core` objects to and from files.
* - Comparing `core` objects with overloaded operators.
* - Efficiently handling block-wise bit manipulations.
*
* @note The `STATS` macro is used to conditionally compile sections of the code
* that track additional metadata such as `start` and `end` indices for
* performance analysis.
*/
#include "core.h"
/**
* @brief Computes the 32-bit MurmurHash3 hash for a given key.
*
* This function computes a 32-bit hash of the input data 'key' with the
* specified length 'len' and an optional seed value. It processes the
* input in blocks and handles any remaining bytes.
*
* @param key Pointer to the data to be hashed.
* @param len The length of the data in bytes.
* @param seed An initial seed value for the hash computation.
* @return The resulting 32-bit hash value.
*/
uint32_t MurmurHash3_32(const void *key, int len, uint32_t seed) {
const uint8_t *data = (const uint8_t *)key;
const int nblocks = len / 4;
uint32_t h1 = seed;
const uint32_t c1 = 0xcc9e2d51;
const uint32_t c2 = 0x1b873593;
// Body: Process blocks of 4 bytes at a time
const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4);
for (int i = -nblocks; i; i++) {
uint32_t k1 = blocks[i];
k1 *= c1;
k1 = (k1 << 15) | (k1 >> (32 - 15));
k1 *= c2;
h1 ^= k1;
h1 = (h1 << 15) | (h1 >> (32 - 15));
h1 = h1 * 5 + 0xe6546b64;
}
// Tail: Process remaining bytes
const uint8_t *tail = (const uint8_t *)(data + nblocks * 4);
uint32_t k1 = 0;
switch (len & 3) {
case 3:
k1 ^= tail[2] << 16;
break;
case 2:
k1 ^= tail[1] << 8;
break;
case 1:
k1 ^= tail[0];
k1 *= c1;
k1 = (k1 << 15) | (k1 >> (32 - 15));
k1 *= c2;
h1 ^= k1;
}
// Finalization: Mix the hash to ensure the last few bits are fully mixed
h1 ^= len;
/* fmix32 */
h1 ^= h1 >> 16;
h1 *= 0x85ebca6b;
h1 ^= h1 >> 13;
h1 *= 0xc2b2ae35;
h1 ^= h1 >> 16;
return h1;
}
void init_core1(struct core *cr, const char *begin, uint64_t distance, uint64_t start_index, uint64_t end_index) {
cr->start = start_index;
cr->end = end_index;
cr->bit_size = alphabet_bit_size * distance;
/* allocate memory for representation */
ubit_size block_number = (cr->bit_size + UBLOCK_BIT_SIZE - 1) / UBLOCK_BIT_SIZE;
cr->bit_rep = (ublock *)malloc(block_number * sizeof(ublock));
memset(cr->bit_rep, 0, block_number * sizeof(ublock));
ubit_size shift = 0;
int block_index = block_number - 1;
for (const char *it = begin + distance - 1; begin <= it; it--) {
int o_bit_rep = alphabet[(int)(*it)];
/* shift and paste */
cr->bit_rep[block_index] |= (o_bit_rep << shift);
/* if there is an overflow after shifting, it pastes the */
/* overfloaw to the left block. */
if (shift + alphabet_bit_size > UBLOCK_BIT_SIZE) {
cr->bit_rep[block_index - 1] |= (o_bit_rep >> (UBLOCK_BIT_SIZE - shift));
}
if (shift + alphabet_bit_size >= UBLOCK_BIT_SIZE) {
block_index--;
}
shift = (shift + alphabet_bit_size) % UBLOCK_BIT_SIZE;
}
cr->label = 0;
cr->label |= ((distance-2) << (3 * alphabet_bit_size));
cr->label |= (alphabet[(*(begin)) & 0xDF] << (2 * alphabet_bit_size));
cr->label |= (alphabet[(*(begin+distance-2)) & 0xDF] << alphabet_bit_size);
cr->label |= (alphabet[(*(begin+distance-1)) & 0xDF]);
}
void init_core2(struct core *cr, const char *begin, uint64_t distance, uint64_t start_index, uint64_t end_index) {
cr->start = start_index;
cr->end = end_index;
cr->bit_size = alphabet_bit_size * distance;
/* allocate memory for representation */
ubit_size block_number = (cr->bit_size + UBLOCK_BIT_SIZE - 1) / UBLOCK_BIT_SIZE;
cr->bit_rep = (ublock *)malloc(block_number * sizeof(ublock));
memset(cr->bit_rep, 0, block_number * sizeof(ublock));
ubit_size shift = 0;
int block_index = block_number - 1;
for (const char *it = begin + distance - 1; begin <= it; it--) {
int o_bit_rep = rc_alphabet[(int)(*it)];
/* shift and paste */
cr->bit_rep[block_index] |= (o_bit_rep << shift);
/* if there is an overflow after shifting, it pastes the */
/* overfloaw to the left block. */
if (shift + alphabet_bit_size > UBLOCK_BIT_SIZE) {
cr->bit_rep[block_index - 1] |= (o_bit_rep >> (UBLOCK_BIT_SIZE - shift));
}
if (shift + alphabet_bit_size >= UBLOCK_BIT_SIZE) {
block_index--;
}
shift = (shift + alphabet_bit_size) % UBLOCK_BIT_SIZE;
}
cr->label = 0;
cr->label |= ((distance-2) << (3 * alphabet_bit_size));
cr->label |= (rc_alphabet[(*(begin)) & 0xDF] << (2 * alphabet_bit_size));
cr->label |= (rc_alphabet[(*(begin+distance-2)) & 0xDF] << alphabet_bit_size);
cr->label |= (rc_alphabet[(*(begin+distance-1)) & 0xDF]);
}
void init_core3(struct core *cr, struct core *begin, uint64_t distance) {
// it is known that other core is placed in cr
free(cr->bit_rep);
cr->start = begin->start;
cr->end = (begin+distance-1)->end;
cr->bit_size = 0;
for (struct core *it = begin; it < begin + distance; it++) {
cr->bit_size += it->bit_size;
}
/* allocate memory for representation */
ubit_size block_number = (cr->bit_size + UBLOCK_BIT_SIZE - 1) / UBLOCK_BIT_SIZE;
cr->bit_rep = (ublock *)malloc(block_number * sizeof(ublock));
memset(cr->bit_rep, 0, block_number * sizeof(ublock));
ubit_size shift = 0;
int block_index = block_number - 1;
for (struct core *it = begin + distance - 1; begin <= it; it--) {
ublock *o_bit_rep = it->bit_rep;
for (int i = (it->bit_size - 1) / UBLOCK_BIT_SIZE; 0 <= i; i--) {
ubit_size curr_block_size = (i > 0 ? UBLOCK_BIT_SIZE : it->bit_size % UBLOCK_BIT_SIZE);
/* shift and paste */
cr->bit_rep[block_index] |= (o_bit_rep[i] << shift);
/* if there is an overflow after shifting, it pastes the */
/* overfloaw to the left block. */
if (shift + curr_block_size > UBLOCK_BIT_SIZE) {
cr->bit_rep[block_index - 1] |= (o_bit_rep[i] >> (UBLOCK_BIT_SIZE - shift));
}
if (shift + curr_block_size >= UBLOCK_BIT_SIZE) {
block_index--;
}
shift = (shift + curr_block_size) % UBLOCK_BIT_SIZE;
}
}
ulabel data[4];
data[0] = (begin)->label;
data[1] = (begin+distance-2)->label;
data[2] = (begin+distance-1)->label;
data[3] = distance-2;
cr->label = MurmurHash3_32((void*)data, 4 * sizeof(ulabel), 42);
}
void init_core4(struct core *cr, ubit_size bit_size, ublock *bit_rep, ulabel label, uint64_t start, uint64_t end) {
cr->bit_size = bit_size;
cr->bit_rep = bit_rep;
cr->label = label;
cr->start = start;
cr->end = end;
}
void free_core(struct core* cr) {
free(cr->bit_rep);
cr->bit_rep = NULL;
}
void core_compress(const struct core *left_core, struct core *right_core) {
ubit_size index = minimum(left_core->bit_size, right_core->bit_size);
ubit_size left_block = (left_core->bit_size - 1) / UBLOCK_BIT_SIZE,
right_block = (right_core->bit_size - 1) / UBLOCK_BIT_SIZE;
while (index >= UBLOCK_BIT_SIZE && left_core->bit_rep[left_block] == right_core->bit_rep[right_block]) {
left_block--;
right_block--;
index -= UBLOCK_BIT_SIZE;
}
left_block = left_core->bit_rep[left_block];
right_block = right_core->bit_rep[right_block];
while (index > 0 && left_block % 2 == right_block % 2) {
left_block /= 2;
right_block /= 2;
index--;
}
if (right_core->bit_size > UBLOCK_BIT_SIZE) {
free(right_core->bit_rep);
right_core->bit_rep = (ublock *)malloc(sizeof(ublock));
}
right_core->bit_rep[0] = 0;
// shift left by 1 bit and set last bit to difference
right_core->bit_rep[0] = 2 * (minimum(right_core->bit_size, left_core->bit_size) - index) + (right_block % 2);
right_core->bit_size = 0;
if (right_core->bit_rep[0] > 0) {
right_core->bit_size = (32 - __builtin_clz(right_core->bit_rep[0]));
}
right_core->bit_size = right_core->bit_size > 1 ? right_core->bit_size : 2;
// now, the right core is dependent on the left; hence, its coverage spans towards the left
right_core->start = left_core->start;
}
uint64_t core_memsize(const struct core *cr) {
return sizeof(struct core) + sizeof(ublock) * ((cr->bit_size + UBLOCK_BIT_SIZE - 1) / UBLOCK_BIT_SIZE);
}
void print_core(const struct core *cr) {
uint64_t block_number = (cr->bit_size - 1) / UBLOCK_BIT_SIZE + 1;
for (int index = cr->bit_size - 1; 0 <= index; index--) {
printf("%d", (cr->bit_rep[block_number - index / UBLOCK_BIT_SIZE - 1] >> (index % UBLOCK_BIT_SIZE)) & 1);
}
}
// core comparison operator implementation
int core_eq(const struct core *lhs, const struct core *rhs) {
if (lhs->bit_size != rhs->bit_size) {
return 0;
}
ubit_size index = 0;
while (index < lhs->bit_size) {
if (lhs->bit_rep[index / UBLOCK_BIT_SIZE] != rhs->bit_rep[index / UBLOCK_BIT_SIZE])
return 0;
index += UBLOCK_BIT_SIZE;
}
return 1;
}
int core_neq(const struct core *lhs, const struct core *rhs) {
if (lhs->bit_size != rhs->bit_size) {
return 1;
}
ubit_size index = 0;
while (index < lhs->bit_size) {
if (lhs->bit_rep[index / UBLOCK_BIT_SIZE] != rhs->bit_rep[index / UBLOCK_BIT_SIZE])
return 1;
index += UBLOCK_BIT_SIZE;
}
return 0;
}
int core_gt(const struct core *lhs, const struct core *rhs) {
if (lhs->bit_size != rhs->bit_size) {
return lhs->bit_size > rhs->bit_size;
}
ubit_size index = 0;
while (index < lhs->bit_size) {
if (lhs->bit_rep[index / UBLOCK_BIT_SIZE] == rhs->bit_rep[index / UBLOCK_BIT_SIZE]) {
index += UBLOCK_BIT_SIZE;
continue;
}
return lhs->bit_rep[index / UBLOCK_BIT_SIZE] > rhs->bit_rep[index / UBLOCK_BIT_SIZE];
}
return 0;
}
int core_lt(const struct core *lhs, const struct core *rhs) {
if (lhs->bit_size != rhs->bit_size) {
return lhs->bit_size < rhs->bit_size;
}
ubit_size index = 0;
while (index < lhs->bit_size) {
if (lhs->bit_rep[index / UBLOCK_BIT_SIZE] == rhs->bit_rep[index / UBLOCK_BIT_SIZE]) {
index += UBLOCK_BIT_SIZE;
continue;
}
return lhs->bit_rep[index / UBLOCK_BIT_SIZE] < rhs->bit_rep[index / UBLOCK_BIT_SIZE];
}
return 0;
}
int core_geq(const struct core *lhs, const struct core *rhs) {
if (lhs->bit_size != rhs->bit_size) {
return lhs->bit_size >= rhs->bit_size;
}
ubit_size index = 0;
while (index < lhs->bit_size) {
if (lhs->bit_rep[index / UBLOCK_BIT_SIZE] != rhs->bit_rep[index / UBLOCK_BIT_SIZE]) {
return lhs->bit_rep[index / UBLOCK_BIT_SIZE] >= rhs->bit_rep[index / UBLOCK_BIT_SIZE];
}
index += UBLOCK_BIT_SIZE;
}
return 1;
}
int core_leq(const struct core *lhs, const struct core *rhs) {
if (lhs->bit_size != rhs->bit_size) {
return lhs->bit_size <= rhs->bit_size;
}
ubit_size index = 0;
while (index < lhs->bit_size) {
if (lhs->bit_rep[index / UBLOCK_BIT_SIZE] != rhs->bit_rep[index / UBLOCK_BIT_SIZE]) {
return lhs->bit_rep[index / UBLOCK_BIT_SIZE] <= rhs->bit_rep[index / UBLOCK_BIT_SIZE];
}
index += UBLOCK_BIT_SIZE;
}
return 1;
}