Skip to content

Commit 1cc1cec

Browse files
committed
Newer version of bitCount()
This updates to "version 3" of Jörg Arndt's 32-bit bitcount function, according to http://www.jjj.de/bitwizardry/files/bitcount.h
1 parent e98f2a3 commit 1cc1cec

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

trie/ctrie/ctrie.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -759,11 +759,11 @@ func flagPos(hashcode uint32, lev uint, bmp uint32) (uint32, uint32) {
759759
}
760760

761761
func bitCount(x uint32) uint32 {
762-
x = ((x >> 1) & 0x55555555) + (x & 0x55555555)
762+
x -= (x >> 1) & 0x55555555
763763
x = ((x >> 2) & 0x33333333) + (x & 0x33333333)
764-
x = ((x >> 4) & 0x0f0f0f0f) + (x & 0x0f0f0f0f)
765-
x = ((x >> 8) & 0x00ff00ff) + (x & 0x00ff00ff)
766-
return ((x >> 16) & 0x0000ffff) + (x & 0x0000ffff)
764+
x = ((x >> 4) + x) & 0x0f0f0f0f
765+
x *= 0x01010101
766+
return x >> 24
767767
}
768768

769769
// gcas is a generation-compare-and-swap which has semantics similar to RDCSS,

0 commit comments

Comments
 (0)