-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.go
104 lines (84 loc) · 2.08 KB
/
utils.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
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
package bp128
import (
"reflect"
"unsafe"
)
// MakeAlignedSlice loads a 16-byte aligned slice
// to dst. dst must be a pointer to an integer slice.
func MakeAlignedSlice(length int, dst interface{}) {
intSize := 0
switch dst.(type) {
case *[]int, *[]uint, *[]int64, *[]uint64:
intSize = 64
case *[]int32, *[]uint32:
intSize = 32
case *[]int16, *[]uint16:
intSize = 16
case *[]int8, *[]uint8:
intSize = 8
default:
panic("bp128: dst is not a pointer to integer slice")
}
padding := (addrAlignment * 8) / intSize
c := length + padding
vdst := reflect.ValueOf(dst).Elem()
vslice := reflect.MakeSlice(vdst.Type(), c, c)
idx := 0
addr := unsafe.Pointer(vslice.Pointer())
for !isAligned(intSize, uintptr(addr), idx) {
idx++
}
vdst.Set(vslice.Slice(idx, idx+length))
}
func isAligned(intSize int, addr uintptr, index int) bool {
addr += uintptr(index * (intSize / 8))
return addr&(addrAlignment-1) == 0
}
func makeAlignedBytes(length int) []byte {
if length == 0 {
return nil
}
out := []byte{}
MakeAlignedSlice(length, &out)
return out
}
func alignSlice(intSize int, v reflect.Value) reflect.Value {
padding := (addrAlignment * 8) / intSize
nslice := v
length := v.Len() + padding
if v.Cap() < length {
nslice = reflect.MakeSlice(v.Type(), length, length)
}
idx := 0
addr := unsafe.Pointer(nslice.Pointer())
for !isAligned(intSize, uintptr(addr), idx) {
idx++
}
return reflect.AppendSlice(nslice.Slice(idx, idx), v)
}
func convertToBytes(intSize int, v reflect.Value) []byte {
if !v.IsValid() {
return nil
}
nbytes := intSize / 8
sh := &reflect.SliceHeader{}
sh.Cap = v.Cap() * nbytes
sh.Len = v.Len() * nbytes
sh.Data = v.Pointer()
return *(*[]uint8)(unsafe.Pointer(sh))
}
func appendBytes(intSize int, v reflect.Value, b []byte) reflect.Value {
length := (len(b) * 8) / intSize
sh := &reflect.SliceHeader{}
sh.Cap = length
sh.Len = length
sh.Data = uintptr(unsafe.Pointer(&b[0]))
nslice := reflect.NewAt(v.Type(), unsafe.Pointer(sh)).Elem()
return reflect.AppendSlice(v, nslice)
}
func min(x, y int) int {
if x < y {
return x
}
return y
}