-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
65 lines (56 loc) · 1.22 KB
/
util.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
package hyper
import (
"encoding/binary"
"reflect"
"unsafe"
"github.com/spaolacci/murmur3"
)
// extractMSB returns the leading bits
func extractMSB(bits uint64, hi uint8, lo uint8) uint64 {
m := uint64(((1 << (hi - lo)) - 1) << lo)
return (bits & m) >> lo
}
// getHashValMur2 returns the murmur3 hash
// value of byte array
func getHashValMur2(data []byte) uint64 {
hashed := murmur3.New64()
hashed.Write(data)
return hashed.Sum64()
}
// getZeroBitTail returns the count of
// consecutive tail bit of n
func getZeroBitTail(n uint64) uint8 {
var cnt uint8 = 0
for i := 0; i < bitUsed; i++ {
if n>>i&1 == 0 {
cnt++
} else {
break
}
}
return cnt
}
func uint16ToBytes(num uint16) []byte {
data := make([]byte, 2)
binary.LittleEndian.PutUint16(data, num)
return data
}
func uint32ToBytes(num uint32) []byte {
data := make([]byte, 4)
binary.LittleEndian.PutUint32(data, num)
return data
}
func uint64ToBytes(num uint64) []byte {
data := make([]byte, 8)
binary.LittleEndian.PutUint64(data, num)
return data
}
func str2Bytes(s string) []byte {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := reflect.SliceHeader{
Data: sh.Data,
Len: sh.Len,
Cap: sh.Len,
}
return *(*[]byte)(unsafe.Pointer(&bh))
}