// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/bitops.c
// Function: redisPopcount
size_t redisPopcount(void *s, long count) {
    size_t bits = 0;
    unsigned char *p = s;
    uint32_t *p4;
    // 通过查表来计算，对于 1 字节所能表示的值来说
    // 这些值的二进制表示所带有的 1 的数量
    // 比如整数 3 的二进制表示 0011 ，带有两个 1
    // 正好是查表 bitsinbyte[3] == 2
    static const unsigned char bitsinbyte[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};

    /* Count initial bytes not aligned to 32 bit. */
    while((unsigned long)p & 3 && count) {
        bits += bitsinbyte[*p++];
        count--;
    }

    /* Count bits 16 bytes at a time */
    // 每次统计 16 字节
    // 关于这里所使用的优化算法，可以参考：
    // http://yesteapea.wordpress.com/2013/03/03/counting-the-number-of-set-bits-in-an-integer/
    p4 = (uint32_t*)p;
    while(count>=16) {
        uint32_t aux1, aux2, aux3, aux4;

        aux1 = *p4++;
        aux2 = *p4++;
        aux3 = *p4++;
        aux4 = *p4++;
        count -= 16;

        aux1 = aux1 - ((aux1 >> 1) & 0x55555555);
        aux1 = (aux1 & 0x33333333) + ((aux1 >> 2) & 0x33333333);
        aux2 = aux2 - ((aux2 >> 1) & 0x55555555);
        aux2 = (aux2 & 0x33333333) + ((aux2 >> 2) & 0x33333333);
        aux3 = aux3 - ((aux3 >> 1) & 0x55555555);
        aux3 = (aux3 & 0x33333333) + ((aux3 >> 2) & 0x33333333);
        aux4 = aux4 - ((aux4 >> 1) & 0x55555555);
        aux4 = (aux4 & 0x33333333) + ((aux4 >> 2) & 0x33333333);
        bits += ((((aux1 + (aux1 >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24) +
                ((((aux2 + (aux2 >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24) +
                ((((aux3 + (aux3 >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24) +
                ((((aux4 + (aux4 >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24);
    }

    /* Count the remaining bytes. */
    // 不足 16 字节的，剩下的每个字节通过查表来完成
    p = (unsigned char*)p4;
    while(count--) bits += bitsinbyte[*p++];
    return bits;
}

// Assembly:
testb $0x3, %dil
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
leaq 0x4574a(%rip), %rcx     # 0x9ebd0
jne 0x594b8
leaq 0x1(%rdi), %rdx
xorl %eax, %eax
movq %rsi, %r8
movq %rax, %rsi
movzbl (%rdi), %eax
incq %rdi
movzbl (%rax,%rcx), %eax
addq %rsi, %rax
leaq -0x1(%r8), %rsi
testb $0x3, %dl
je 0x594ba
incq %rdx
cmpq $0x1, %r8
movq %rsi, %r8
jne 0x59491
jmp 0x594ba
xorl %eax, %eax
cmpq $0x10, %rsi
jl 0x5955f
movdqa 0x456c4(%rip), %xmm0    # 0x9eb90
movdqa 0x456cc(%rip), %xmm1    # 0x9eba0
movdqa 0x456d4(%rip), %xmm2    # 0x9ebb0
pxor %xmm3, %xmm3
leaq -0x10(%rsi), %rdx
movdqu (%rdi), %xmm4
addq $0x10, %rdi
movdqa %xmm4, %xmm5
psrlw $0x1, %xmm5
pand %xmm0, %xmm5
psubb %xmm5, %xmm4
movdqa %xmm4, %xmm5
pand %xmm1, %xmm5
psrlw $0x2, %xmm4
pand %xmm1, %xmm4
paddb %xmm5, %xmm4
movdqa %xmm4, %xmm5
psrlw $0x4, %xmm5
paddb %xmm4, %xmm5
pand %xmm2, %xmm5
movdqa %xmm5, %xmm4
punpckhdq %xmm3, %xmm4    # xmm4 = xmm4[2],xmm3[2],xmm4[3],xmm3[3]
psadbw %xmm3, %xmm4
punpckldq %xmm3, %xmm5    # xmm5 = xmm5[0],xmm3[0],xmm5[1],xmm3[1]
psadbw %xmm3, %xmm5
packuswb %xmm4, %xmm5
packuswb %xmm4, %xmm4
paddd %xmm5, %xmm4
pshufd $0x55, %xmm4, %xmm5     # xmm5 = xmm4[1,1,1,1]
paddd %xmm4, %xmm5
movd %xmm5, %r8d
addq %r8, %rax
cmpq $0x1f, %rsi
movq %rdx, %rsi
ja 0x594e0
jmp 0x59562
movq %rsi, %rdx
testq %rdx, %rdx
je 0x5957e
xorl %esi, %esi
movzbl (%rdi,%rsi), %r8d
movzbl (%r8,%rcx), %r8d
addq %r8, %rax
incq %rsi
cmpq %rsi, %rdx
jne 0x59569
retq