xxhash_other.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // +build !amd64 appengine !gc purego
  2. package xxhash
  3. // Sum64 computes the 64-bit xxHash digest of b.
  4. func Sum64(b []byte) uint64 {
  5. // A simpler version would be
  6. // d := New()
  7. // d.Write(b)
  8. // return d.Sum64()
  9. // but this is faster, particularly for small inputs.
  10. n := len(b)
  11. var h uint64
  12. if n >= 32 {
  13. v1 := prime1v + prime2
  14. v2 := prime2
  15. v3 := uint64(0)
  16. v4 := -prime1v
  17. for len(b) >= 32 {
  18. v1 = round(v1, u64(b[0:8:len(b)]))
  19. v2 = round(v2, u64(b[8:16:len(b)]))
  20. v3 = round(v3, u64(b[16:24:len(b)]))
  21. v4 = round(v4, u64(b[24:32:len(b)]))
  22. b = b[32:len(b):len(b)]
  23. }
  24. h = rol1(v1) + rol7(v2) + rol12(v3) + rol18(v4)
  25. h = mergeRound(h, v1)
  26. h = mergeRound(h, v2)
  27. h = mergeRound(h, v3)
  28. h = mergeRound(h, v4)
  29. } else {
  30. h = prime5
  31. }
  32. h += uint64(n)
  33. i, end := 0, len(b)
  34. for ; i+8 <= end; i += 8 {
  35. k1 := round(0, u64(b[i:i+8:len(b)]))
  36. h ^= k1
  37. h = rol27(h)*prime1 + prime4
  38. }
  39. if i+4 <= end {
  40. h ^= uint64(u32(b[i:i+4:len(b)])) * prime1
  41. h = rol23(h)*prime2 + prime3
  42. i += 4
  43. }
  44. for ; i < end; i++ {
  45. h ^= uint64(b[i]) * prime5
  46. h = rol11(h) * prime1
  47. }
  48. h ^= h >> 33
  49. h *= prime2
  50. h ^= h >> 29
  51. h *= prime3
  52. h ^= h >> 32
  53. return h
  54. }
  55. func writeBlocks(d *Digest, b []byte) int {
  56. v1, v2, v3, v4 := d.v1, d.v2, d.v3, d.v4
  57. n := len(b)
  58. for len(b) >= 32 {
  59. v1 = round(v1, u64(b[0:8:len(b)]))
  60. v2 = round(v2, u64(b[8:16:len(b)]))
  61. v3 = round(v3, u64(b[16:24:len(b)]))
  62. v4 = round(v4, u64(b[24:32:len(b)]))
  63. b = b[32:len(b):len(b)]
  64. }
  65. d.v1, d.v2, d.v3, d.v4 = v1, v2, v3, v4
  66. return n - len(b)
  67. }