mirror of
https://github.com/chhylp123/hifiasm.git
synced 2026-09-15 12:47:57 +08:00
43 lines
1018 B
C++
43 lines
1018 B
C++
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "yak.h"
|
|
|
|
yak_bf_t *yak_bf_init(int n_shift, int n_hashes)
|
|
{
|
|
yak_bf_t *b;
|
|
void *ptr = 0;
|
|
if (n_shift + YAK_BLK_SHIFT > 64 || n_shift < YAK_BLK_SHIFT) return 0;
|
|
CALLOC(b, 1);
|
|
b->n_shift = n_shift;
|
|
b->n_hashes = n_hashes;
|
|
posix_memalign(&ptr, 1<<(YAK_BLK_SHIFT-3), 1ULL<<(n_shift-3));
|
|
b->b = (uint8_t*)ptr;
|
|
bzero(b->b, 1ULL<<(n_shift-3));
|
|
return b;
|
|
}
|
|
|
|
void yak_bf_destroy(yak_bf_t *b)
|
|
{
|
|
if (b == 0) return;
|
|
free(b->b); free(b);
|
|
}
|
|
|
|
int yak_bf_insert(yak_bf_t *b, uint64_t hash)
|
|
{
|
|
int x = b->n_shift - YAK_BLK_SHIFT;
|
|
uint64_t y = hash & ((1ULL<<x) - 1);
|
|
int h1 = hash >> x & YAK_BLK_MASK;
|
|
int h2 = hash >> b->n_shift & YAK_BLK_MASK;
|
|
uint8_t *p = &b->b[y<<(YAK_BLK_SHIFT-3)];
|
|
int i, z = h1, cnt = 0;
|
|
if ((h2&31) == 0) h2 = (h2 + 1) & YAK_BLK_MASK; // otherwise we may repeatedly use a few bits
|
|
for (i = 0; i < b->n_hashes; z = (z + h2) & YAK_BLK_MASK) {
|
|
uint8_t *q = &p[z>>3], u;
|
|
u = 1<<(z&7);
|
|
cnt += !!(*q & u);
|
|
*q |= u;
|
|
++i;
|
|
}
|
|
return cnt;
|
|
}
|