forked from kelindar/bitmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simd_generic.go
67 lines (56 loc) · 1.58 KB
/
simd_generic.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
// 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 "math/bits"
// Count counts the number of bits set to one
func count(arr []uint64) int {
sum := 0
for i := 0; i < len(arr); i++ {
sum += bits.OnesCount64(arr[i])
}
return sum
}
// and computes the intersection of multiple bitmaps
func and(a Bitmap, upper int, other Bitmap, extra []Bitmap) {
for i := 0; i < upper; i++ {
a[i] = a[i] & other[i]
}
for _, b := range extra {
for i := 0; i < upper; i++ {
a[i] = a[i] & b[i]
}
}
}
// AndNot computes the difference between two bitmaps and stores the result in the current bitmap
func andn(a Bitmap, upper int, other Bitmap, extra []Bitmap) {
for i := 0; i < upper; i++ {
a[i] = a[i] &^ other[i]
}
for _, b := range extra {
for i := 0; i < upper; i++ {
a[i] = a[i] &^ b[i]
}
}
}
// or computes the union between two bitmaps and stores the result in the current bitmap
func or(a Bitmap, other Bitmap, extra []Bitmap) {
for i := 0; i < len(other); i++ {
a[i] = a[i] | other[i]
}
for _, b := range extra {
for i := 0; i < len(b); i++ {
a[i] = a[i] | b[i]
}
}
}
// Xor computes the symmetric difference between two bitmaps and stores the result in the current bitmap
func xor(a Bitmap, other Bitmap, extra []Bitmap) {
for i := 0; i < len(other); i++ {
a[i] = a[i] ^ other[i]
}
for _, b := range extra {
for i := 0; i < len(b); i++ {
a[i] = a[i] ^ b[i]
}
}
}