// File: /workspace/github_c_makefile_star2_O2/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:
xorl %eax, %eax
leaq 0x3fec7(%rip), %rcx     # 0x94b90
movq %rdi, %rdx
movq %rsi, %r8
addq $-0x1, %r8
setb %r9b
testb $0x3, %dil
je 0x54d86
testb %r9b, %r9b
je 0x54d86
movzbl (%rdx), %esi
incq %rdx
movzbl (%rsi,%rcx), %esi
addq %rsi, %rax
incq %rdi
movq %r8, %rsi
jmp 0x54ccf
addq $-0x10, %rsi
movdqu (%rdx), %xmm0
addq $0x10, %rdx
movdqa %xmm0, %xmm1
psrlw $0x1, %xmm1
pand 0x3fe34(%rip), %xmm1    # 0x94b50
psubb %xmm1, %xmm0
movdqa 0x3fe38(%rip), %xmm1    # 0x94b60
movdqa %xmm0, %xmm2
pand %xmm1, %xmm2
psrlw $0x2, %xmm0
pand %xmm1, %xmm0
paddb %xmm2, %xmm0
movdqa %xmm0, %xmm1
psrlw $0x4, %xmm1
paddb %xmm0, %xmm1
pand 0x3fe1e(%rip), %xmm1    # 0x94b70
pxor %xmm0, %xmm0
movdqa %xmm1, %xmm2
punpckhdq %xmm0, %xmm2    # xmm2 = xmm2[2],xmm0[2],xmm2[3],xmm0[3]
psadbw %xmm0, %xmm2
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
psadbw %xmm0, %xmm1
packuswb %xmm2, %xmm1
packuswb %xmm2, %xmm2
paddd %xmm1, %xmm2
pshufd $0x55, %xmm2, %xmm0     # xmm0 = xmm2[1,1,1,1]
paddd %xmm2, %xmm0
movd %xmm0, %edi
addq %rdi, %rax
cmpq $0xf, %rsi
jg 0x54cff
xorl %edi, %edi
cmpq %rdi, %rsi
je 0x54da9
movzbl (%rdx,%rdi), %r8d
movzbl (%r8,%rcx), %r8d
addq %r8, %rax
incq %rdi
jmp 0x54d92
retq