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.
 
 

229 lines
5.0 KiB

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