xxh32zero.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Package xxh32 implements the very fast XXH hashing algorithm (32 bits version).
  2. // (https://github.com/Cyan4973/XXH/)
  3. package xxh32
  4. import (
  5. "encoding/binary"
  6. )
  7. const (
  8. prime1 uint32 = 2654435761
  9. prime2 uint32 = 2246822519
  10. prime3 uint32 = 3266489917
  11. prime4 uint32 = 668265263
  12. prime5 uint32 = 374761393
  13. primeMask = 0xFFFFFFFF
  14. prime1plus2 = uint32((uint64(prime1) + uint64(prime2)) & primeMask) // 606290984
  15. prime1minus = uint32((-int64(prime1)) & primeMask) // 1640531535
  16. )
  17. // XXHZero represents an xxhash32 object with seed 0.
  18. type XXHZero struct {
  19. v1 uint32
  20. v2 uint32
  21. v3 uint32
  22. v4 uint32
  23. totalLen uint64
  24. buf [16]byte
  25. bufused int
  26. }
  27. // Sum appends the current hash to b and returns the resulting slice.
  28. // It does not change the underlying hash state.
  29. func (xxh XXHZero) Sum(b []byte) []byte {
  30. h32 := xxh.Sum32()
  31. return append(b, byte(h32), byte(h32>>8), byte(h32>>16), byte(h32>>24))
  32. }
  33. // Reset resets the Hash to its initial state.
  34. func (xxh *XXHZero) Reset() {
  35. xxh.v1 = prime1plus2
  36. xxh.v2 = prime2
  37. xxh.v3 = 0
  38. xxh.v4 = prime1minus
  39. xxh.totalLen = 0
  40. xxh.bufused = 0
  41. }
  42. // Size returns the number of bytes returned by Sum().
  43. func (xxh *XXHZero) Size() int {
  44. return 4
  45. }
  46. // BlockSize gives the minimum number of bytes accepted by Write().
  47. func (xxh *XXHZero) BlockSize() int {
  48. return 1
  49. }
  50. // Write adds input bytes to the Hash.
  51. // It never returns an error.
  52. func (xxh *XXHZero) Write(input []byte) (int, error) {
  53. if xxh.totalLen == 0 {
  54. xxh.Reset()
  55. }
  56. n := len(input)
  57. m := xxh.bufused
  58. xxh.totalLen += uint64(n)
  59. r := len(xxh.buf) - m
  60. if n < r {
  61. copy(xxh.buf[m:], input)
  62. xxh.bufused += len(input)
  63. return n, nil
  64. }
  65. p := 0
  66. // Causes compiler to work directly from registers instead of stack:
  67. v1, v2, v3, v4 := xxh.v1, xxh.v2, xxh.v3, xxh.v4
  68. if m > 0 {
  69. // some data left from previous update
  70. copy(xxh.buf[xxh.bufused:], input[:r])
  71. xxh.bufused += len(input) - r
  72. // fast rotl(13)
  73. buf := xxh.buf[:16] // BCE hint.
  74. v1 = rol13(v1+binary.LittleEndian.Uint32(buf[:])*prime2) * prime1
  75. v2 = rol13(v2+binary.LittleEndian.Uint32(buf[4:])*prime2) * prime1
  76. v3 = rol13(v3+binary.LittleEndian.Uint32(buf[8:])*prime2) * prime1
  77. v4 = rol13(v4+binary.LittleEndian.Uint32(buf[12:])*prime2) * prime1
  78. p = r
  79. xxh.bufused = 0
  80. }
  81. for n := n - 16; p <= n; p += 16 {
  82. sub := input[p:][:16] //BCE hint for compiler
  83. v1 = rol13(v1+binary.LittleEndian.Uint32(sub[:])*prime2) * prime1
  84. v2 = rol13(v2+binary.LittleEndian.Uint32(sub[4:])*prime2) * prime1
  85. v3 = rol13(v3+binary.LittleEndian.Uint32(sub[8:])*prime2) * prime1
  86. v4 = rol13(v4+binary.LittleEndian.Uint32(sub[12:])*prime2) * prime1
  87. }
  88. xxh.v1, xxh.v2, xxh.v3, xxh.v4 = v1, v2, v3, v4
  89. copy(xxh.buf[xxh.bufused:], input[p:])
  90. xxh.bufused += len(input) - p
  91. return n, nil
  92. }
  93. // Sum32 returns the 32 bits Hash value.
  94. func (xxh *XXHZero) Sum32() uint32 {
  95. h32 := uint32(xxh.totalLen)
  96. if h32 >= 16 {
  97. h32 += rol1(xxh.v1) + rol7(xxh.v2) + rol12(xxh.v3) + rol18(xxh.v4)
  98. } else {
  99. h32 += prime5
  100. }
  101. p := 0
  102. n := xxh.bufused
  103. buf := xxh.buf
  104. for n := n - 4; p <= n; p += 4 {
  105. h32 += binary.LittleEndian.Uint32(buf[p:p+4]) * prime3
  106. h32 = rol17(h32) * prime4
  107. }
  108. for ; p < n; p++ {
  109. h32 += uint32(buf[p]) * prime5
  110. h32 = rol11(h32) * prime1
  111. }
  112. h32 ^= h32 >> 15
  113. h32 *= prime2
  114. h32 ^= h32 >> 13
  115. h32 *= prime3
  116. h32 ^= h32 >> 16
  117. return h32
  118. }
  119. // ChecksumZero returns the 32bits Hash value.
  120. func ChecksumZero(input []byte) uint32 {
  121. n := len(input)
  122. h32 := uint32(n)
  123. if n < 16 {
  124. h32 += prime5
  125. } else {
  126. v1 := prime1plus2
  127. v2 := prime2
  128. v3 := uint32(0)
  129. v4 := prime1minus
  130. p := 0
  131. for n := n - 16; p <= n; p += 16 {
  132. sub := input[p:][:16] //BCE hint for compiler
  133. v1 = rol13(v1+binary.LittleEndian.Uint32(sub[:])*prime2) * prime1
  134. v2 = rol13(v2+binary.LittleEndian.Uint32(sub[4:])*prime2) * prime1
  135. v3 = rol13(v3+binary.LittleEndian.Uint32(sub[8:])*prime2) * prime1
  136. v4 = rol13(v4+binary.LittleEndian.Uint32(sub[12:])*prime2) * prime1
  137. }
  138. input = input[p:]
  139. n -= p
  140. h32 += rol1(v1) + rol7(v2) + rol12(v3) + rol18(v4)
  141. }
  142. p := 0
  143. for n := n - 4; p <= n; p += 4 {
  144. h32 += binary.LittleEndian.Uint32(input[p:p+4]) * prime3
  145. h32 = rol17(h32) * prime4
  146. }
  147. for p < n {
  148. h32 += uint32(input[p]) * prime5
  149. h32 = rol11(h32) * prime1
  150. p++
  151. }
  152. h32 ^= h32 >> 15
  153. h32 *= prime2
  154. h32 ^= h32 >> 13
  155. h32 *= prime3
  156. h32 ^= h32 >> 16
  157. return h32
  158. }
  159. // Uint32Zero hashes x with seed 0.
  160. func Uint32Zero(x uint32) uint32 {
  161. h := prime5 + 4 + x*prime3
  162. h = rol17(h) * prime4
  163. h ^= h >> 15
  164. h *= prime2
  165. h ^= h >> 13
  166. h *= prime3
  167. h ^= h >> 16
  168. return h
  169. }
  170. func rol1(u uint32) uint32 {
  171. return u<<1 | u>>31
  172. }
  173. func rol7(u uint32) uint32 {
  174. return u<<7 | u>>25
  175. }
  176. func rol11(u uint32) uint32 {
  177. return u<<11 | u>>21
  178. }
  179. func rol12(u uint32) uint32 {
  180. return u<<12 | u>>20
  181. }
  182. func rol13(u uint32) uint32 {
  183. return u<<13 | u>>19
  184. }
  185. func rol17(u uint32) uint32 {
  186. return u<<17 | u>>15
  187. }
  188. func rol18(u uint32) uint32 {
  189. return u<<18 | u>>14
  190. }