implement popcnt with a simple loop

This commit is contained in:
Quentin Carbonneaux 2015-10-05 17:30:03 -04:00
parent e30fab31e1
commit bc9233d278

View file

@ -53,27 +53,16 @@ emiti(Ins i)
int
bcnt(Bits *b)
{
const uint64_t m1 = 0x5555555555555555;
const uint64_t m2 = 0x3333333333333333;
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;
int z, i, j;
ulong tmp;
i = 0;
for (z=0; z<BITS; z++) {
tmp = b->t[z];
if (!tmp)
continue;
tmp = (tmp&m1) + (tmp>> 1&m1);
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;
for (j=0; j<64; j++) {
i += 1 & tmp;
tmp >>= 1;
}
}
return i;
}