10
c1_128 = 0x87c37b91114253d5
11
c2_128 = 0x4cf5ad432745937f
14
// Make sure interfaces are correctly implemented.
16
_ hash.Hash = new(digest128)
17
_ Hash128 = new(digest128)
18
_ bmixer = new(digest128)
21
// Hack: the standard api doesn't define any Hash128 interface.
22
type Hash128 interface {
24
Sum128() (uint64, uint64)
27
// digest128 represents a partial evaluation of a 128 bites hash.
28
type digest128 struct {
30
h1 uint64 // Unfinalized running hash part 1.
31
h2 uint64 // Unfinalized running hash part 2.
34
func New128() Hash128 {
41
func (d *digest128) Size() int { return 16 }
43
func (d *digest128) reset() { d.h1, d.h2 = 1, 1 }
45
func (d *digest128) Sum(b []byte) []byte {
48
byte(h1>>56), byte(h1>>48), byte(h1>>40), byte(h1>>32),
49
byte(h1>>24), byte(h1>>16), byte(h1>>8), byte(h1),
51
byte(h2>>56), byte(h2>>48), byte(h2>>40), byte(h2>>32),
52
byte(h2>>24), byte(h2>>16), byte(h2>>8), byte(h2),
56
func (d *digest128) bmix(p []byte) (tail []byte) {
59
nblocks := len(p) / 16
60
for i := 0; i < nblocks; i++ {
61
t := (*[2]uint64)(unsafe.Pointer(&p[i*16]))
65
k1 = (k1 << 31) | (k1 >> 33) // rotl64(k1, 31)
69
h1 = (h1 << 27) | (h1 >> 37) // rotl64(h1, 27)
71
h1 = h1*5 + 0x52dce729
74
k2 = (k2 << 33) | (k2 >> 31) // rotl64(k2, 33)
78
h2 = (h2 << 31) | (h2 >> 33) // rotl64(h2, 31)
80
h2 = h2*5 + 0x38495ab5
83
return p[nblocks*d.Size():]
86
func (d *digest128) Sum128() (h1, h2 uint64) {
91
switch len(d.tail) & 15 {
93
k2 ^= uint64(d.tail[14]) << 48
96
k2 ^= uint64(d.tail[13]) << 40
99
k2 ^= uint64(d.tail[12]) << 32
102
k2 ^= uint64(d.tail[11]) << 24
105
k2 ^= uint64(d.tail[10]) << 16
108
k2 ^= uint64(d.tail[9]) << 8
111
k2 ^= uint64(d.tail[8]) << 0
114
k2 = (k2 << 33) | (k2 >> 31) // rotl64(k2, 33)
121
k1 ^= uint64(d.tail[7]) << 56
124
k1 ^= uint64(d.tail[6]) << 48
127
k1 ^= uint64(d.tail[5]) << 40
130
k1 ^= uint64(d.tail[4]) << 32
133
k1 ^= uint64(d.tail[3]) << 24
136
k1 ^= uint64(d.tail[2]) << 16
139
k1 ^= uint64(d.tail[1]) << 8
142
k1 ^= uint64(d.tail[0]) << 0
144
k1 = (k1 << 31) | (k1 >> 33) // rotl64(k1, 31)
164
func fmix64(k uint64) uint64 {
166
k *= 0xff51afd7ed558ccd
168
k *= 0xc4ceb9fe1a85ec53
174
func rotl64(x uint64, r byte) uint64 {
175
return (x << r) | (x >> (64 - r))
179
// Sum128 returns the MurmurHash3 sum of data. It is equivalent to the
180
// following sequence (without the extra burden and the extra allocation):
181
// hasher := New128()
182
// hasher.Write(data)
183
// return hasher.Sum128()
184
func Sum128(data []byte) (h1 uint64, h2 uint64) {
185
d := &digest128{h1: 1, h2: 1}
186
d.tail = d.bmix(data)