1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
//go:build runtime_memhash_tsip
// This is the tsip hash developed by Damian Gryski, based on ideas from SipHash.
// It is slower than leveldb's hash, but should be "stronger".
// https://en.wikipedia.org/wiki/SipHash
// https://github.com/veorq/SipHash
// https://github.com/dgryski/tsip
package runtime
import (
"internal/binary"
"math/bits"
"unsafe"
)
type sip struct {
v0, v1 uint64
}
func (s *sip) round() {
s.v0 += s.v1
s.v1 = bits.RotateLeft64(s.v1, 13) ^ s.v0
s.v0 = bits.RotateLeft64(s.v0, 35) + s.v1
s.v1 = bits.RotateLeft64(s.v1, 17) ^ s.v0
s.v0 = bits.RotateLeft64(s.v0, 21)
}
func hash64(ptr unsafe.Pointer, n uintptr, seed uintptr) uint64 {
p := unsafe.Slice((*byte)(ptr), n)
k0 := uint64(seed)
k1 := uint64(0)
s := sip{
v0: k0 ^ 0x736f6d6570736575,
v1: k1 ^ 0x646f72616e646f6d,
}
b := uint64(len(p)) << 56
for len(p) >= 8 {
m := binary.LittleEndian.Uint64(p[:8])
s.v1 ^= m
s.round()
s.v0 ^= m
p = p[8:]
}
switch len(p) {
case 7:
b |= uint64(p[6]) << 48
fallthrough
case 6:
b |= uint64(p[5]) << 40
fallthrough
case 5:
b |= uint64(p[4]) << 32
fallthrough
case 4:
b |= uint64(p[3]) << 24
fallthrough
case 3:
b |= uint64(p[2]) << 16
fallthrough
case 2:
b |= uint64(p[1]) << 8
fallthrough
case 1:
b |= uint64(p[0])
}
// last block
s.v1 ^= b
s.round()
s.v0 ^= b
// finalization
s.v1 ^= 0xff
s.round()
s.v1 = bits.RotateLeft64(s.v1, 32)
s.round()
s.v1 = bits.RotateLeft64(s.v1, 32)
return s.v0 ^ s.v1
}
type sip32 struct {
v0, v1 uint32
}
func (s *sip32) round() {
s.v0 += s.v1
s.v1 = bits.RotateLeft32(s.v1, 5) ^ s.v0
s.v0 = bits.RotateLeft32(s.v0, 8) + s.v1
s.v1 = bits.RotateLeft32(s.v1, 13) ^ s.v0
s.v0 = bits.RotateLeft32(s.v0, 7)
}
func hash32(ptr unsafe.Pointer, n uintptr, seed uintptr) uint32 {
p := unsafe.Slice((*byte)(ptr), n)
k0 := uint32(seed)
k1 := uint32(seed >> 32)
s := sip32{
v0: k0 ^ 0x74656462,
v1: k1 ^ 0x6c796765,
}
b := uint32(len(p)) << 24
for len(p) >= 4 {
m := binary.LittleEndian.Uint32(p[:4])
s.v1 ^= m
s.round()
s.v0 ^= m
p = p[4:]
}
switch len(p) {
case 3:
b |= uint32(p[2]) << 16
fallthrough
case 2:
b |= uint32(p[1]) << 8
fallthrough
case 1:
b |= uint32(p[0])
}
// last block
s.v1 ^= b
s.round()
s.v0 ^= b
// finalization
s.v1 ^= 0xff
s.round()
s.v1 = bits.RotateLeft32(s.v1, 16)
s.round()
s.v1 = bits.RotateLeft32(s.v1, 16)
return s.v0 ^ s.v1
}
|