14 #define BITS_PER_INDEX (sizeof(uint32_t) * 8)
15 #define BITS_INDEX_MASK (BITS_PER_INDEX - 1)
17 static unsigned int jhash_init[] = { 0, 0x12db635, 0x2a4a53 };
20 #define MIN_ENTRIES 1073741824UL
22 struct bloom *bloom_new(uint64_t entries)
27 b = malloc(sizeof(*b));
28 b->nentries = entries;
29 no_uints = (entries + BITS_PER_INDEX - 1) / BITS_PER_INDEX;
30 no_uints = max((unsigned long) no_uints, MIN_ENTRIES);
31 b->map = calloc(no_uints, sizeof(uint32_t));
40 void bloom_free(struct bloom *b)
46 static int __bloom_check(struct bloom *b, uint32_t *data, unsigned int nwords,
49 uint32_t hashes[N_HASHES];
52 for (i = 0; i < N_HASHES; i++)
53 hashes[i] = jhash(data, nwords, jhash_init[i]) % b->nentries;
56 for (i = 0; i < N_HASHES; i++) {
57 const unsigned int index = hashes[i] / BITS_PER_INDEX;
58 const unsigned int bit = hashes[i] & BITS_INDEX_MASK;
60 if (b->map[index] & (1U << bit))
63 b->map[index] |= 1U << bit;
66 return was_set == N_HASHES;
69 int bloom_check(struct bloom *b, uint32_t *data, unsigned int nwords)
71 return __bloom_check(b, data, nwords, 0);
74 int bloom_set(struct bloom *b, uint32_t *data, unsigned int nwords)
76 return __bloom_check(b, data, nwords, 1);