From 0048047647953c55506769a5912d8f3f410a2408 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 13 Aug 2008 19:25:08 +0000 Subject: [PATCH] (libT) tr_bitfieldRemRange speedup. (muks) --- libtransmission/utils.c | 75 ++++++++++++----------------------------- 1 file changed, 22 insertions(+), 53 deletions(-) diff --git a/libtransmission/utils.c b/libtransmission/utils.c index 4b056e4f0..bd66cd458 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -780,53 +780,6 @@ tr_bitfieldHas( const tr_bitfield * bitfield, size_t nth ) && ( tr_bitfieldHasFast( bitfield, nth ) ); } -#if 0 -static int -find_top_bit( uint8_t val ) -{ - int pos = 0; - if ( val & 0xF0U ) pos |= 4, val >>= 4; - if ( val & 0xCU ) pos |= 2, val >>= 2; - if ( val & 0x2 ) pos |= 1; - return 7 - pos; -} - -int -tr_bitfieldFindTrue( const tr_bitfield * bitfield, - size_t startBit, - size_t * setmePos ) -{ - if( bitfield && bitfield->bits && startBit < bitfield->bitCount ) - { - const uint8_t * b = bitfield->bits + startBit/8; - const uint8_t * end = bitfield->bits + bitfield->byteCount; - - /* If first byte already contains a set bit after startBit*/ - if( *b & ( 0xff >> (startBit&7) ) ) { - *setmePos = 8 * ( b - bitfield->bits ); - *setmePos += find_top_bit( *b & ( 0xff >> (startBit&7) ) ); - return 1; - } - - /* Test bitfield for first non zero byte */ - ++b; - while( (b < end) && !*b ) - ++b; - - /* If we hit the end of our bitfield, no set bit was found */ - if( b == end ) - return 0; - - /* New bitposition is byteoff*8 */ - *setmePos = 8 * ( b - bitfield->bits ) + find_top_bit( *b ); - - return 1; - } - - return 0; -} -#endif - int tr_bitfieldAdd( tr_bitfield * bitfield, size_t nth ) { @@ -872,12 +825,28 @@ tr_bitfieldRemRange ( tr_bitfield * b, size_t begin, size_t end ) { - int err = 0; - size_t i; - for( i=begin; i= b->bitCount ) || ( begin > end ) ) + return -1; + + sb = begin >> 3; + sm = 0xff << ( 8 - ( begin & 7 ) ); + eb = end >> 3; + em = ~( 0xff << ( 8 - ( end & 7 ) ) ); + + if ( sb == eb ) { + b->bits[sb] &= ( sm | em ); + } else { + b->bits[sb] &= sm; + b->bits[eb] &= em; + if( ++sb < eb ) + memset (b->bits + sb, 0, eb - sb); + } + + return 0; + } tr_bitfield*