You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

popcnt.go 1020B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package bitset
  2. // bit population count, take from
  3. // https://code.google.com/p/go/issues/detail?id=4988#c11
  4. // credit: https://code.google.com/u/arnehormann/
  5. func popcount(x uint64) (n uint64) {
  6. x -= (x >> 1) & 0x5555555555555555
  7. x = (x>>2)&0x3333333333333333 + x&0x3333333333333333
  8. x += x >> 4
  9. x &= 0x0f0f0f0f0f0f0f0f
  10. x *= 0x0101010101010101
  11. return x >> 56
  12. }
  13. func popcntSliceGo(s []uint64) uint64 {
  14. cnt := uint64(0)
  15. for _, x := range s {
  16. cnt += popcount(x)
  17. }
  18. return cnt
  19. }
  20. func popcntMaskSliceGo(s, m []uint64) uint64 {
  21. cnt := uint64(0)
  22. for i := range s {
  23. cnt += popcount(s[i] &^ m[i])
  24. }
  25. return cnt
  26. }
  27. func popcntAndSliceGo(s, m []uint64) uint64 {
  28. cnt := uint64(0)
  29. for i := range s {
  30. cnt += popcount(s[i] & m[i])
  31. }
  32. return cnt
  33. }
  34. func popcntOrSliceGo(s, m []uint64) uint64 {
  35. cnt := uint64(0)
  36. for i := range s {
  37. cnt += popcount(s[i] | m[i])
  38. }
  39. return cnt
  40. }
  41. func popcntXorSliceGo(s, m []uint64) uint64 {
  42. cnt := uint64(0)
  43. for i := range s {
  44. cnt += popcount(s[i] ^ m[i])
  45. }
  46. return cnt
  47. }