xxhash.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Package xxhash implements the 64-bit variant of xxHash (XXH64) as described
  2. // at http://cyan4973.github.io/xxHash/.
  3. // THIS IS VENDORED: Go to github.com/cespare/xxhash for original package.
  4. package xxhash
  5. import (
  6. "encoding/binary"
  7. "errors"
  8. "math/bits"
  9. )
  10. const (
  11. prime1 uint64 = 11400714785074694791
  12. prime2 uint64 = 14029467366897019727
  13. prime3 uint64 = 1609587929392839161
  14. prime4 uint64 = 9650029242287828579
  15. prime5 uint64 = 2870177450012600261
  16. )
  17. // Store the primes in an array as well.
  18. //
  19. // The consts are used when possible in Go code to avoid MOVs but we need a
  20. // contiguous array of the assembly code.
  21. var primes = [...]uint64{prime1, prime2, prime3, prime4, prime5}
  22. // Digest implements hash.Hash64.
  23. type Digest struct {
  24. v1 uint64
  25. v2 uint64
  26. v3 uint64
  27. v4 uint64
  28. total uint64
  29. mem [32]byte
  30. n int // how much of mem is used
  31. }
  32. // New creates a new Digest that computes the 64-bit xxHash algorithm.
  33. func New() *Digest {
  34. var d Digest
  35. d.Reset()
  36. return &d
  37. }
  38. // Reset clears the Digest's state so that it can be reused.
  39. func (d *Digest) Reset() {
  40. d.v1 = primes[0] + prime2
  41. d.v2 = prime2
  42. d.v3 = 0
  43. d.v4 = -primes[0]
  44. d.total = 0
  45. d.n = 0
  46. }
  47. // Size always returns 8 bytes.
  48. func (d *Digest) Size() int { return 8 }
  49. // BlockSize always returns 32 bytes.
  50. func (d *Digest) BlockSize() int { return 32 }
  51. // Write adds more data to d. It always returns len(b), nil.
  52. func (d *Digest) Write(b []byte) (n int, err error) {
  53. n = len(b)
  54. d.total += uint64(n)
  55. memleft := d.mem[d.n&(len(d.mem)-1):]
  56. if d.n+n < 32 {
  57. // This new data doesn't even fill the current block.
  58. copy(memleft, b)
  59. d.n += n
  60. return
  61. }
  62. if d.n > 0 {
  63. // Finish off the partial block.
  64. c := copy(memleft, b)
  65. d.v1 = round(d.v1, u64(d.mem[0:8]))
  66. d.v2 = round(d.v2, u64(d.mem[8:16]))
  67. d.v3 = round(d.v3, u64(d.mem[16:24]))
  68. d.v4 = round(d.v4, u64(d.mem[24:32]))
  69. b = b[c:]
  70. d.n = 0
  71. }
  72. if len(b) >= 32 {
  73. // One or more full blocks left.
  74. nw := writeBlocks(d, b)
  75. b = b[nw:]
  76. }
  77. // Store any remaining partial block.
  78. copy(d.mem[:], b)
  79. d.n = len(b)
  80. return
  81. }
  82. // Sum appends the current hash to b and returns the resulting slice.
  83. func (d *Digest) Sum(b []byte) []byte {
  84. s := d.Sum64()
  85. return append(
  86. b,
  87. byte(s>>56),
  88. byte(s>>48),
  89. byte(s>>40),
  90. byte(s>>32),
  91. byte(s>>24),
  92. byte(s>>16),
  93. byte(s>>8),
  94. byte(s),
  95. )
  96. }
  97. // Sum64 returns the current hash.
  98. func (d *Digest) Sum64() uint64 {
  99. var h uint64
  100. if d.total >= 32 {
  101. v1, v2, v3, v4 := d.v1, d.v2, d.v3, d.v4
  102. h = rol1(v1) + rol7(v2) + rol12(v3) + rol18(v4)
  103. h = mergeRound(h, v1)
  104. h = mergeRound(h, v2)
  105. h = mergeRound(h, v3)
  106. h = mergeRound(h, v4)
  107. } else {
  108. h = d.v3 + prime5
  109. }
  110. h += d.total
  111. b := d.mem[:d.n&(len(d.mem)-1)]
  112. for ; len(b) >= 8; b = b[8:] {
  113. k1 := round(0, u64(b[:8]))
  114. h ^= k1
  115. h = rol27(h)*prime1 + prime4
  116. }
  117. if len(b) >= 4 {
  118. h ^= uint64(u32(b[:4])) * prime1
  119. h = rol23(h)*prime2 + prime3
  120. b = b[4:]
  121. }
  122. for ; len(b) > 0; b = b[1:] {
  123. h ^= uint64(b[0]) * prime5
  124. h = rol11(h) * prime1
  125. }
  126. h ^= h >> 33
  127. h *= prime2
  128. h ^= h >> 29
  129. h *= prime3
  130. h ^= h >> 32
  131. return h
  132. }
  133. const (
  134. magic = "xxh\x06"
  135. marshaledSize = len(magic) + 8*5 + 32
  136. )
  137. // MarshalBinary implements the encoding.BinaryMarshaler interface.
  138. func (d *Digest) MarshalBinary() ([]byte, error) {
  139. b := make([]byte, 0, marshaledSize)
  140. b = append(b, magic...)
  141. b = appendUint64(b, d.v1)
  142. b = appendUint64(b, d.v2)
  143. b = appendUint64(b, d.v3)
  144. b = appendUint64(b, d.v4)
  145. b = appendUint64(b, d.total)
  146. b = append(b, d.mem[:d.n]...)
  147. b = b[:len(b)+len(d.mem)-d.n]
  148. return b, nil
  149. }
  150. // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
  151. func (d *Digest) UnmarshalBinary(b []byte) error {
  152. if len(b) < len(magic) || string(b[:len(magic)]) != magic {
  153. return errors.New("xxhash: invalid hash state identifier")
  154. }
  155. if len(b) != marshaledSize {
  156. return errors.New("xxhash: invalid hash state size")
  157. }
  158. b = b[len(magic):]
  159. b, d.v1 = consumeUint64(b)
  160. b, d.v2 = consumeUint64(b)
  161. b, d.v3 = consumeUint64(b)
  162. b, d.v4 = consumeUint64(b)
  163. b, d.total = consumeUint64(b)
  164. copy(d.mem[:], b)
  165. d.n = int(d.total % uint64(len(d.mem)))
  166. return nil
  167. }
  168. func appendUint64(b []byte, x uint64) []byte {
  169. var a [8]byte
  170. binary.LittleEndian.PutUint64(a[:], x)
  171. return append(b, a[:]...)
  172. }
  173. func consumeUint64(b []byte) ([]byte, uint64) {
  174. x := u64(b)
  175. return b[8:], x
  176. }
  177. func u64(b []byte) uint64 { return binary.LittleEndian.Uint64(b) }
  178. func u32(b []byte) uint32 { return binary.LittleEndian.Uint32(b) }
  179. func round(acc, input uint64) uint64 {
  180. acc += input * prime2
  181. acc = rol31(acc)
  182. acc *= prime1
  183. return acc
  184. }
  185. func mergeRound(acc, val uint64) uint64 {
  186. val = round(0, val)
  187. acc ^= val
  188. acc = acc*prime1 + prime4
  189. return acc
  190. }
  191. func rol1(x uint64) uint64 { return bits.RotateLeft64(x, 1) }
  192. func rol7(x uint64) uint64 { return bits.RotateLeft64(x, 7) }
  193. func rol11(x uint64) uint64 { return bits.RotateLeft64(x, 11) }
  194. func rol12(x uint64) uint64 { return bits.RotateLeft64(x, 12) }
  195. func rol18(x uint64) uint64 { return bits.RotateLeft64(x, 18) }
  196. func rol23(x uint64) uint64 { return bits.RotateLeft64(x, 23) }
  197. func rol27(x uint64) uint64 { return bits.RotateLeft64(x, 27) }
  198. func rol31(x uint64) uint64 { return bits.RotateLeft64(x, 31) }