summaryrefslogtreecommitdiff
path: root/compiler_and_linker/unsorted/BitVectors.c
blob: 3a928fd33fe131fec71de6c44ea6c82c8ccb84cd (plain)
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
#include "compiler/BitVectors.h"

void bitvectorcopy(UInt32 *dst, UInt32 *src, int len) {
    int i = (len + 31) >> 5;

    while (i--)
        *(dst++) = *(src++);
}

int bitvectorchanged(UInt32 *dst, UInt32 *src, int len) {
    int i = (len + 31) >> 5;
    int flag = 0;
    UInt32 v;

    while (i--) {
        v = *src;
        if (*dst != v)
            flag = 1;
        *dst = v;
        dst++;
        src++;
    }

    return flag;
}

void bitvectorinitialize(UInt32 *vec, int len, UInt32 initval) {
    int i = (len + 31) >> 5;

    while (i--)
        *(vec++) = initval;
}

void bitvectorintersect(UInt32 *dst, UInt32 *src, int len) {
    int i = (len + 31) >> 5;

    while (i--)
        *(dst++) &= *(src++);
}

void bitvectorunion(UInt32 *dst, UInt32 *src, int len) {
    int i = (len + 31) >> 5;

    while (i--)
        *(dst++) |= *(src++);
}

void bitvectordifference(UInt32 *dst, UInt32 *src, int len) {
    int i = (len + 31) >> 5;

    while (i--)
        *(dst++) &= ~*(src++);
}

void bitvectorcomplement(UInt32 *dst, UInt32 *src, int len) {
    int i = (len + 31) >> 5;

    while (i--)
        *(dst++) = ~*(src++);
}

int bitvectorcount(UInt32 *vec, int len) {
    static unsigned char nbits[] = {
        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
    };

    UInt32 v;
    int i = (len + 31) >> 5;
    int total = 0;

    while (i--) {
        if ((v = *vec)) {
            total += nbits[v & 0xFF];
            total += nbits[(v >> 8) & 0xFF];
            total += nbits[(v >> 16) & 0xFF];
            total += nbits[(v >> 24) & 0xFF];
        }
        vec++;
    }

    return total;
}

int bitvectorisempty(UInt32 *vec, int len) {
    int i = (len + 31) >> 5;

    while (i--) {
        if (*vec)
            return 0;
        vec++;
    }

    return 1;
}

int bitvectorintersectionisempty(UInt32 *a, UInt32 *b, int len) {
    int i = (len + 31) >> 5;

    while (i--) {
        if (*a & *b)
            return 0;
        a++;
        b++;
    }

    return 1;
}