murmur_appengine.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // +build appengine
  2. package gocql
  3. import "encoding/binary"
  4. func murmur3H1(data []byte) uint64 {
  5. length := len(data)
  6. var h1, h2, k1, k2 uint64
  7. const (
  8. c1 = 0x87c37b91114253d5
  9. c2 = 0x4cf5ad432745937f
  10. )
  11. // body
  12. nBlocks := length / 16
  13. for i := 0; i < nBlocks; i++ {
  14. // block := (*[2]uint64)(unsafe.Pointer(&data[i*16]))
  15. k1 = binary.LittleEndian.Uint64(data[i*16:])
  16. k2 = binary.LittleEndian.Uint64(data[(i*16)+8:])
  17. k1 *= c1
  18. k1 = (k1 << 31) | (k1 >> 33) // ROTL64(k1, 31)
  19. k1 *= c2
  20. h1 ^= k1
  21. h1 = (h1 << 27) | (h1 >> 37) // ROTL64(h1, 27)
  22. h1 += h2
  23. h1 = h1*5 + 0x52dce729
  24. k2 *= c2
  25. k2 = (k2 << 33) | (k2 >> 31) // ROTL64(k2, 33)
  26. k2 *= c1
  27. h2 ^= k2
  28. h2 = (h2 << 31) | (h2 >> 33) // ROTL64(h2, 31)
  29. h2 += h1
  30. h2 = h2*5 + 0x38495ab5
  31. }
  32. // tail
  33. tail := data[nBlocks*16:]
  34. k1 = 0
  35. k2 = 0
  36. switch length & 15 {
  37. case 15:
  38. k2 ^= uint64(tail[14]) << 48
  39. fallthrough
  40. case 14:
  41. k2 ^= uint64(tail[13]) << 40
  42. fallthrough
  43. case 13:
  44. k2 ^= uint64(tail[12]) << 32
  45. fallthrough
  46. case 12:
  47. k2 ^= uint64(tail[11]) << 24
  48. fallthrough
  49. case 11:
  50. k2 ^= uint64(tail[10]) << 16
  51. fallthrough
  52. case 10:
  53. k2 ^= uint64(tail[9]) << 8
  54. fallthrough
  55. case 9:
  56. k2 ^= uint64(tail[8])
  57. k2 *= c2
  58. k2 = (k2 << 33) | (k2 >> 31) // ROTL64(k2, 33)
  59. k2 *= c1
  60. h2 ^= k2
  61. fallthrough
  62. case 8:
  63. k1 ^= uint64(tail[7]) << 56
  64. fallthrough
  65. case 7:
  66. k1 ^= uint64(tail[6]) << 48
  67. fallthrough
  68. case 6:
  69. k1 ^= uint64(tail[5]) << 40
  70. fallthrough
  71. case 5:
  72. k1 ^= uint64(tail[4]) << 32
  73. fallthrough
  74. case 4:
  75. k1 ^= uint64(tail[3]) << 24
  76. fallthrough
  77. case 3:
  78. k1 ^= uint64(tail[2]) << 16
  79. fallthrough
  80. case 2:
  81. k1 ^= uint64(tail[1]) << 8
  82. fallthrough
  83. case 1:
  84. k1 ^= uint64(tail[0])
  85. k1 *= c1
  86. k1 = (k1 << 31) | (k1 >> 33) // ROTL64(k1, 31)
  87. k1 *= c2
  88. h1 ^= k1
  89. }
  90. h1 ^= uint64(length)
  91. h2 ^= uint64(length)
  92. h1 += h2
  93. h2 += h1
  94. // finalizer
  95. const (
  96. fmix1 = 0xff51afd7ed558ccd
  97. fmix2 = 0xc4ceb9fe1a85ec53
  98. )
  99. // fmix64(h1)
  100. h1 ^= h1 >> 33
  101. h1 *= fmix1
  102. h1 ^= h1 >> 33
  103. h1 *= fmix2
  104. h1 ^= h1 >> 33
  105. // fmix64(h2)
  106. h2 ^= h2 >> 33
  107. h2 *= fmix1
  108. h2 ^= h2 >> 33
  109. h2 *= fmix2
  110. h2 ^= h2 >> 33
  111. h1 += h2
  112. // the following is extraneous since h2 is discarded
  113. // h2 += h1
  114. return h1
  115. }