7 #include "../crc/xxhash.h"
8 #include "../crc/crc32c.h"
16 #define BITS_PER_INDEX (sizeof(uint32_t) * 8)
17 #define BITS_INDEX_MASK (BITS_PER_INDEX - 1)
21 uint32_t (*fn)(const void *, uint32_t, uint32_t);
24 static uint32_t b_crc32c(const void *buf, uint32_t len, uint32_t seed)
26 return fio_crc32c(buf, len);
29 struct bloom_hash hashes[] = {
46 #define MIN_ENTRIES 1073741824UL
48 struct bloom *bloom_new(uint64_t entries)
55 b = malloc(sizeof(*b));
56 b->nentries = entries;
57 no_uints = (entries + BITS_PER_INDEX - 1) / BITS_PER_INDEX;
58 no_uints = max((unsigned long) no_uints, MIN_ENTRIES);
59 b->map = calloc(no_uints, sizeof(uint32_t));
68 void bloom_free(struct bloom *b)
74 static int __bloom_check(struct bloom *b, uint32_t *data, unsigned int nwords,
77 uint32_t hash[N_HASHES];
80 for (i = 0; i < N_HASHES; i++) {
81 hash[i] = hashes[i].fn(data, nwords, hashes[i].seed);
82 hash[i] = hash[i] % b->nentries;
86 for (i = 0; i < N_HASHES; i++) {
87 const unsigned int index = hash[i] / BITS_PER_INDEX;
88 const unsigned int bit = hash[i] & BITS_INDEX_MASK;
90 if (b->map[index] & (1U << bit))
93 b->map[index] |= 1U << bit;
96 return was_set == N_HASHES;
99 int bloom_check(struct bloom *b, uint32_t *data, unsigned int nwords)
101 return __bloom_check(b, data, nwords, 0);
104 int bloom_set(struct bloom *b, uint32_t *data, unsigned int nwords)
106 return __bloom_check(b, data, nwords, 1);