Figure 5-16. Alternative Population Count Code Sequence
|
|
C Source Code
|
|
|
int nbits(unsigned int x)
|
|
|
{
|
|
|
static unsigned char popcnt[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};
|
|
|
int count0, count1, count2, count3;
|
|
|
|
|
|
|
|
count0 = popcnt[(x & 0x000000FF)];
|
|
|
count1 = popcnt[((x >> 8) & 0x000000FF)];
|
|
|
count2 = popcnt[((x >> 16) & 0x000000FF)];
|
|
|
count3 = popcnt[((x >> 24) & 0x000000FF)];
|
|
|
return(count0 + count1 + count2 + count3);
|
|
|
}
|
|
|
|
|
|
|
|
Assembly Code
|
|
|
# R3 contains x
|
|
|
lwz
|
R2
|
# load R2 with address of POPCNT array
|
|
|
|
|
|
|
|
andi
|
R4,R3,0x00FF
|
# extract & right-justify byte 3
|
|
|
rlwinm
|
R5,R3,24,24,31
|
# extract & right-justify byte 2
|
|
|
lbzx
|
R4,R2,R4
|
# popcnt[byte 3]
|
|
|
rlwinm
|
R6,R3,16,24,31
|
# extract & right-justify byte 1
|
|
|
lbzx
|
R5,R2,R5
|
# popcnt[byte 2]
|
|
|
rlwinm
|
R7,R3, 8,24,31
|
# extract & right-justify byte 0
|
|
|
lbzx
|
R6,R2,R6
|
# popcnt[byte 1]
|
|
|
lbzx
|
R7,R2,R7
|
# popcnt[byte 0]
|
|
|
|
|
|
|
|
add
|
R3,R4,R5
|
#
|
|
|
add
|
R4,R6,R7
|
# accumulate result into R3 and return
|
|
|
add
|
R3,R3,R4
|
#
|