implement popcnt with a simple loop
This commit is contained in:
parent
e30fab31e1
commit
bc9233d278
1 changed files with 6 additions and 17 deletions
23
lisc/util.c
23
lisc/util.c
|
@ -53,27 +53,16 @@ emiti(Ins i)
|
||||||
int
|
int
|
||||||
bcnt(Bits *b)
|
bcnt(Bits *b)
|
||||||
{
|
{
|
||||||
const uint64_t m1 = 0x5555555555555555;
|
int z, i, j;
|
||||||
const uint64_t m2 = 0x3333333333333333;
|
ulong tmp;
|
||||||
const uint64_t m3 = 0x0f0f0f0f0f0f0f0f;
|
|
||||||
const uint64_t m4 = 0x00ff00ff00ff00ff;
|
|
||||||
const uint64_t m5 = 0x0000ffff0000ffff;
|
|
||||||
const uint64_t m6 = 0x00000000ffffffff;
|
|
||||||
uint64_t tmp;
|
|
||||||
int z, i;
|
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
for (z=0; z<BITS; z++) {
|
for (z=0; z<BITS; z++) {
|
||||||
tmp = b->t[z];
|
tmp = b->t[z];
|
||||||
if (!tmp)
|
for (j=0; j<64; j++) {
|
||||||
continue;
|
i += 1 & tmp;
|
||||||
tmp = (tmp&m1) + (tmp>> 1&m1);
|
tmp >>= 1;
|
||||||
tmp = (tmp&m2) + (tmp>> 2&m2);
|
}
|
||||||
tmp = (tmp&m3) + (tmp>> 4&m3);
|
|
||||||
tmp = (tmp&m4) + (tmp>> 8&m4);
|
|
||||||
tmp = (tmp&m5) + (tmp>>16&m5);
|
|
||||||
tmp = (tmp&m6) + (tmp>>32&m6);
|
|
||||||
i += tmp;
|
|
||||||
}
|
}
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue