-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
bitmap_amd64.go
146 lines (133 loc) · 3.82 KB
/
bitmap_amd64.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
// Copyright (c) Roman Atachiants and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
//go:build !noasm && amd64
package bitmap
import "unsafe"
// And computes the intersection between two bitmaps and stores the result in the current bitmap
func (dst *Bitmap) And(other Bitmap, extra ...Bitmap) {
max := minlen(*dst, other, extra)
dst.shrink(max)
if max == 0 {
return
}
switch hardware {
case isAccelerated:
switch len(extra) {
case 0:
_and(unsafe.Pointer(&(*dst)[0]), unsafe.Pointer(&other[0]), uint64(max))
default:
vx, _ := pointersOf(other, extra)
_and_many(unsafe.Pointer(&(*dst)[0]), vx, dimensionsOf(max, len(extra)+1))
}
case isAVX512:
switch len(extra) {
case 0:
_and_avx512(unsafe.Pointer(&(*dst)[0]), unsafe.Pointer(&other[0]), uint64(max))
default:
vx, _ := pointersOf(other, extra)
_and_many_avx512(unsafe.Pointer(&(*dst)[0]), vx, dimensionsOf(max, len(extra)+1))
}
default:
and(*dst, max, other, extra)
return
}
}
// AndNot computes the difference between two bitmaps and stores the result in the current bitmap.
// Operation works as set subtract: dst - b
func (dst *Bitmap) AndNot(other Bitmap, extra ...Bitmap) {
max := minlen(*dst, other, extra)
if max == 0 {
return
}
switch hardware {
case isAccelerated:
switch len(extra) {
case 0:
_andn(unsafe.Pointer(&(*dst)[0]), unsafe.Pointer(&other[0]), uint64(max))
default:
vx, _ := pointersOf(other, extra)
_andn_many(unsafe.Pointer(&(*dst)[0]), vx, dimensionsOf(max, len(extra)+1))
}
case isAVX512:
switch len(extra) {
case 0:
_andn_avx512(unsafe.Pointer(&(*dst)[0]), unsafe.Pointer(&other[0]), uint64(max))
default:
vx, _ := pointersOf(other, extra)
_andn_many_avx512(unsafe.Pointer(&(*dst)[0]), vx, dimensionsOf(max, len(extra)+1))
}
default:
andn(*dst, max, other, extra)
return
}
}
// Or computes the union between two bitmaps and stores the result in the current bitmap
func (dst *Bitmap) Or(other Bitmap, extra ...Bitmap) {
max := maxlen(*dst, other, extra)
if max == 0 {
return
}
dst.grow(max - 1)
switch hardware {
case isAccelerated:
switch len(extra) {
case 0:
_or(unsafe.Pointer(&(*dst)[0]), unsafe.Pointer(&other[0]), uint64(len(other)))
default:
vx, max := pointersOf(other, extra)
_or_many(unsafe.Pointer(&(*dst)[0]), vx, dimensionsOf(max, len(extra)+1))
}
case isAVX512:
switch len(extra) {
case 0:
_or_avx512(unsafe.Pointer(&(*dst)[0]), unsafe.Pointer(&other[0]), uint64(len(other)))
default:
vx, max := pointersOf(other, extra)
_or_many_avx512(unsafe.Pointer(&(*dst)[0]), vx, dimensionsOf(max, len(extra)+1))
}
default:
or(*dst, other, extra)
}
}
// Xor computes the symmetric difference between two bitmaps and stores the result in the current bitmap
func (dst *Bitmap) Xor(other Bitmap, extra ...Bitmap) {
max := maxlen(*dst, other, extra)
if max == 0 {
return
}
dst.grow(max - 1)
switch hardware {
case isAccelerated:
switch len(extra) {
case 0:
_xor(unsafe.Pointer(&(*dst)[0]), unsafe.Pointer(&other[0]), uint64(len(other)))
default:
vx, max := pointersOf(other, extra)
_xor_many(unsafe.Pointer(&(*dst)[0]), vx, dimensionsOf(max, len(extra)+1))
}
case isAVX512:
switch len(extra) {
case 0:
_xor_avx512(unsafe.Pointer(&(*dst)[0]), unsafe.Pointer(&other[0]), uint64(len(other)))
default:
vx, max := pointersOf(other, extra)
_xor_many_avx512(unsafe.Pointer(&(*dst)[0]), vx, dimensionsOf(max, len(extra)+1))
}
default:
xor(*dst, other, extra)
}
}
// Count returns the number of elements in this bitmap
func (dst Bitmap) Count() int {
if len(dst) == 0 {
return 0
}
switch hardware {
case isAccelerated:
var res uint64
_count(unsafe.Pointer(&dst[0]), uint64(len(dst)), unsafe.Pointer(&res))
return int(res)
default:
return count(dst)
}
}