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- nbits[(n >> 16)&0xffu]+
|
|
|
- nbits[(n >> 24)&0xffu];
|
|
|
+ cntbits[n&0xffu]+
|
|
|
+ cntbits[(n >> 8)&0xffu]+
|
|
|
+ cntbits[(n >> 16)&0xffu]+
|
|
|
+ cntbits[(n >> 24)&0xffu];
|
|
|
}
|
|
|
|
|
|
// Counts set bit in bitfield.
|
|
@@ -125,6 +126,26 @@ inline size_t countSetBit(const unsigned char* bitfield, size_t nbits)
|
|
|
return count;
|
|
|
}
|
|
|
|
|
|
+// Counts set bit in bitfield. This is a bit slower than countSetBit
|
|
|
+// but can accept array template expression as bitfield.
|
|
|
+template<typename Array>
|
|
|
+size_t countSetBitSlow(const Array& bitfield, size_t nbits)
|
|
|
+{
|
|
|
+ if(nbits == 0) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ size_t count = 0;
|
|
|
+ size_t to = (nbits+7)/8;
|
|
|
+ if(to > 1) {
|
|
|
+ for(size_t i = 0; i < to - 1; ++i) {
|
|
|
+ count += cntbits[static_cast<unsigned char>(bitfield[i])];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ count +=
|
|
|
+ cntbits[static_cast<unsigned char>(bitfield[to - 1])&lastByteMask(nbits)];
|
|
|
+ return count;
|
|
|
+}
|
|
|
+
|
|
|
void flipBit(unsigned char* data, size_t length, size_t bitIndex);
|
|
|
|
|
|
// Stores first set bit index of bitfield to index. bitfield contains
|