-
Notifications
You must be signed in to change notification settings - Fork 276
/
bits.go
172 lines (151 loc) · 4.19 KB
/
bits.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Licensed to LinDB under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. LinDB licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package trie
import (
"encoding/binary"
"math/bits"
"reflect"
"unsafe"
)
const wordSize = 64
var endian = binary.LittleEndian
// A precomputed tabled containing the positions of the set bits in the binary
// representations of all 8-bit unsigned integers.
//
// For i: [0, 256) ranging over all 8-bit unsigned integers and for j: [0, 8)
// ranging over all 0-based bit positions in an 8-bit unsigned integer, the
// table entry selectInByteLut[i][j] is the 0-based bit position of the j-th set
// bit in the binary representation of i, or 8 if it has fewer than j set bits.
//
// Example: i: 17 (b00010001), j: [0, 8)
//
// selectInByteLut[b00010001][0] = 0
// selectInByteLut[b00010001][1] = 4
// selectInByteLut[b00010001][2] = 8
// ...
// selectInByteLut[b00010001][7] = 8
var selectInByteLut [256][8]uint8
func init() {
for i := 0; i < 256; i++ {
for j := 0; j < 8; j++ {
selectInByteLut[i][j] = selectInByte(i, j)
}
}
}
func findFirstSet(x int) int {
return bits.TrailingZeros64(uint64(x)) + 1
}
func selectInByte(i, j int) uint8 {
r := 0
for ; j != 0; j-- {
s := findFirstSet(i)
r += s
i >>= s
}
if i == 0 {
return 8
}
return uint8(r + findFirstSet(i) - 1)
}
func select64Broadword(x uint64, nth int64) int64 {
const (
onesStep4 = uint64(0x1111111111111111)
onesStep8 = uint64(0x0101010101010101)
msbsStep8 = uint64(0x80) * onesStep8
)
k := uint64(nth - 1)
s := x
s -= (s & (0xa * onesStep4)) >> 1
s = (s & (0x3 * onesStep4)) + ((s >> 2) & (0x3 * onesStep4))
s = (s + (s >> 4)) & (0xf * onesStep8)
byteSums := s * onesStep8
step8 := k * onesStep8
geqKStep8 := ((step8 | msbsStep8) - byteSums) & msbsStep8
place := bits.OnesCount64(geqKStep8) * 8
byteRank := k - (((byteSums << 8) >> place) & uint64(0xff))
return int64(place + int(selectInByteLut[(x>>place)&0xff][byteRank]))
}
func popcountBlock(bs []uint64, off, nbits uint32) uint32 {
if nbits == 0 {
return 0
}
lastWord := (nbits - 1) / wordSize
lastBits := (nbits - 1) % wordSize
var i, p uint32
for i = 0; i < lastWord; i++ {
p += uint32(bits.OnesCount64(bs[off+i]))
}
last := bs[off+lastWord] << (wordSize - 1 - lastBits)
return p + uint32(bits.OnesCount64(last))
}
func readBit(bs []uint64, pos uint32) bool {
wordOff := pos / wordSize
bitsOff := pos % wordSize
return bs[wordOff]&(uint64(1)<<bitsOff) != 0
}
func setBit(bs []uint64, pos uint32) {
wordOff := pos / wordSize
bitsOff := pos % wordSize
bs[wordOff] |= uint64(1) << bitsOff
}
func align(off int64) int64 {
return (off + 7) & ^int64(7)
}
func u64SliceToBytes(u []uint64) []byte {
if len(u) == 0 {
return nil
}
var b []byte
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&b))
hdr.Len = len(u) * 8
hdr.Cap = hdr.Len
hdr.Data = uintptr(unsafe.Pointer(&u[0]))
return b
}
func bytesToU64Slice(b []byte) []uint64 {
if len(b) == 0 {
return nil
}
var u32s []uint64
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&u32s))
hdr.Len = len(b) / 8
hdr.Cap = hdr.Len
hdr.Data = uintptr(unsafe.Pointer(&b[0]))
return u32s
}
func u32SliceToBytes(u []uint32) []byte {
if len(u) == 0 {
return nil
}
var b []byte
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&b))
hdr.Len = len(u) * 4
hdr.Cap = hdr.Len
hdr.Data = uintptr(unsafe.Pointer(&u[0]))
return b
}
func bytesToU32Slice(b []byte) []uint32 {
if len(b) == 0 {
return nil
}
var u32s []uint32
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&u32s))
hdr.Len = len(b) / 4
hdr.Cap = hdr.Len
hdr.Data = uintptr(unsafe.Pointer(&b[0]))
return u32s
}