xxhash.go 5.5 KB

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