14 #ifndef LLVM_ADT_BITVECTOR_H
15 #define LLVM_ADT_BITVECTOR_H
28 typedef unsigned long BitWord;
30 enum { BITWORD_SIZE = (
unsigned)
sizeof(BitWord) * CHAR_BIT };
32 static_assert(BITWORD_SIZE == 64 || BITWORD_SIZE == 32,
33 "Unsupported word size");
52 WordRef = &b.Bits[Idx / BITWORD_SIZE];
53 BitPos = Idx % BITWORD_SIZE;
65 *WordRef |= BitWord(1) << BitPos;
67 *WordRef &= ~(BitWord(1) << BitPos);
72 return ((*WordRef) & (BitWord(1) << BitPos)) ?
true :
false;
84 explicit BitVector(
unsigned s,
bool t =
false) : Size(s) {
85 Capacity = NumBitWords(s);
86 Bits = (BitWord *)std::malloc(Capacity *
sizeof(BitWord));
87 init_words(
Bits, Capacity, t);
100 Capacity = NumBitWords(RHS.
size());
101 Bits = (BitWord *)std::malloc(Capacity *
sizeof(BitWord));
102 std::memcpy(Bits, RHS.Bits, Capacity *
sizeof(BitWord));
106 :
Bits(RHS.
Bits), Size(RHS.Size), Capacity(RHS.Capacity) {
115 bool empty()
const {
return Size == 0; }
122 unsigned NumBits = 0;
123 for (
unsigned i = 0; i < NumBitWords(
size()); ++i)
130 for (
unsigned i = 0; i < NumBitWords(
size()); ++i)
138 for (
unsigned i = 0; i < Size / BITWORD_SIZE; ++i)
143 if (
unsigned Remainder = Size % BITWORD_SIZE)
144 return Bits[Size / BITWORD_SIZE] == (1UL << Remainder) - 1;
157 for (
unsigned i = 0; i < NumBitWords(
size()); ++i)
170 unsigned WordPos = Prev / BITWORD_SIZE;
171 unsigned BitPos = Prev % BITWORD_SIZE;
172 BitWord Copy =
Bits[WordPos];
174 Copy &= ~0UL << BitPos;
180 for (
unsigned i = WordPos+1; i < NumBitWords(
size()); ++i)
193 if (N > Capacity * BITWORD_SIZE) {
194 unsigned OldCapacity = Capacity;
196 init_words(&
Bits[OldCapacity], (Capacity-OldCapacity), t);
206 unsigned OldSize = Size;
208 if (t || N < OldSize)
213 if (N > Capacity * BITWORD_SIZE)
219 init_words(
Bits, Capacity,
true);
225 assert(
Bits &&
"Bits never allocated");
226 Bits[Idx / BITWORD_SIZE] |= BitWord(1) << (Idx % BITWORD_SIZE);
232 assert(I <= E &&
"Attempted to set backwards range!");
233 assert(E <=
size() &&
"Attempted to set out-of-bounds range!");
235 if (I == E)
return *
this;
237 if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
238 BitWord EMask = 1UL << (E % BITWORD_SIZE);
239 BitWord IMask = 1UL << (I % BITWORD_SIZE);
240 BitWord Mask = EMask - IMask;
241 Bits[I / BITWORD_SIZE] |= Mask;
245 BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
246 Bits[I / BITWORD_SIZE] |= PrefixMask;
249 for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
250 Bits[I / BITWORD_SIZE] = ~0UL;
252 BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
254 Bits[I / BITWORD_SIZE] |= PostfixMask;
260 init_words(
Bits, Capacity,
false);
265 Bits[Idx / BITWORD_SIZE] &= ~(BitWord(1) << (Idx % BITWORD_SIZE));
271 assert(I <= E &&
"Attempted to reset backwards range!");
272 assert(E <=
size() &&
"Attempted to reset out-of-bounds range!");
274 if (I == E)
return *
this;
276 if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
277 BitWord EMask = 1UL << (E % BITWORD_SIZE);
278 BitWord IMask = 1UL << (I % BITWORD_SIZE);
279 BitWord Mask = EMask - IMask;
280 Bits[I / BITWORD_SIZE] &= ~Mask;
284 BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
285 Bits[I / BITWORD_SIZE] &= ~PrefixMask;
288 for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
289 Bits[I / BITWORD_SIZE] = 0UL;
291 BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
293 Bits[I / BITWORD_SIZE] &= ~PostfixMask;
299 for (
unsigned i = 0; i < NumBitWords(
size()); ++i)
306 Bits[Idx / BITWORD_SIZE] ^= BitWord(1) << (Idx % BITWORD_SIZE);
312 assert (Idx < Size &&
"Out-of-bounds Bit access.");
317 assert (Idx < Size &&
"Out-of-bounds Bit access.");
318 BitWord Mask = BitWord(1) << (Idx % BITWORD_SIZE);
319 return (
Bits[Idx / BITWORD_SIZE] & Mask) != 0;
322 bool test(
unsigned Idx)
const {
328 unsigned ThisWords = NumBitWords(
size());
329 unsigned RHSWords = NumBitWords(RHS.
size());
330 for (
unsigned i = 0, e =
std::min(ThisWords, RHSWords); i != e; ++i)
331 if (
Bits[i] & RHS.Bits[i])
338 unsigned ThisWords = NumBitWords(
size());
339 unsigned RHSWords = NumBitWords(RHS.
size());
341 for (i = 0; i !=
std::min(ThisWords, RHSWords); ++i)
342 if (
Bits[i] != RHS.Bits[i])
346 if (i != ThisWords) {
347 for (; i != ThisWords; ++i)
350 }
else if (i != RHSWords) {
351 for (; i != RHSWords; ++i)
359 return !(*
this == RHS);
364 unsigned ThisWords = NumBitWords(
size());
365 unsigned RHSWords = NumBitWords(RHS.
size());
367 for (i = 0; i !=
std::min(ThisWords, RHSWords); ++i)
368 Bits[i] &= RHS.Bits[i];
373 for (; i != ThisWords; ++i)
381 unsigned ThisWords = NumBitWords(
size());
382 unsigned RHSWords = NumBitWords(RHS.
size());
384 for (i = 0; i !=
std::min(ThisWords, RHSWords); ++i)
385 Bits[i] &= ~RHS.Bits[i];
392 unsigned ThisWords = NumBitWords(
size());
393 unsigned RHSWords = NumBitWords(RHS.
size());
395 for (i = 0; i !=
std::min(ThisWords, RHSWords); ++i)
396 if ((
Bits[i] & ~RHS.Bits[i]) != 0)
399 for (; i != ThisWords ; ++i)
409 for (
size_t i = 0, e = NumBitWords(RHS.
size()); i != e; ++i)
410 Bits[i] |= RHS.Bits[i];
417 for (
size_t i = 0, e = NumBitWords(RHS.
size()); i != e; ++i)
418 Bits[i] ^= RHS.Bits[i];
424 if (
this == &RHS)
return *
this;
427 unsigned RHSWords = NumBitWords(Size);
428 if (Size <= Capacity * BITWORD_SIZE) {
430 std::memcpy(
Bits, RHS.Bits, RHSWords *
sizeof(BitWord));
437 assert(Capacity > 0 &&
"negative capacity?");
438 BitWord *NewBits = (BitWord *)std::malloc(Capacity *
sizeof(BitWord));
439 std::memcpy(NewBits, RHS.Bits, Capacity *
sizeof(BitWord));
449 if (
this == &RHS)
return *
this;
454 Capacity = RHS.Capacity;
482 applyMask<true, false>(Mask, MaskWords);
488 applyMask<false, false>(Mask, MaskWords);
494 applyMask<true, true>(Mask, MaskWords);
500 applyMask<false, true>(Mask, MaskWords);
504 unsigned NumBitWords(
unsigned S)
const {
505 return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
509 void set_unused_bits(
bool t =
true) {
511 unsigned UsedWords = NumBitWords(Size);
512 if (Capacity > UsedWords)
513 init_words(&
Bits[UsedWords], (Capacity-UsedWords), t);
516 unsigned ExtraBits = Size % BITWORD_SIZE;
518 BitWord ExtraBitMask = ~0UL << ExtraBits;
520 Bits[UsedWords-1] |= ExtraBitMask;
522 Bits[UsedWords-1] &= ~ExtraBitMask;
527 void clear_unused_bits() {
528 set_unused_bits(
false);
531 void grow(
unsigned NewSize) {
532 Capacity = std::max(NumBitWords(NewSize), Capacity * 2);
533 assert(Capacity > 0 &&
"realloc-ing zero space");
534 Bits = (BitWord *)std::realloc(
Bits, Capacity *
sizeof(BitWord));
539 void init_words(BitWord *B,
unsigned NumWords,
bool t) {
540 memset(B, 0 - (
int)t, NumWords*
sizeof(BitWord));
543 template<
bool AddBits,
bool InvertMask>
544 void applyMask(
const uint32_t *Mask,
unsigned MaskWords) {
545 static_assert(BITWORD_SIZE % 32 == 0,
"Unsupported BitWord size.");
547 const unsigned Scale = BITWORD_SIZE / 32;
549 for (i = 0; MaskWords >= Scale; ++i, MaskWords -= Scale) {
550 BitWord BW =
Bits[i];
552 for (
unsigned b = 0; b != BITWORD_SIZE; b += 32) {
553 uint32_t M = *Mask++;
554 if (InvertMask) M = ~M;
555 if (AddBits) BW |= BitWord(M) << b;
556 else BW &= ~(BitWord(M) << b);
560 for (
unsigned b = 0; MaskWords; b += 32, --MaskWords) {
561 uint32_t M = *Mask++;
562 if (InvertMask) M = ~M;
563 if (AddBits)
Bits[i] |= BitWord(M) << b;
564 else Bits[i] &= ~(BitWord(M) << b);
BitVector & reset(const BitVector &RHS)
reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
clearBitsInMask - Clear any bits in this vector that are set in Mask.
int find_first() const
find_first - Returns the index of the first set bit, -1 if none of the bits are set.
size_type size() const
size - Returns the number of bits in this bitvector.
bool none() const
none - Returns true if none of the bits are set.
bool anyCommon(const BitVector &RHS) const
Test if any common bits are set.
int find_next(unsigned Prev) const
find_next - Returns the index of the next set bit following the "Prev" bit.
BitVector & set(unsigned Idx)
BitVector(const BitVector &RHS)
BitVector copy ctor.
void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
bool any() const
any - Returns true if any bit is set.
void clear()
clear - Clear all bits.
void swap(BitVector &RHS)
BitVector & operator&=(const BitVector &RHS)
Intersection, union, disjoint union.
Number of individual test Apply this number of consecutive mutations to each input exit after the first new interesting input is found the minimized corpus is saved into the first input directory Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
reference(BitVector &b, unsigned Idx)
BitVector & operator|=(const BitVector &RHS)
bool all() const
all - Returns true if all bits are set.
BitVector()
BitVector default ctor - Creates an empty bitvector.
bool operator[](unsigned Idx) const
BitVector & flip(unsigned Idx)
void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
size_type count() const
count - Returns the number of bits which are set.
std::size_t countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1...
BitVector & set(unsigned I, unsigned E)
set - Efficiently set a range of bits in [I, E)
bool empty() const
empty - Tests whether there are no bits in this bitvector.
BitVector & operator^=(const BitVector &RHS)
BitVector(unsigned s, bool t=false)
BitVector ctor - Creates a bitvector of specified number of bits.
for(unsigned i=0, e=MI->getNumOperands();i!=e;++i)
BitVector(BitVector &&RHS)
BitVector & reset(unsigned I, unsigned E)
reset - Efficiently reset a range of bits in [I, E)
const BitVector & operator=(const BitVector &RHS)
unsigned countPopulation(T Value)
Count the number of set bits in a value.
reference operator[](unsigned Idx)
bool test(unsigned Idx) const
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
bool test(const BitVector &RHS) const
test - Check if (This - RHS) is zero.
reference & operator=(bool t)
reference & operator=(reference t)
BitVector & reset(unsigned Idx)
bool operator!=(const BitVector &RHS) const
bool operator==(const BitVector &RHS) const
void setBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
setBitsInMask - Add '1' bits from Mask to this vector.
const BitVector & operator=(BitVector &&RHS)