blob: c68d13f89e92e951e8f94d2887dedc49124d740d [file] [log] [blame]
Matteo Scandolo2e6f1e32020-04-15 11:28:45 -07001// +build !amd64 appengine !gc purego
2
3package xxhash
4
5// Sum64 computes the 64-bit xxHash digest of b.
6func Sum64(b []byte) uint64 {
7 // A simpler version would be
8 // x := New()
9 // x.Write(b)
10 // return x.Sum64()
11 // but this is faster, particularly for small inputs.
12
13 n := len(b)
14 var h uint64
15
16 if n >= 32 {
17 v1 := prime1v + prime2
18 v2 := prime2
19 v3 := uint64(0)
20 v4 := -prime1v
21 for len(b) >= 32 {
22 v1 = round(v1, u64(b[0:8:len(b)]))
23 v2 = round(v2, u64(b[8:16:len(b)]))
24 v3 = round(v3, u64(b[16:24:len(b)]))
25 v4 = round(v4, u64(b[24:32:len(b)]))
26 b = b[32:len(b):len(b)]
27 }
28 h = rol1(v1) + rol7(v2) + rol12(v3) + rol18(v4)
29 h = mergeRound(h, v1)
30 h = mergeRound(h, v2)
31 h = mergeRound(h, v3)
32 h = mergeRound(h, v4)
33 } else {
34 h = prime5
35 }
36
37 h += uint64(n)
38
39 i, end := 0, len(b)
40 for ; i+8 <= end; i += 8 {
41 k1 := round(0, u64(b[i:i+8:len(b)]))
42 h ^= k1
43 h = rol27(h)*prime1 + prime4
44 }
45 if i+4 <= end {
46 h ^= uint64(u32(b[i:i+4:len(b)])) * prime1
47 h = rol23(h)*prime2 + prime3
48 i += 4
49 }
50 for ; i < end; i++ {
51 h ^= uint64(b[i]) * prime5
52 h = rol11(h) * prime1
53 }
54
55 h ^= h >> 33
56 h *= prime2
57 h ^= h >> 29
58 h *= prime3
59 h ^= h >> 32
60
61 return h
62}
63
64func writeBlocks(x *xxh, b []byte) []byte {
65 v1, v2, v3, v4 := x.v1, x.v2, x.v3, x.v4
66 for len(b) >= 32 {
67 v1 = round(v1, u64(b[0:8:len(b)]))
68 v2 = round(v2, u64(b[8:16:len(b)]))
69 v3 = round(v3, u64(b[16:24:len(b)]))
70 v4 = round(v4, u64(b[24:32:len(b)]))
71 b = b[32:len(b):len(b)]
72 }
73 x.v1, x.v2, x.v3, x.v4 = v1, v2, v3, v4
74 return b
75}