Changeset 3164


Ignore:
Timestamp:
Sep 25, 2007, 5:43:39 PM (15 years ago)
Author:
charles
Message:

tweak the bitfield code to be a little faster.

Location:
trunk/libtransmission
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/utils.c

    r3117 r3164  
    751751}
    752752
    753 #define BIN(nth) ((nth>>3))
    754 #define BIT(nth) (1<<(7-(nth%8)))
     753int
     754tr_bitfieldHas( const tr_bitfield * bitfield, size_t nth )
     755{
     756    static const uint8_t ands[8] = { 128, 64, 32, 16, 8, 4, 2, 1 };
     757    return bitfield!=NULL && (bitfield->bits[nth>>3u] & ands[nth&7u] );
     758}
    755759
    756760void
    757761tr_bitfieldAdd( tr_bitfield  * bitfield, size_t nth )
    758762{
    759     assert( bitfield != NULL );
    760     assert( BIN(nth) < bitfield->len );
    761     bitfield->bits[ BIN(nth) ] |= BIT(nth);
     763    static const uint8_t ands[8] = { 128, 64, 32, 16, 8, 4, 2, 1 };
     764    bitfield->bits[nth>>3u] |= ands[nth&7u];
    762765}
    763766
     
    777780                size_t          nth )
    778781{
     782    static const uint8_t rems[8] = { 127, 191, 223, 239, 247, 251, 253, 254 };
     783
    779784    if( bitfield != NULL )
    780     {
    781         const size_t bin = BIN(nth);
    782         assert( bin < bitfield->len );
    783         bitfield->bits[bin] &= ~BIT(nth);
    784     }
     785        bitfield->bits[nth>>3u] &= rems[nth&7u];
    785786}
    786787
  • trunk/libtransmission/utils.h

    r3117 r3164  
    180180void tr_bitfieldRemRange ( tr_bitfield*, size_t begin, size_t end );
    181181
     182int    tr_bitfieldHas( const tr_bitfield*, size_t bit );
    182183int    tr_bitfieldIsEmpty( const tr_bitfield* );
    183184size_t tr_bitfieldCountTrueBits( const tr_bitfield* );
     
    186187tr_bitfield* tr_bitfieldAnd( tr_bitfield*, const tr_bitfield* );
    187188
    188 #define tr_bitfieldHas(bitfield,nth) \
    189     ( ( bitfield ) && ( (bitfield)->bits[(nth)>>3] & 128 >>( (nth) & 7 ) ) )
    190 
    191189#endif
Note: See TracChangeset for help on using the changeset viewer.