-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
range.go
348 lines (318 loc) · 8.02 KB
/
range.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright (c) Roman Atachiants and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package bitmap
import (
"unsafe"
"github.com/kelindar/simd"
)
const full = 0xffffffffffffffff
// Range iterates over all of the bits set to one in this bitmap.
func (dst Bitmap) Range(fn func(x uint32)) {
for blkAt := 0; blkAt < len(dst); blkAt++ {
blk := (dst)[blkAt]
if blk == 0x0 {
continue // Skip the empty page
}
// Iterate in a 4-bit chunks so we can reduce the number of function calls and skip
// the bits for which we should not call our range function.
offset := uint32(blkAt << 6)
for ; blk > 0; blk = blk >> 4 {
switch blk & 0b1111 {
case 0b0001:
fn(offset + 0)
case 0b0010:
fn(offset + 1)
case 0b0011:
fn(offset + 0)
fn(offset + 1)
case 0b0100:
fn(offset + 2)
case 0b0101:
fn(offset + 0)
fn(offset + 2)
case 0b0110:
fn(offset + 1)
fn(offset + 2)
case 0b0111:
fn(offset + 0)
fn(offset + 1)
fn(offset + 2)
case 0b1000:
fn(offset + 3)
case 0b1001:
fn(offset + 0)
fn(offset + 3)
case 0b1010:
fn(offset + 1)
fn(offset + 3)
case 0b1011:
fn(offset + 0)
fn(offset + 1)
fn(offset + 3)
case 0b1100:
fn(offset + 2)
fn(offset + 3)
case 0b1101:
fn(offset + 0)
fn(offset + 2)
fn(offset + 3)
case 0b1110:
fn(offset + 1)
fn(offset + 2)
fn(offset + 3)
case 0b1111:
fn(offset + 0)
fn(offset + 1)
fn(offset + 2)
fn(offset + 3)
}
offset += 4
}
}
}
// Filter predicate
type predicate = func(x uint32) byte
// Filter iterates over the bitmap elements and calls a predicate provided for each
// containing element. If the predicate returns false, the bitmap at the element's
// position is set to zero.
func (dst *Bitmap) Filter(f func(x uint32) bool) {
fn := *(*predicate)(unsafe.Pointer(&f))
for blkAt := 0; blkAt < len(*dst); blkAt++ {
blk := (*dst)[blkAt]
if blk == 0x0 {
continue // Skip the empty page
}
offset := uint32(blkAt << 6)
var mask uint64
var i uint32
// Iterate in a 4-bit chunks so we can reduce the number of function calls and skip
// the bits for which we should not call our filter function.
for ; blk > 0; blk = blk >> 4 {
switch blk & 0b1111 {
case 0b0001:
mask |= uint64(fn(offset)) << i
case 0b0010:
mask |= uint64(fn(offset+1)<<1) << i
case 0b0011:
mask |= uint64(fn(offset)|(fn(offset+1)<<1)) << i
case 0b0100:
mask |= uint64(fn(offset+2)<<2) << i
case 0b0101:
mask |= uint64(fn(offset)|fn(offset+2)<<2) << i
case 0b0110:
mask |= uint64((fn(offset+1)<<1)|(fn(offset+2)<<2)) << i
case 0b0111:
mask |= uint64(fn(offset)|(fn(offset+1)<<1)|(fn(offset+2)<<2)) << i
case 0b1000:
mask |= uint64(fn(offset+3)<<3) << i
case 0b1001:
mask |= uint64(fn(offset)|(fn(offset+3)<<3)) << i
case 0b1010:
mask |= uint64((fn(offset+1)<<1)|(fn(offset+3)<<3)) << i
case 0b1011:
mask |= uint64(fn(offset)|(fn(offset+1)<<1)|(fn(offset+3)<<3)) << i
case 0b1100:
mask |= uint64((fn(offset+2)<<2)|(fn(offset+3)<<3)) << i
case 0b1101:
mask |= uint64(fn(offset)|(fn(offset+2)<<2)|(fn(offset+3)<<3)) << i
case 0b1110:
mask |= uint64((fn(offset+1)<<1)|(fn(offset+2)<<2)|(fn(offset+3)<<3)) << i
case 0b1111:
mask |= uint64(fn(offset)|(fn(offset+1)<<1)|(fn(offset+2)<<2)|(fn(offset+3)<<3)) << i
}
i += 4
offset += 4
}
// Apply the mask
(*dst)[blkAt] &= mask
}
}
// Sum computes a horizontal sum of a slice, filtered by the provided bitmap
func Sum[T simd.Number](src []T, filter Bitmap) (sum T) {
tail := minint(len(src)/64, len(filter)) << 6 // End of 64-byte blocks
last := minint(len(src), len(filter)*64) // End of slice or mask
var frame [64]T
var i0, i1 int
for i1 = 0; i1 < tail; i1 += 64 {
switch filter[i1>>6] {
case full:
continue // Continue buffering
case 0:
default:
sum += simd.Sum(leftPack(&frame, src[i1:i1+64], filter[i1>>6]))
}
// Flush the current buffer
if (i1 - i0) > 0 {
sum += simd.Sum(src[i0:i1])
}
i0 = i1 + 64
}
// Flush the accumulated buffer so far
if (i1 - i0) > 0 {
sum += simd.Sum(src[i0:i1])
}
// Process the tail
for i := tail; i < last; i++ {
if filter.Contains(uint32(i)) {
sum += src[i]
}
}
return sum
}
// Min finds the smallest value in a slice, filtered by the provided bitmap
func Min[T simd.Number](src []T, filter Bitmap) (min T, hit bool) {
tail := minint(len(src)/64, len(filter)) << 6 // End of 64-byte blocks
last := minint(len(src), len(filter)*64) // End of slice or mask
var frame [64]T
var i0, i1 int
for i1 = 0; i1 < tail; i1 += 64 {
switch filter[i1>>6] {
case full:
continue // Continue buffering
case 0:
default:
if m := simd.Min(leftPack(&frame, src[i1:i1+64], filter[i1>>6])); m < min || !hit {
hit = true
min = m
}
}
// Flush the current buffer
if (i1 - i0) > 0 {
if m := simd.Min(src[i0:i1]); m < min || !hit {
hit = true
min = m
}
}
i0 = i1 + 64
}
// Flush the accumulated buffer so far
if (i1 - i0) > 0 {
if m := simd.Min(src[i0:i1]); m < min || !hit {
hit = true
min = m
}
}
// Process the tail
for i := tail; i < last; i++ {
if filter.Contains(uint32(i)) && (src[i] < min || !hit) {
hit = true
min = src[i]
}
}
return
}
// Max finds the largest value in a slice, filtered by the provided bitmap
func Max[T simd.Number](src []T, filter Bitmap) (max T, hit bool) {
tail := minint(len(src)/64, len(filter)) << 6 // End of 64-byte blocks
last := minint(len(src), len(filter)*64) // End of slice or mask
var frame [64]T
var i0, i1 int
for i1 = 0; i1 < tail; i1 += 64 {
switch filter[i1>>6] {
case full:
continue // Continue buffering
case 0:
default:
if m := simd.Max(leftPack(&frame, src[i1:i1+64], filter[i1>>6])); m > max || !hit {
hit = true
max = m
}
}
// Flush the current buffer
if (i1 - i0) > 0 {
if m := simd.Max(src[i0:i1]); m > max || !hit {
hit = true
max = m
}
}
i0 = i1 + 64
}
// Flush the accumulated buffer so far
if (i1 - i0) > 0 {
if m := simd.Max(src[i0:i1]); m > max || !hit {
hit = true
max = m
}
}
// Process the tail
for i := tail; i < last; i++ {
if filter.Contains(uint32(i)) && (src[i] > max || !hit) {
hit = true
max = src[i]
}
}
return
}
// leftPack left-packs a src slice into a dst for a single block blk
func leftPack[T any](dst *[64]T, src []T, blk uint64) []T {
offset := 0
cursor := 0
for ; blk > 0; blk = blk >> 4 {
switch blk & 0b1111 {
case 0b0001:
dst[cursor] = src[offset+0]
cursor += 1
case 0b0010:
dst[cursor] = src[offset+1]
cursor += 1
case 0b0011:
dst[cursor] = src[offset+0]
dst[cursor+1] = src[offset+1]
cursor += 2
case 0b0100:
dst[cursor] = src[offset+2]
cursor += 1
case 0b0101:
dst[cursor] = src[offset+0]
dst[cursor+1] = src[offset+2]
cursor += 2
case 0b0110:
dst[cursor] = src[offset+1]
dst[cursor+1] = src[offset+2]
cursor += 2
case 0b0111:
dst[cursor] = src[offset+0]
dst[cursor+1] = src[offset+1]
dst[cursor+2] = src[offset+2]
cursor += 3
case 0b1000:
dst[cursor] = src[offset+3]
cursor += 1
case 0b1001:
dst[cursor] = src[offset+0]
dst[cursor+1] = src[offset+3]
cursor += 2
case 0b1010:
dst[cursor] = src[offset+1]
dst[cursor+1] = src[offset+3]
cursor += 2
case 0b1011:
dst[cursor] = src[offset+0]
dst[cursor+1] = src[offset+1]
dst[cursor+2] = src[offset+3]
cursor += 3
case 0b1100:
dst[cursor] = src[offset+2]
dst[cursor+1] = src[offset+3]
cursor += 2
case 0b1101:
dst[cursor] = src[offset+0]
dst[cursor+1] = src[offset+2]
dst[cursor+2] = src[offset+3]
cursor += 3
case 0b1110:
dst[cursor] = src[offset+1]
dst[cursor+1] = src[offset+2]
dst[cursor+2] = src[offset+3]
cursor += 3
case 0b1111:
dst[cursor] = src[offset+0]
dst[cursor+1] = src[offset+1]
dst[cursor+2] = src[offset+2]
dst[cursor+3] = src[offset+3]
cursor += 4
}
offset += 4
}
return (*dst)[:cursor]
}