From f8fd4e0437ae1a087015a023bcdb9fddaec57628 Mon Sep 17 00:00:00 2001 From: nick Date: Wed, 24 Jul 2013 11:19:52 +0100 Subject: [PATCH] bitset: Actually enable an optimization in bit_set/clear_range Previously, we were setting bits up to the first byte boundary, memset()ing to the last byte boundary, then ignoring the memset() and resetting every single bit up to the last one individually, from where the first for-loop left off. This should be *at least* nine times faster. --- src/bitset.h | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/bitset.h b/src/bitset.h index 41d618d..13e399d 100644 --- a/src/bitset.h +++ b/src/bitset.h @@ -34,16 +34,40 @@ static inline void bit_clear(char* b, uint64_t idx) { //__sync_fetch_and_nand(b+(idx/8), char_with_bit_set(idx)); } /** Sets ''len'' bits in array ''b'' starting at offset ''from'' */ -static inline void bit_set_range(char* b, uint64_t from, uint64_t len) { - for (; from%8 != 0 && len > 0; len--) { bit_set(b, from++); } - if (len >= 8) { memset(b+(from/8), 255, len/8); } - for (; len > 0; len--) { bit_set(b, from++); } +static inline void bit_set_range(char* b, uint64_t from, uint64_t len) +{ + for ( ; from%8 != 0 && len > 0 ; len-- ) { + bit_set( b, from++ ); + } + + if (len >= 8) { + memset(b+(from/8), 255, len/8 ); + from += len; + len = (len%8); + from -= len; + } + + for ( ; len > 0 ; len-- ) { + bit_set( b, from++ ); + } } /** Clears ''len'' bits in array ''b'' starting at offset ''from'' */ -static inline void bit_clear_range(char* b, uint64_t from, uint64_t len) { - for (; from%8 != 0 && len > 0; len--) { bit_clear(b, from++); } - if (len >= 8) { memset(b+(from/8), 0, len/8); } - for (; len > 0; len--) { bit_clear(b, from++); } +static inline void bit_clear_range(char* b, uint64_t from, uint64_t len) +{ + for ( ; from%8 != 0 && len > 0 ; len-- ) { + bit_clear( b, from++ ); + } + + if (len >= 8) { + memset(b+(from/8), 0, ( len/8 ) + 1); + from += len; + len = (len%8); + from -= len; + } + + for ( ; len > 0 ; len-- ) { + bit_clear( b, from++ ); + } } /** Counts the number of contiguous bits in array ''b'', starting at ''from''