Skip to content

Commit

Permalink
use a full 8-bit table for popcount lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
spaskalev committed Jun 10, 2023
1 parent 2c6deda commit 8332066
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions buddy_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -1795,10 +1795,19 @@ static void bitset_debug(FILE *stream, unsigned char *bitset, size_t length) {
Bits
*/

static const unsigned char popcount_lookup[16] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
static const unsigned char popcount_lookup[256] = {
0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
};

static unsigned int popcount_byte(unsigned char b) {
return popcount_lookup[b & 15] + popcount_lookup[b >> 4];
static inline unsigned int popcount_byte(unsigned char b) {
return popcount_lookup[b];
}

/* Returns the highest set bit position for the given value. Returns zero for zero. */
Expand Down

0 comments on commit 8332066

Please sign in to comment.