From 0c45ed935d81d7df958604ae4df34fda7181fca8 Mon Sep 17 00:00:00 2001 From: "Rob Kendrick (humdrum)" Date: Thu, 16 May 2013 17:24:05 +0100 Subject: Better bit and byte selection --- utils/bloom.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'utils/bloom.c') diff --git a/utils/bloom.c b/utils/bloom.c index e6b9dcf92..1b07d6f1b 100644 --- a/utils/bloom.c +++ b/utils/bloom.c @@ -20,7 +20,6 @@ * Trivial bloom filter */ #include -#include #include "utils/bloom.h" /** @@ -79,10 +78,11 @@ void bloom_insert_str(struct bloom_filter *b, const char *s, size_t z) void bloom_insert_hash(struct bloom_filter *b, uint32_t hash) { - int index = hash % b->size; - int bit = hash % 8; + unsigned int index = hash % (b->size << 3); + unsigned int byte_index = index >> 3; + unsigned int bit_index = index & 7; - b->filter[index] |= (1 << bit); + b->filter[byte_index] |= (1 << bit_index); b->items++; } @@ -94,10 +94,11 @@ bool bloom_search_str(struct bloom_filter *b, const char *s, size_t z) bool bloom_search_hash(struct bloom_filter *b, uint32_t hash) { - int index = hash % b->size; - int bit = hash % 8; + unsigned int index = hash % (b->size << 3); + unsigned int byte_index = index >> 3; + unsigned int bit_index = index & 7; - return (b->filter[index] & (1 << bit)) != 0; + return (b->filter[byte_index] & (1 << bit_index)) != 0; } uint32_t bloom_items(struct bloom_filter *b) -- cgit v1.2.3