diff options
author | Sun He <sunheehnus@gmail.com> | 2014-12-02 14:46:15 +0800 |
---|---|---|
committer | Sun He <sunheehnus@gmail.com> | 2014-12-02 14:46:15 +0800 |
commit | 0ec7672a5dc2966962f33d55f959ed63da450bbd (patch) | |
tree | c3f0c69180feb4033ecef4fa08f8750a741d3df4 /src/bitops.c | |
parent | 93eed9ae0163e328c33b16ab9ea3c4fbe0f98674 (diff) | |
download | redis-0ec7672a5dc2966962f33d55f959ed63da450bbd.tar.gz |
bitops.c/redisPopcount: little optimization in loop
Diffstat (limited to 'src/bitops.c')
-rw-r--r-- | src/bitops.c | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/src/bitops.c b/src/bitops.c index 94c7f3537..15ce787cf 100644 --- a/src/bitops.c +++ b/src/bitops.c @@ -70,16 +70,19 @@ size_t redisPopcount(void *s, long count) { count--; } - /* Count bits 16 bytes at a time */ + /* Count bits 28 bytes at a time */ p4 = (uint32_t*)p; - while(count>=16) { - uint32_t aux1, aux2, aux3, aux4; + while(count>=28) { + uint32_t aux1, aux2, aux3, aux4, aux5, aux6, aux7; aux1 = *p4++; aux2 = *p4++; aux3 = *p4++; aux4 = *p4++; - count -= 16; + aux5 = *p4++; + aux6 = *p4++; + aux7 = *p4++; + count -= 28; aux1 = aux1 - ((aux1 >> 1) & 0x55555555); aux1 = (aux1 & 0x33333333) + ((aux1 >> 2) & 0x33333333); @@ -89,10 +92,19 @@ size_t redisPopcount(void *s, long count) { 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); + aux5 = aux5 - ((aux5 >> 1) & 0x55555555); + aux5 = (aux5 & 0x33333333) + ((aux5 >> 2) & 0x33333333); + aux6 = aux6 - ((aux6 >> 1) & 0x55555555); + aux6 = (aux6 & 0x33333333) + ((aux6 >> 2) & 0x33333333); + aux7 = aux7 - ((aux7 >> 1) & 0x55555555); + aux7 = (aux7 & 0x33333333) + ((aux7 >> 2) & 0x33333333); + bits += ((((aux1 + (aux1 >> 4)) & 0x0F0F0F0F) + + ((aux2 + (aux2 >> 4)) & 0x0F0F0F0F) + + ((aux3 + (aux3 >> 4)) & 0x0F0F0F0F) + + ((aux4 + (aux4 >> 4)) & 0x0F0F0F0F) + + ((aux5 + (aux5 >> 4)) & 0x0F0F0F0F) + + ((aux6 + (aux6 >> 4)) & 0x0F0F0F0F) + + ((aux7 + (aux7 >> 4)) & 0x0F0F0F0F))* 0x01010101) >> 24; } /* Count the remaining bytes. */ p = (unsigned char*)p4; |