本站源代码
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.

44 lines
1.5KB

  1. package numeric
  2. var interleaveMagic = []uint64{
  3. 0x5555555555555555,
  4. 0x3333333333333333,
  5. 0x0F0F0F0F0F0F0F0F,
  6. 0x00FF00FF00FF00FF,
  7. 0x0000FFFF0000FFFF,
  8. 0x00000000FFFFFFFF,
  9. 0xAAAAAAAAAAAAAAAA,
  10. }
  11. var interleaveShift = []uint{1, 2, 4, 8, 16}
  12. // Interleave the first 32 bits of each uint64
  13. // apdated from org.apache.lucene.util.BitUtil
  14. // which was adapted from:
  15. // http://graphics.stanford.edu/~seander/bithacks.html#InterleaveBMN
  16. func Interleave(v1, v2 uint64) uint64 {
  17. v1 = (v1 | (v1 << interleaveShift[4])) & interleaveMagic[4]
  18. v1 = (v1 | (v1 << interleaveShift[3])) & interleaveMagic[3]
  19. v1 = (v1 | (v1 << interleaveShift[2])) & interleaveMagic[2]
  20. v1 = (v1 | (v1 << interleaveShift[1])) & interleaveMagic[1]
  21. v1 = (v1 | (v1 << interleaveShift[0])) & interleaveMagic[0]
  22. v2 = (v2 | (v2 << interleaveShift[4])) & interleaveMagic[4]
  23. v2 = (v2 | (v2 << interleaveShift[3])) & interleaveMagic[3]
  24. v2 = (v2 | (v2 << interleaveShift[2])) & interleaveMagic[2]
  25. v2 = (v2 | (v2 << interleaveShift[1])) & interleaveMagic[1]
  26. v2 = (v2 | (v2 << interleaveShift[0])) & interleaveMagic[0]
  27. return (v2 << 1) | v1
  28. }
  29. // Deinterleave the 32-bit value starting at position 0
  30. // to get the other 32-bit value, shift it by 1 first
  31. func Deinterleave(b uint64) uint64 {
  32. b &= interleaveMagic[0]
  33. b = (b ^ (b >> interleaveShift[0])) & interleaveMagic[1]
  34. b = (b ^ (b >> interleaveShift[1])) & interleaveMagic[2]
  35. b = (b ^ (b >> interleaveShift[2])) & interleaveMagic[3]
  36. b = (b ^ (b >> interleaveShift[3])) & interleaveMagic[4]
  37. b = (b ^ (b >> interleaveShift[4])) & interleaveMagic[5]
  38. return b
  39. }
上海开阖软件有限公司 沪ICP备12045867号-1