5 #include "../crc/xxhash.h"
6 #include "../crc/murmur3.h"
7 #include "../crc/crc32c.h"
8 #include "../crc/fnv.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 bloom_crc32c(const void *buf, uint32_t len, uint32_t seed)
26 return fio_crc32c(buf, len);
29 static uint32_t bloom_fnv(const void *buf, uint32_t len, uint32_t seed)
31 return fnv(buf, len, seed);
34 #define BLOOM_SEED 0x8989
36 static struct bloom_hash hashes[] = {
61 struct bloom *bloom_new(uint64_t entries)
69 b = malloc(sizeof(*b));
70 b->nentries = entries;
71 no_uints = (entries + BITS_PER_INDEX - 1) / BITS_PER_INDEX;
72 b->map = calloc(no_uints, sizeof(uint32_t));
81 void bloom_free(struct bloom *b)
87 static bool __bloom_check(struct bloom *b, const void *data, unsigned int len,
90 uint32_t hash[N_HASHES];
93 for (i = 0; i < N_HASHES; i++) {
94 hash[i] = hashes[i].fn(data, len, hashes[i].seed);
95 hash[i] = hash[i] % b->nentries;
99 for (i = 0; i < N_HASHES; i++) {
100 const unsigned int index = hash[i] / BITS_PER_INDEX;
101 const unsigned int bit = hash[i] & BITS_INDEX_MASK;
103 if (b->map[index] & (1U << bit))
106 b->map[index] |= 1U << bit;
111 return was_set == N_HASHES;
114 bool bloom_set(struct bloom *b, uint32_t *data, unsigned int nwords)
116 return __bloom_check(b, data, nwords * sizeof(uint32_t), true);
119 bool bloom_string(struct bloom *b, const char *data, unsigned int len,
122 return __bloom_check(b, data, len, set);