popcnt_compat.go 532 B

123456789101112131415161718
  1. //go:build !go1.9
  2. // +build !go1.9
  3. package roaring
  4. // bit population count, take from
  5. // https://code.google.com/p/go/issues/detail?id=4988#c11
  6. // credit: https://code.google.com/u/arnehormann/
  7. // credit: https://play.golang.org/p/U7SogJ7psJ
  8. // credit: http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
  9. func popcount(x uint64) uint64 {
  10. x -= (x >> 1) & 0x5555555555555555
  11. x = (x>>2)&0x3333333333333333 + x&0x3333333333333333
  12. x += x >> 4
  13. x &= 0x0f0f0f0f0f0f0f0f
  14. x *= 0x0101010101010101
  15. return x >> 56
  16. }