7 #include "../crc/xxhash.h"
8 #include "../crc/murmur3.h"
9 #include "../crc/crc32c.h"
10 #include "../crc/fnv.h"
18 #define BITS_PER_INDEX (sizeof(uint32_t) * 8)
19 #define BITS_INDEX_MASK (BITS_PER_INDEX - 1)
23 uint32_t (*fn)(const void *, uint32_t, uint32_t);
26 static uint32_t bloom_crc32c(const void *buf, uint32_t len, uint32_t seed)
28 return fio_crc32c(buf, len);
31 static uint32_t bloom_fnv(const void *buf, uint32_t len, uint32_t seed)
33 return fnv(buf, len, seed);
36 #define BLOOM_SEED 0x8989
38 static struct bloom_hash hashes[] = {
63 #define MIN_ENTRIES 1073741824UL
65 struct bloom *bloom_new(uint64_t entries)
72 b = malloc(sizeof(*b));
73 b->nentries = entries;
74 no_uints = (entries + BITS_PER_INDEX - 1) / BITS_PER_INDEX;
75 no_uints = max((unsigned long) no_uints, MIN_ENTRIES);
76 b->map = calloc(no_uints, sizeof(uint32_t));
85 void bloom_free(struct bloom *b)
91 static int __bloom_check(struct bloom *b, uint32_t *data, unsigned int nwords,
94 uint32_t hash[N_HASHES];
97 for (i = 0; i < N_HASHES; i++) {
98 hash[i] = hashes[i].fn(data, nwords, hashes[i].seed);
99 hash[i] = hash[i] % b->nentries;
103 for (i = 0; i < N_HASHES; i++) {
104 const unsigned int index = hash[i] / BITS_PER_INDEX;
105 const unsigned int bit = hash[i] & BITS_INDEX_MASK;
107 if (b->map[index] & (1U << bit))
110 b->map[index] |= 1U << bit;
113 return was_set == N_HASHES;
116 int bloom_set(struct bloom *b, uint32_t *data, unsigned int nwords)
118 return __bloom_check(b, data, nwords, 1);