-
Notifications
You must be signed in to change notification settings - Fork 12
/
hash.go
75 lines (62 loc) · 1.86 KB
/
hash.go
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
// Copyright (c) 2014-2015 Utkan Güngördü <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cuckoo
// Hash is the internal hash type. Any change in its definition will require overall changes in this file.
type hash uint32
const hashBits = 32 // # of bits in hash type, at most unsafe.Sizeof(Key)*8.
const (
murmur3_c1_32 uint32 = 0xcc9e2d51
murmur3_c2_32 uint32 = 0x1b873593
)
const (
xx_prime32_1 uint32 = 2654435761
xx_prime32_2 uint32 = 2246822519
xx_prime32_3 uint32 = 3266489917
xx_prime32_4 uint32 = 668265263
xx_prime32_5 uint32 = 374761393
)
const (
mem_c0 = 2860486313
mem_c1 = 3267000013
)
func murmur3_32(k uint32, seed uint32) uint32 {
k *= murmur3_c1_32
k = (k << 15) | (k >> (32 - 15))
k *= murmur3_c2_32
h := seed
h ^= k
h = (h << 13) | (h >> (32 - 13))
h = (h<<2 + h) + 0xe6546b64
return h
}
func xx_32(k uint32, seed uint32) uint32 {
h := seed + xx_prime32_5
h += k * xx_prime32_3
h = ((h << 17) | (h >> (32 - 17))) * xx_prime32_4
h ^= h >> 15
h *= xx_prime32_2
h ^= h >> 13
h *= xx_prime32_3
h ^= h >> 16
return h
}
func mem_32(k uint32, seed uint32) uint32 {
h := k ^ mem_c0
h ^= (k & 0xff) * mem_c1
h ^= (k >> 8 & 0xff) * mem_c1
h ^= (k >> 16 & 0xff) * mem_c1
h ^= (k >> 24 & 0xff) * mem_c1
return h
}