-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
bitvector.c
202 lines (171 loc) · 4.29 KB
/
bitvector.c
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
// This file is a part of Julia. License is MIT: http:https://julialang.org/license
/*
bit vector primitives
*/
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "dtypes.h"
#include "bitvector.h"
#ifdef _OS_WINDOWS_
#include <malloc.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
u_int32_t *bitvector_resize(u_int32_t *b, uint64_t oldsz, uint64_t newsz,
int initzero)
{
u_int32_t *p;
size_t sz = ((newsz+31)>>5) * sizeof(uint32_t);
p = (u_int32_t*)LLT_REALLOC(b, sz);
if (p == NULL) return NULL;
if (initzero && newsz>oldsz) {
size_t osz = ((oldsz+31)>>5) * sizeof(uint32_t);
memset(&p[osz/sizeof(uint32_t)], 0, sz-osz);
}
return p;
}
u_int32_t *bitvector_new(u_int64_t n, int initzero)
{
return bitvector_resize(NULL, 0, n, initzero);
}
size_t bitvector_nwords(u_int64_t nbits)
{
return ((nbits+31)>>5);
}
void bitvector_set(u_int32_t *b, u_int64_t n, u_int32_t c)
{
if (c)
b[n>>5] |= (1<<(n&31));
else
b[n>>5] &= ~(1<<(n&31));
}
u_int32_t bitvector_get(u_int32_t *b, u_int64_t n)
{
return b[n>>5] & (1<<(n&31));
}
// a mask with n set lo or hi bits
#define lomask(n) (u_int32_t)((((u_int32_t)1)<<(n))-1)
#define ONES32 ((u_int32_t)0xffffffff)
#if defined(__INTEL_COMPILER) && !defined(__clang__)
#define count_bits(b) _popcnt32(b)
#else
STATIC_INLINE u_int32_t count_bits(u_int32_t b)
{
b = b - ((b>>1)&0x55555555);
b = ((b>>2)&0x33333333) + (b&0x33333333);
b = ((b>>4)+b)&0x0f0f0f0f;
b += (b>>8);
b += (b>>16);
return b & 0x3f;
// here is the non-optimized version, for clarity:
/*
b = ((b>> 1)&0x55555555) + (b&0x55555555);
b = ((b>> 2)&0x33333333) + (b&0x33333333);
b = ((b>> 4)&0x0f0f0f0f) + (b&0x0f0f0f0f);
b = ((b>> 8)&0x00ff00ff) + (b&0x00ff00ff);
b = ((b>>16)&0x0000ffff) + (b&0x0000ffff);
return b & 0x3f;
*/
}
#endif
static int ntz(uint32_t x)
{
int n;
if (x == 0) return 32;
n = 1;
if ((x & 0x0000FFFF) == 0) {n = n +16; x = x >>16;}
if ((x & 0x000000FF) == 0) {n = n + 8; x = x >> 8;}
if ((x & 0x0000000F) == 0) {n = n + 4; x = x >> 4;}
if ((x & 0x00000003) == 0) {n = n + 2; x = x >> 2;}
return n - (x & 1);
}
// given a bitvector of n bits, starting at bit n0 find the next
// set bit, including n0.
// returns n if no set bits.
uint64_t bitvector_next(uint32_t *b, uint64_t n0, uint64_t n)
{
if (n0 >= n) return n;
uint32_t i = n0>>5;
uint32_t nb = n0&31;
uint32_t nw = (n+31)>>5;
uint32_t w;
if (i < nw-1 || (n&31)==0)
w = b[i]>>nb;
else
w = (b[i]&lomask(n&31))>>nb;
if (w != 0)
return ntz(w)+n0;
if (i == nw-1)
return n;
i++;
while (i < nw-1) {
w = b[i];
if (w != 0) {
return ntz(w) + (((uint64_t)i)<<5);
}
i++;
}
w = b[i];
nb = n&31;
i = ntz(w);
if (nb == 0)
return i + (n-32);
if (i >= nb)
return n;
return i + (n-nb);
}
u_int64_t bitvector_count(u_int32_t *b, u_int64_t offs, u_int64_t nbits)
{
size_t i, nw;
u_int32_t ntail;
u_int64_t ans;
if (nbits == 0) return 0;
nw = (offs+nbits+31)>>5;
if (nw == 1) {
if (nbits == 32)
return count_bits(b[0] & (ONES32<<offs));
return count_bits(b[0] & (lomask(nbits)<<offs));
}
ans = count_bits(b[0]>>offs); // first end cap
for(i=1; i < nw-1; i++) {
ans += count_bits(b[i]);
}
ntail = (offs+nbits)&31;
ans += count_bits(b[i]&(ntail>0?lomask(ntail):ONES32)); // last end cap
return ans;
}
u_int32_t bitvector_any1(u_int32_t *b, u_int64_t offs, u_int64_t nbits)
{
index_t i;
u_int32_t nw, tail;
u_int32_t mask;
if (nbits == 0) return 0;
nw = (offs+nbits+31)>>5;
if (nw == 1) {
if (nbits == 32)
mask = (ONES32<<offs);
else
mask = (lomask(nbits)<<offs);
if ((b[0] & mask) != 0) return 1;
return 0;
}
mask = ~lomask(offs);
if ((b[0] & mask) != 0) return 1;
for(i=1; i < nw-1; i++) {
if (b[i] != 0) return 1;
}
tail = (offs+nbits)&31;
if (tail==0) {
if (b[i] != 0) return 1;
}
else {
mask = lomask(tail);
if ((b[i] & mask) != 0) return 1;
}
return 0;
}
#ifdef __cplusplus
}
#endif