blob: 4a5a821603e5b8d876d07219882c5e127619f1a3 [file] [log] [blame]
Joey Armstronge8c091f2023-01-17 16:56:26 -05001// +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 // d := New()
9 // d.Write(b)
10 // return d.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(d *Digest, b []byte) int {
65 v1, v2, v3, v4 := d.v1, d.v2, d.v3, d.v4
66 n := len(b)
67 for len(b) >= 32 {
68 v1 = round(v1, u64(b[0:8:len(b)]))
69 v2 = round(v2, u64(b[8:16:len(b)]))
70 v3 = round(v3, u64(b[16:24:len(b)]))
71 v4 = round(v4, u64(b[24:32:len(b)]))
72 b = b[32:len(b):len(b)]
73 }
74 d.v1, d.v2, d.v3, d.v4 = v1, v2, v3, v4
75 return n - len(b)
76}