| 1 |
package farm |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/binary" |
| 5 |
"math/bits" |
| 6 |
) |
| 7 |
|
| 8 |
func uoH(x, y, mul uint64, r uint) uint64 { |
| 9 |
a := (x ^ y) * mul |
| 10 |
a ^= (a >> 47) |
| 11 |
b := (y ^ a) * mul |
| 12 |
return bits.RotateLeft64(b, -int(r)) * mul |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
func Hash64WithSeeds(s []byte, seed0, seed1 uint64) uint64 { |
| 17 |
slen := len(s) |
| 18 |
if slen <= 64 { |
| 19 |
return naHash64WithSeeds(s, seed0, seed1) |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
x := seed0 |
| 25 |
y := seed1*k2 + 113 |
| 26 |
z := shiftMix(y*k2) * k2 |
| 27 |
v := uint128{seed0, seed1} |
| 28 |
var w uint128 |
| 29 |
u := x - z |
| 30 |
x *= k2 |
| 31 |
mul := k2 + (u & 0x82) |
| 32 |
|
| 33 |
|
| 34 |
endIdx := ((slen - 1) / 64) * 64 |
| 35 |
last64Idx := endIdx + ((slen - 1) & 63) - 63 |
| 36 |
last64 := s[last64Idx:] |
| 37 |
|
| 38 |
for len(s) > 64 { |
| 39 |
a0 := binary.LittleEndian.Uint64(s[0 : 0+8]) |
| 40 |
a1 := binary.LittleEndian.Uint64(s[8 : 8+8]) |
| 41 |
a2 := binary.LittleEndian.Uint64(s[16 : 16+8]) |
| 42 |
a3 := binary.LittleEndian.Uint64(s[24 : 24+8]) |
| 43 |
a4 := binary.LittleEndian.Uint64(s[32 : 32+8]) |
| 44 |
a5 := binary.LittleEndian.Uint64(s[40 : 40+8]) |
| 45 |
a6 := binary.LittleEndian.Uint64(s[48 : 48+8]) |
| 46 |
a7 := binary.LittleEndian.Uint64(s[56 : 56+8]) |
| 47 |
x += a0 + a1 |
| 48 |
y += a2 |
| 49 |
z += a3 |
| 50 |
v.lo += a4 |
| 51 |
v.hi += a5 + a1 |
| 52 |
w.lo += a6 |
| 53 |
w.hi += a7 |
| 54 |
|
| 55 |
x = bits.RotateLeft64(x, -26) |
| 56 |
x *= 9 |
| 57 |
y = bits.RotateLeft64(y, -29) |
| 58 |
z *= mul |
| 59 |
v.lo = bits.RotateLeft64(v.lo, -33) |
| 60 |
v.hi = bits.RotateLeft64(v.hi, -30) |
| 61 |
w.lo ^= x |
| 62 |
w.lo *= 9 |
| 63 |
z = bits.RotateLeft64(z, -32) |
| 64 |
z += w.hi |
| 65 |
w.hi += z |
| 66 |
z *= 9 |
| 67 |
u, y = y, u |
| 68 |
|
| 69 |
z += a0 + a6 |
| 70 |
v.lo += a2 |
| 71 |
v.hi += a3 |
| 72 |
w.lo += a4 |
| 73 |
w.hi += a5 + a6 |
| 74 |
x += a1 |
| 75 |
y += a7 |
| 76 |
|
| 77 |
y += v.lo |
| 78 |
v.lo += x - y |
| 79 |
v.hi += w.lo |
| 80 |
w.lo += v.hi |
| 81 |
w.hi += x - y |
| 82 |
x += w.hi |
| 83 |
w.hi = bits.RotateLeft64(w.hi, -34) |
| 84 |
u, z = z, u |
| 85 |
s = s[64:] |
| 86 |
} |
| 87 |
|
| 88 |
s = last64 |
| 89 |
u *= 9 |
| 90 |
v.hi = bits.RotateLeft64(v.hi, -28) |
| 91 |
v.lo = bits.RotateLeft64(v.lo, -20) |
| 92 |
w.lo += (uint64(slen-1) & 63) |
| 93 |
u += y |
| 94 |
y += u |
| 95 |
x = bits.RotateLeft64(y-x+v.lo+binary.LittleEndian.Uint64(s[8:8+8]), -37) * mul |
| 96 |
y = bits.RotateLeft64(y^v.hi^binary.LittleEndian.Uint64(s[48:48+8]), -42) * mul |
| 97 |
x ^= w.hi * 9 |
| 98 |
y += v.lo + binary.LittleEndian.Uint64(s[40:40+8]) |
| 99 |
z = bits.RotateLeft64(z+w.lo, -33) * mul |
| 100 |
v.lo, v.hi = weakHashLen32WithSeeds(s, v.hi*mul, x+w.lo) |
| 101 |
w.lo, w.hi = weakHashLen32WithSeeds(s[32:], z+w.hi, y+binary.LittleEndian.Uint64(s[16:16+8])) |
| 102 |
return uoH(hashLen16Mul(v.lo+x, w.lo^y, mul)+z-u, |
| 103 |
uoH(v.hi+y, w.hi+z, k2, 30)^x, |
| 104 |
k2, |
| 105 |
31) |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
func Hash64WithSeed(s []byte, seed uint64) uint64 { |
| 110 |
if len(s) <= 64 { |
| 111 |
return naHash64WithSeed(s, seed) |
| 112 |
} |
| 113 |
return Hash64WithSeeds(s, 0, seed) |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
func uoHash64(s []byte) uint64 { |
| 118 |
if len(s) <= 64 { |
| 119 |
return naHash64(s) |
| 120 |
} |
| 121 |
return Hash64WithSeeds(s, 81, 0) |
| 122 |
} |