File: bitarray.c

package info (click to toggle)
indigo 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 48,936 kB
  • sloc: ansic: 332,816; cpp: 169,470; python: 20,033; java: 13,701; cs: 9,979; asm: 8,475; sql: 6,743; xml: 6,354; javascript: 1,245; sh: 555; php: 506; makefile: 54
file content (367 lines) | stat: -rw-r--r-- 11,654 bytes parent folder | download
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/****************************************************************************
 * Copyright (C) from 2009 to Present EPAM Systems.
 *
 * This file is part of Indigo toolkit.
 *
 * Licensed 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.
 ***************************************************************************/

#include <stdlib.h>
#include <string.h>

#include "base_c/bitarray.h"

int bitGetBit(const void* bitarray, int bitno)
{
    return ((((char*)bitarray)[bitno / 8] & (char)(1 << (bitno % 8))) == 0) ? 0 : 1;
}

void bitSetBit(void* bitarray, int bitno, int value)
{
    if (value)
        ((char*)bitarray)[bitno / 8] |= (1 << (bitno % 8));
    else
        ((char*)bitarray)[bitno / 8] &= ~(1 << (bitno % 8));
}

void bitFlipBit(void* bitarray, int bitno)
{
    ((char*)bitarray)[bitno / 8] ^= (1 << (bitno % 8));
}

int bitTestEquality(const void* bits1, const void* bits2, int nbits)
{
    char* chars1 = (char*)bits1;
    char* chars2 = (char*)bits2;
    char mask = ~(0xFF << (nbits & 7));
    int i;

    for (i = 0; i < nbits / 8; i++)
        if (chars1[i] != chars2[i])
            return 0;

    if ((chars1[nbits / 8] & mask) != (chars2[nbits / 8] & mask))
        return 0;

    return 1;
}

int bitTestEquality_Array(const void* bits, const void* bitarray, int array_start, int nbits)
{
    int i;

    for (i = 0; i < nbits; i++)
        if (bitGetBit(bits, i) != bitGetBit(bitarray, array_start + i))
            return 0;

    return 1;
}

int bitTestEqualityByMask(const void* bits1, const void* bits2, const void* bitMaskVoid, int nbits)
{
    const char* chars1 = (const char*)bits1;
    const char* chars2 = (const char*)bits2;
    const char* bitMask = (const char*)bitMaskVoid;
    char mask = ~(0xFF << (nbits & 7));
    int i;

    for (i = 0; i < nbits / 8; i++)
        if ((chars1[i] & bitMask[i]) != (chars2[i] & bitMask[i]))
            return 0;

    if (((chars1[nbits / 8] & bitMask[nbits / 8]) & mask) != ((chars2[nbits / 8] & bitMask[nbits / 8]) & mask))
        return 0;

    return 1;
}

// res = a & (b ^ ~c)
int bitGetAandBxorNotC(const void* a, const void* b, const void* c, void* res, int nbits)
{
    const char* ac = (const char*)a;
    const char* bc = (const char*)b;
    const char* cc = (const char*)c;
    char* rc = (char*)res;
    int i;

    for (i = 0; i < nbits / 8; i++)
        rc[i] = ac[i] & (bc[i] ^ ~cc[i]);

    if ((nbits & 7) != 0)
        rc[i] = ac[i] & (bc[i] ^ ~cc[i]);

    return 1;
}

int bitGetOnesCountByte(byte value)
{
    static const int onesCount[] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2,
                                    3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3,
                                    3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5,
                                    6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4,
                                    3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4,
                                    5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6,
                                    6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8};
    return onesCount[value];
}

int bitGetOnesCountDword(dword v)
{
    // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
    v = v - ((v >> 1) & 0x55555555);                         // reuse input as temporary
    v = (v & 0x33333333) + ((v >> 2) & 0x33333333);          // temp
    return (((v + (v >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24; // count
}

int bitGetOnesCountQword(qword value)
{
    return bitGetOnesCountDword((dword)value) + bitGetOnesCountDword((dword)(value >> 32));
}

int bitGetOnesCount(const byte* data, int size)
{
    int count = 0;
    while (size-- > 0)
        count += bitGetOnesCountByte(*data++);
    return count;
}

int bitGetOneHOIndex(byte value)
{
    static const int oneHOIndex[] = {0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6,
                                     6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
                                     7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
                                     7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
                                     8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
                                     8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
                                     8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8};
    return oneHOIndex[value];
}

int bitGetOneLOIndex(byte value)
{
    static const int oneLOIndex[] = {8, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2,
                                     0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0,
                                     1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1,
                                     0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0,
                                     2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3,
                                     0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0,
                                     1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0};
    return oneLOIndex[value];
}

int bitGetSize(int nbits)
{
    return (nbits + 7) / 8;
}

int bitTestOnes(const byte* pattern, const byte* candidate, int n_bytes)
{
    int qwords_count = n_bytes / sizeof(qword);
    int bytes_left = n_bytes - qwords_count * sizeof(qword);

    const qword* pattern_ptr = (const qword*)pattern;
    const qword* candidate_ptr = (const qword*)candidate;
    while (qwords_count-- > 0)
    {
        if ((*candidate_ptr & *pattern_ptr) != *pattern_ptr)
            return 0;
        pattern_ptr++;
        candidate_ptr++;
    }

    if (bytes_left != 0)
    {
        qword mask = ~(qword)0 >> (64 - 8 * bytes_left);

        if ((*candidate_ptr & *pattern_ptr & mask) != (*pattern_ptr & mask))
            return 0;
    }
    return 1;
}

int bitIdecticalBits(const byte* bit1, const byte* bit2, int n_bytes)
{
    int qwords_count = n_bytes / sizeof(qword);
    int bytes_left = n_bytes - qwords_count * sizeof(qword);

    int count = 0;
    const qword* bit1_ptr = (const qword*)bit1;
    const qword* bit2_ptr = (const qword*)bit2;
    while (qwords_count-- > 0)
    {
        qword id = ~(*bit1_ptr ^ *bit2_ptr);
        count += bitGetOnesCountQword(id);

        bit1_ptr++;
        bit2_ptr++;
    }

    if (bytes_left != 0)
    {
        qword mask = ~(qword)0 >> (64 - 8 * bytes_left);
        qword id = (~(*bit1_ptr ^ *bit2_ptr)) & mask;
        count += bitGetOnesCountQword(id);
    }
    return count;
}

int bitCommonOnes(const byte* bit1, const byte* bit2, int n_bytes)
{
    int qwords_count = n_bytes / sizeof(qword);
    int bytes_left = n_bytes - qwords_count * sizeof(qword);

    int count = 0;
    const qword* bit1_ptr = (const qword*)bit1;
    const qword* bit2_ptr = (const qword*)bit2;
    while (qwords_count-- > 0)
    {
        qword id = *bit1_ptr & *bit2_ptr;
        count += bitGetOnesCountQword(id);

        bit1_ptr++;
        bit2_ptr++;
    }

    if (bytes_left != 0)
    {
        qword mask = ~(qword)0 >> (64 - 8 * bytes_left);
        qword id = *bit1_ptr & *bit2_ptr & mask;
        count += bitGetOnesCountQword(id);
    }
    return count;
}

int bitUniqueOnes(const byte* bit1, const byte* bit2, int n_bytes)
{
    int qwords_count = n_bytes / sizeof(qword);
    int bytes_left = n_bytes - qwords_count * sizeof(qword);

    int count = 0;
    const qword* bit1_ptr = (const qword*)bit1;
    const qword* bit2_ptr = (const qword*)bit2;
    while (qwords_count-- > 0)
    {
        qword id = *bit1_ptr & ~*bit2_ptr;
        count += bitGetOnesCountQword(id);

        bit1_ptr++;
        bit2_ptr++;
    }

    if (bytes_left != 0)
    {
        qword mask = ~(qword)0 >> (64 - 8 * bytes_left);
        qword id = *bit1_ptr & ~*bit2_ptr & mask;
        count += bitGetOnesCountQword(id);
    }
    return count;
}

int bitDifferentOnes(const byte* bit1, const byte* bit2, int n_bytes)
{
    int qwords_count = n_bytes / sizeof(qword);
    int bytes_left = n_bytes - qwords_count * sizeof(qword);

    int count = 0;
    const qword* bit1_ptr = (const qword*)bit1;
    const qword* bit2_ptr = (const qword*)bit2;
    while (qwords_count-- > 0)
    {
        qword id = *bit1_ptr ^ *bit2_ptr;
        count += bitGetOnesCount((byte*)&id, sizeof(qword));

        bit1_ptr++;
        bit2_ptr++;
    }

    if (bytes_left != 0)
    {
        qword mask = ~(qword)0 >> (64 - 8 * bytes_left);
        qword id = (*bit1_ptr ^ *bit2_ptr) & mask;
        count += bitGetOnesCountQword(id);
    }
    return count;
}

int bitUnionOnes(const byte* bit1, const byte* bit2, int n_bytes)
{
    int qwords_count = n_bytes / sizeof(qword);
    int bytes_left = n_bytes - qwords_count * sizeof(qword);

    int count = 0;
    const qword* bit1_ptr = (const qword*)bit1;
    const qword* bit2_ptr = (const qword*)bit2;
    while (qwords_count-- > 0)
    {
        qword id = *bit1_ptr | *bit2_ptr;
        count += bitGetOnesCountQword(id);

        bit1_ptr++;
        bit2_ptr++;
    }

    if (bytes_left != 0)
    {
        qword mask = ~(qword)0 >> (64 - 8 * bytes_left);
        qword id = (*bit1_ptr | *bit2_ptr) & mask;
        count += bitGetOnesCountQword(id);
    }
    return count;
}

// a &= b
void bitAnd(byte* a, const byte* b, int nbytes)
{
    while (nbytes-- > 0)
    {
        *a = *a & *b;
        a++;
        b++;
    }
}

// a |= b
void bitOr(byte* a, const byte* b, int nbytes)
{
    while (nbytes-- > 0)
    {
        *a = *a | *b;
        a++;
        b++;
    }
}

int bitIsAllZero(const void* bits, int nbytes)
{
    const byte* a = (const byte*)bits;

    while (nbytes-- > 0)
    {
        if (*a != 0)
            return 0;
        a++;
    }
    return 1;
}

int bitLog2Dword(dword input)
{
    int count = 0;
    while (input > 0)
    {
        count++;
        input >>= 1;
    }
    return count;
}