File: BitSet.cpp

package info (click to toggle)
intel-graphics-compiler2 2.20.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 107,552 kB
  • sloc: cpp: 807,012; lisp: 287,936; ansic: 16,397; python: 4,010; yacc: 2,588; lex: 1,666; pascal: 313; sh: 186; makefile: 37
file content (244 lines) | stat: -rw-r--r-- 6,804 bytes parent folder | download | duplicates (2)
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
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#include "Assertions.h"
#include "BitSet.h"

void BitSet::create(unsigned size) {
  const unsigned newArraySize =
      (size + NUM_BITS_PER_ELT - 1) / NUM_BITS_PER_ELT;
  const unsigned oldArraySize =
      (m_Size + NUM_BITS_PER_ELT - 1) / NUM_BITS_PER_ELT;
  const unsigned numBitsLeft = size % NUM_BITS_PER_ELT;

  if (size == 0) {
    free(m_BitSetArray);
    m_BitSetArray = nullptr;
    m_Size = 0;
    return;
  }

  if (newArraySize == oldArraySize) {
    // same array size, zero out the unused bits if necessary
    m_Size = size;
    if (newArraySize && numBitsLeft != 0) {
      m_BitSetArray[newArraySize - 1] &= _BIT(numBitsLeft) - 1;
    }
  } else {
    BITSET_ARRAY_TYPE *ptr =
        (BITSET_ARRAY_TYPE *)malloc(newArraySize * sizeof(BITSET_ARRAY_TYPE));

    if (ptr) {
      if (m_BitSetArray) {
        if (newArraySize > oldArraySize) {
          // copy entire old array over, set uninitialized bits to zero
          memcpy_s(ptr, newArraySize * sizeof(BITSET_ARRAY_TYPE), m_BitSetArray,
                   oldArraySize * sizeof(BITSET_ARRAY_TYPE));
          memset(ptr + oldArraySize, 0,
                 (newArraySize - oldArraySize) * sizeof(BITSET_ARRAY_TYPE));
        } else {
          // copy old array up to the size of new array, zero out the unused
          // bits
          memcpy_s(ptr, newArraySize * sizeof(BITSET_ARRAY_TYPE), m_BitSetArray,
                   newArraySize * sizeof(BITSET_ARRAY_TYPE));
          if (numBitsLeft != 0) {
            ptr[newArraySize - 1] &= _BIT(numBitsLeft) - 1;
          }
        }
      } else {
        memset(ptr, 0, newArraySize * sizeof(BITSET_ARRAY_TYPE));
      }

      free(m_BitSetArray);

      m_BitSetArray = ptr;
      m_Size = size;
    } else {
      vASSERT(false);
    }
  }
}

void BitSet::setAll(void) {
  if (m_BitSetArray) {
    unsigned index = m_Size / NUM_BITS_PER_ELT;
    std::fill_n(m_BitSetArray, index, ~(BITSET_ARRAY_TYPE)0);

    // do the leftover bits, make sure we don't change the values of the unused
    // bits, so isEmpty() can be implemented faster
    int numBitsLeft = m_Size % NUM_BITS_PER_ELT;
    if (numBitsLeft) {
      m_BitSetArray[index] = _BIT(numBitsLeft) - 1;
    }
  }
}

void BitSet::invert(void) {
  if (m_BitSetArray) {
    unsigned index;
    for (index = 0; index < m_Size / NUM_BITS_PER_ELT; index++) {
      m_BitSetArray[index] = ~m_BitSetArray[index];
    }

    // do the leftover bits
    int numBitsLeft = m_Size % NUM_BITS_PER_ELT;
    if (numBitsLeft) {
      m_BitSetArray[index] = ~m_BitSetArray[index] & (_BIT(numBitsLeft) - 1);
    }
  }
}

template <typename T>
void vector_and(T *__restrict p1, const T *const p2, unsigned n) {
  for (unsigned i = 0; i < n; ++i) {
    p1[i] &= p2[i];
  }
}

template <typename T>
void vector_or(T *__restrict p1, const T *const p2, unsigned n) {
  for (unsigned i = 0; i < n; ++i) {
    p1[i] |= p2[i];
  }
}

template <typename T>
void vector_minus(T *__restrict p1, const T *const p2, unsigned n) {
  for (unsigned i = 0; i < n; ++i) {
    p1[i] &= ~p2[i];
  }
}

BitSet &BitSet::operator|=(const BitSet &other) {
  unsigned size = other.m_Size;

  // grow the set to the size of the other set if necessary
  if (m_Size < other.m_Size) {
    create(other.m_Size);
    size = m_Size;
  }

  unsigned arraySize = (size + NUM_BITS_PER_ELT - 1) / NUM_BITS_PER_ELT;
  vector_or(m_BitSetArray, other.m_BitSetArray, arraySize);

  return *this;
}

BitSet &BitSet::operator-=(const BitSet &other) {
  // do not grow the set for subtract
  unsigned size = m_Size < other.m_Size ? m_Size : other.m_Size;
  unsigned arraySize = (size + NUM_BITS_PER_ELT - 1) / NUM_BITS_PER_ELT;
  vector_minus(m_BitSetArray, other.m_BitSetArray, arraySize);
  return *this;
}

BitSet &BitSet::operator&=(const BitSet &other) {
  // do not grow the set for and
  unsigned size = m_Size < other.m_Size ? m_Size : other.m_Size;
  unsigned arraySize = (size + NUM_BITS_PER_ELT - 1) / NUM_BITS_PER_ELT;
  vector_and(m_BitSetArray, other.m_BitSetArray, arraySize);

  // zero out the leftover bits if there are any
  unsigned myArraySize = (m_Size + NUM_BITS_PER_ELT - 1) / NUM_BITS_PER_ELT;
  for (unsigned i = arraySize; i < myArraySize; i++) {
    m_BitSetArray[i] = 0;
  }

  return *this;
}

// Create a bitmask with the N right-most bits set to 1, and all other bits set
// to 0.
static BITSET_ARRAY_TYPE maskTrailingOnes(unsigned n) {
  vASSERT(n <= NUM_BITS_PER_ELT);
  return n == 0 ? 0 : (BITSET_ARRAY_TYPE(-1) >> (NUM_BITS_PER_ELT - n));
}

// Create a bitmask with the N right-most bits set to 0, and all other bits set
// to 1.
static BITSET_ARRAY_TYPE maskTrailingZeros(unsigned n) {
  return ~maskTrailingOnes(n);
}

// TODO: Use c++20 bit manipulation utility functions.
static unsigned countTrailingZeros(BITSET_ARRAY_TYPE val) {
  vASSERT(val != 0);
  unsigned count = 0;
  while ((val & 1) == 0) {
    val >>= 1;
    ++count;
  }
  return count;
}

static unsigned countLeadingZeros(BITSET_ARRAY_TYPE val) {
  vASSERT(val != 0);
  unsigned count = 0;
  while ((val & (1 << (NUM_BITS_PER_ELT - 1))) == 0) {
    val <<= 1;
    ++count;
  }
  return count;
}

int BitSet::findFirstIn(unsigned begin, unsigned end) const {
  vASSERT(begin <= end && end <= m_Size);
  if (begin == end)
    return -1;

  unsigned firstElt = begin / NUM_BITS_PER_ELT;
  unsigned lastElt = (end - 1) / NUM_BITS_PER_ELT;

  for (unsigned i = firstElt; i <= lastElt; ++i) {
    auto elt = getElt(i);

    if (i == firstElt) {
      unsigned firstBit = begin % NUM_BITS_PER_ELT;
      elt &= maskTrailingZeros(firstBit);
    }

    if (i == lastElt) {
      unsigned lastBit = (end - 1) % NUM_BITS_PER_ELT;
      elt &= maskTrailingOnes(lastBit + 1);
    }

    if (elt != 0)
      return i * NUM_BITS_PER_ELT + countTrailingZeros(elt);
  }

  return -1;
}

int BitSet::findLastIn(unsigned begin, unsigned end) const {
  vASSERT(begin <= end && end <= m_Size);
  if (begin == end)
    return -1;

  unsigned lastElt = (end - 1) / NUM_BITS_PER_ELT;
  unsigned firstElt = begin / NUM_BITS_PER_ELT;

  for (unsigned i = lastElt + 1; i >= firstElt + 1; --i) {
    unsigned currentElt = i - 1;
    auto elt = getElt(currentElt);

    if (currentElt == lastElt) {
      unsigned lastBit = (end - 1) % NUM_BITS_PER_ELT;
      elt &= maskTrailingOnes(lastBit + 1);
    }

    if (currentElt == firstElt) {
      unsigned firstBit = begin % NUM_BITS_PER_ELT;
      elt &= maskTrailingZeros(firstBit);
    }

    if (elt != 0)
      return (currentElt + 1) * NUM_BITS_PER_ELT - countLeadingZeros(elt) - 1;
  }

  return -1;
}