File tree Expand file tree Collapse file tree 2 files changed +76
-2
lines changed Expand file tree Collapse file tree 2 files changed +76
-2
lines changed Original file line number Diff line number Diff line change 1
- //go:build !runtime_memhash_tsip
2
- // +build !runtime_memhash_tsip
1
+ //go:build !runtime_memhash_tsip && !runtime_memhash_leveldb
2
+ // +build !runtime_memhash_tsip,!runtime_memhash_leveldb
3
3
4
4
package runtime
5
5
Original file line number Diff line number Diff line change
1
+ //go:build runtime_memhash_leveldb
2
+ // +build runtime_memhash_leveldb
3
+
4
+ package runtime
5
+
6
+ import (
7
+ "unsafe"
8
+ )
9
+
10
+ func ptrToSlice (ptr unsafe.Pointer , n uintptr ) []byte {
11
+ var p []byte
12
+
13
+ type _bslice struct {
14
+ ptr * byte
15
+ len uintptr
16
+ cap uintptr
17
+ }
18
+
19
+ pslice := (* _bslice )(unsafe .Pointer (& p ))
20
+ pslice .ptr = (* byte )(ptr )
21
+ pslice .cap = n
22
+ pslice .len = n
23
+
24
+ return p
25
+ }
26
+
27
+ // leveldb hash
28
+ func hash32 (ptr unsafe.Pointer , n , seed uintptr ) uint32 {
29
+
30
+ const (
31
+ lseed = 0xbc9f1d34
32
+ m = 0xc6a4a793
33
+ )
34
+
35
+ b := ptrToSlice (ptr , n )
36
+
37
+ h := uint32 (lseed ^ seed ) ^ uint32 (uint (len (b ))* uint (m ))
38
+
39
+ for ; len (b ) >= 4 ; b = b [4 :] {
40
+ h += uint32 (b [0 ]) | uint32 (b [1 ])<< 8 | uint32 (b [2 ])<< 16 | uint32 (b [3 ])<< 24
41
+ h *= m
42
+ h ^= h >> 16
43
+ }
44
+ switch len (b ) {
45
+ case 3 :
46
+ h += uint32 (b [2 ]) << 16
47
+ fallthrough
48
+ case 2 :
49
+ h += uint32 (b [1 ]) << 8
50
+ fallthrough
51
+ case 1 :
52
+ h += uint32 (b [0 ])
53
+ h *= m
54
+ h ^= h >> 24
55
+ }
56
+
57
+ return h
58
+ }
59
+
60
+ func hash64finalizer (key uint64 ) uint64 {
61
+ key = ^ key + (key << 21 ) // key = (key << 21) - key - 1;
62
+ key = key ^ (key >> 24 )
63
+ key = (key + (key << 3 )) + (key << 8 ) // key * 265
64
+ key = key ^ (key >> 14 )
65
+ key = (key + (key << 2 )) + (key << 4 ) // key * 21
66
+ key = key ^ (key >> 28 )
67
+ key = key + (key << 31 )
68
+ return key
69
+ }
70
+
71
+ func hash64 (ptr unsafe.Pointer , n , seed uintptr ) uint64 {
72
+ h32 := hash32 (ptr , n , seed )
73
+ return hash64finalizer ((uint64 (h32 ^ xorshift32 (uint32 (seed ))) << 32 ) | uint64 (h32 ))
74
+ }
You can’t perform that action at this time.
0 commit comments