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

tweak the bitfield code to be a little faster.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.