-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathu64cmp.go
46 lines (36 loc) · 911 Bytes
/
u64cmp.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
package bitstring
import (
"bytes"
"reflect"
"unsafe"
)
// invariant: len(a) == len(b)
func u64cmp(a, b []uint64) bool {
// Above this threshold, unsafe uint64 comparison
// (cast to []byte) is faster)
const unsafe_threshold = 128
if len(a) < unsafe_threshold {
b = b[:len(a)] // remove BCE
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
}
return bytesEq(a, b)
}
func bytesEq(a, b []uint64) bool {
p := unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&a)).Data)
var aBytes []byte
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&aBytes))
hdr.Data = uintptr(p)
hdr.Len = len(a) * 8
hdr.Cap = len(a) * 8
p = unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&b)).Data)
var bBytes []byte
hdr = (*reflect.SliceHeader)(unsafe.Pointer(&bBytes))
hdr.Data = uintptr(p)
hdr.Len = len(b) * 8
hdr.Cap = len(b) * 8
return bytes.Equal(aBytes, bBytes)
}