14 #ifndef LLVM_ADT_SMALLBITVECTOR_H
15 #define LLVM_ADT_SMALLBITVECTOR_H
39 NumBaseBits =
sizeof(uintptr_t) * CHAR_BIT,
43 SmallNumRawBits = NumBaseBits - 1,
48 SmallNumSizeBits = (NumBaseBits == 32 ? 5 :
49 NumBaseBits == 64 ? 6 :
53 SmallNumDataBits = SmallNumRawBits - SmallNumSizeBits
56 static_assert(NumBaseBits == 64 || NumBaseBits == 32,
57 "Unsupported word size");
78 TheVector.
set(BitPos);
80 TheVector.
reset(BitPos);
85 return const_cast<const SmallBitVector &
>(TheVector).
operator[](BitPos);
90 bool isSmall()
const {
91 return X & uintptr_t(1);
94 BitVector *getPointer()
const {
96 return reinterpret_cast<BitVector *
>(X);
99 void switchToSmall(uintptr_t NewSmallBits,
size_t NewSize) {
101 setSmallSize(NewSize);
102 setSmallBits(NewSmallBits);
105 void switchToLarge(BitVector *BV) {
106 X =
reinterpret_cast<uintptr_t
>(BV);
107 assert(!isSmall() &&
"Tried to use an unaligned pointer");
112 uintptr_t getSmallRawBits()
const {
117 void setSmallRawBits(uintptr_t NewRawBits) {
119 X = (NewRawBits << 1) | uintptr_t(1);
123 size_t getSmallSize()
const {
124 return getSmallRawBits() >> SmallNumDataBits;
127 void setSmallSize(
size_t Size) {
128 setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
132 uintptr_t getSmallBits()
const {
133 return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
136 void setSmallBits(uintptr_t NewBits) {
137 setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
138 (getSmallSize() << SmallNumDataBits));
148 if (s <= SmallNumDataBits)
149 switchToSmall(t ? ~uintptr_t(0) : 0, s);
159 switchToLarge(
new BitVector(*RHS.getPointer()));
173 return isSmall() ? getSmallSize() == 0 : getPointer()->
empty();
178 return isSmall() ? getSmallSize() : getPointer()->
size();
184 uintptr_t
Bits = getSmallBits();
187 return getPointer()->
count();
193 return getSmallBits() != 0;
194 return getPointer()->
any();
200 return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1;
201 return getPointer()->
all();
207 return getSmallBits() == 0;
208 return getPointer()->
none();
215 uintptr_t
Bits = getSmallBits();
227 uintptr_t
Bits = getSmallBits();
229 Bits &= ~uintptr_t(0) << (Prev + 1);
230 if (Bits == 0 || Prev + 1 >= getSmallSize())
247 getPointer()->
resize(N, t);
248 }
else if (SmallNumDataBits >= N) {
249 uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
251 setSmallBits(NewBits | getSmallBits());
254 uintptr_t OldBits = getSmallBits();
255 for (
size_t i = 0, e = getSmallSize(); i != e; ++i)
256 (*BV)[i] = (OldBits >> i) & 1;
263 if (N > SmallNumDataBits) {
264 uintptr_t OldBits = getSmallRawBits();
265 size_t SmallSize = getSmallSize();
267 for (
size_t i = 0; i < SmallSize; ++i)
268 if ((OldBits >> i) & 1)
281 setSmallBits(~uintptr_t(0));
289 assert(Idx <= static_cast<unsigned>(
290 std::numeric_limits<uintptr_t>::digits) &&
291 "undefined behavior");
292 setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
295 getPointer()->
set(Idx);
301 assert(I <= E &&
"Attempted to set backwards range!");
302 assert(E <=
size() &&
"Attempted to set out-of-bounds range!");
303 if (I == E)
return *
this;
305 uintptr_t EMask = ((uintptr_t)1) << E;
306 uintptr_t IMask = ((uintptr_t)1) <<
I;
307 uintptr_t Mask = EMask - IMask;
308 setSmallBits(getSmallBits() | Mask);
310 getPointer()->
set(I, E);
318 getPointer()->
reset();
324 setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
326 getPointer()->
reset(Idx);
332 assert(I <= E &&
"Attempted to reset backwards range!");
333 assert(E <=
size() &&
"Attempted to reset out-of-bounds range!");
334 if (I == E)
return *
this;
336 uintptr_t EMask = ((uintptr_t)1) << E;
337 uintptr_t IMask = ((uintptr_t)1) <<
I;
338 uintptr_t Mask = EMask - IMask;
339 setSmallBits(getSmallBits() & ~Mask);
341 getPointer()->
reset(I, E);
347 setSmallBits(~getSmallBits());
349 getPointer()->
flip();
355 setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
357 getPointer()->
flip(Idx);
368 assert(Idx <
size() &&
"Out-of-bounds Bit access.");
373 assert(Idx <
size() &&
"Out-of-bounds Bit access.");
375 return ((getSmallBits() >> Idx) & 1) != 0;
376 return getPointer()->operator[](Idx);
379 bool test(
unsigned Idx)
const {
385 if (isSmall() && RHS.isSmall())
386 return (getSmallBits() & RHS.getSmallBits()) != 0;
387 if (!isSmall() && !RHS.isSmall())
388 return getPointer()->
anyCommon(*RHS.getPointer());
401 return getSmallBits() == RHS.getSmallBits();
403 return *getPointer() == *RHS.getPointer();
407 return !(*
this == RHS);
414 setSmallBits(getSmallBits() & RHS.getSmallBits());
415 else if (!RHS.isSmall())
416 getPointer()->operator&=(*RHS.getPointer());
420 getPointer()->operator&=(*Copy.getPointer());
427 if (isSmall() && RHS.isSmall())
428 setSmallBits(getSmallBits() & ~RHS.getSmallBits());
429 else if (!isSmall() && !RHS.isSmall())
430 getPointer()->
reset(*RHS.getPointer());
442 if (isSmall() && RHS.isSmall())
443 return (getSmallBits() & ~RHS.getSmallBits()) != 0;
444 if (!isSmall() && !RHS.isSmall())
445 return getPointer()->
test(*RHS.getPointer());
452 for (e =
size(); i != e; ++i)
462 setSmallBits(getSmallBits() | RHS.getSmallBits());
463 else if (!RHS.isSmall())
464 getPointer()->operator|=(*RHS.getPointer());
468 getPointer()->operator|=(*Copy.getPointer());
476 setSmallBits(getSmallBits() ^ RHS.getSmallBits());
477 else if (!RHS.isSmall())
478 getPointer()->operator^=(*RHS.getPointer());
482 getPointer()->operator^=(*Copy.getPointer());
493 switchToLarge(
new BitVector(*RHS.getPointer()));
496 *getPointer() = *RHS.getPointer();
521 applyMask<true, false>(Mask, MaskWords);
530 applyMask<false, false>(Mask, MaskWords);
539 applyMask<true, true>(Mask, MaskWords);
548 applyMask<false, true>(Mask, MaskWords);
554 template<
bool AddBits,
bool InvertMask>
555 void applyMask(
const uint32_t *Mask,
unsigned MaskWords) {
556 if (NumBaseBits == 64 && MaskWords >= 2) {
557 uint64_t M = Mask[0] | (uint64_t(Mask[1]) << 32);
558 if (InvertMask) M = ~M;
559 if (AddBits) setSmallBits(getSmallBits() | M);
560 else setSmallBits(getSmallBits() & ~M);
562 uint32_t M = Mask[0];
563 if (InvertMask) M = ~M;
564 if (AddBits) setSmallBits(getSmallBits() | M);
565 else setSmallBits(getSmallBits() & ~M);
570 inline SmallBitVector
577 inline SmallBitVector
584 inline SmallBitVector
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.
size_type count() const
count - Returns the number of bits which are set.
void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
clearBitsInMask - Clear any bits in this vector that are set in Mask.
bool operator!=(const SmallBitVector &RHS) const
SmallBitVector - This is a 'bitvector' (really, a variable-sized bit array), optimized for the case w...
const SmallBitVector & operator=(SmallBitVector &&RHS)
bool none() const
none - Returns true if none of the bits are set.
reference operator[](unsigned Idx)
SmallBitVector & reset(unsigned I, unsigned E)
reset - Efficiently reset a range of bits in [I, E)
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.
bool all() const
all - Returns true if all bits are set.
SmallBitVector operator&(const SmallBitVector &LHS, const SmallBitVector &RHS)
const SmallBitVector & operator=(const SmallBitVector &RHS)
void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
bool any() const
any - Returns true if any bit is set.
SmallBitVector & flip(unsigned Idx)
SmallBitVector & operator&=(const SmallBitVector &RHS)
SmallBitVector(unsigned s, bool t=false)
SmallBitVector ctor - Creates a bitvector of specified number of bits.
bool anyCommon(const SmallBitVector &RHS) const
Test if any common bits are set.
int find_first() const
find_first - Returns the index of the first set bit, -1 if none of the bits are set.
SmallBitVector operator|(const SmallBitVector &LHS, const SmallBitVector &RHS)
reference & operator=(reference t)
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
SmallBitVector & reset(unsigned Idx)
SmallBitVector & reset(const SmallBitVector &RHS)
reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
bool test(const SmallBitVector &RHS) const
test - Check if (This - RHS) is zero.
bool all() const
all - Returns true if all bits are set.
reference & operator=(bool t)
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.
reference(SmallBitVector &b, unsigned Idx)
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...
void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
bool empty() const
empty - Tests whether there are no bits in this bitvector.
int find_next(unsigned Prev) const
find_next - Returns the index of the next set bit following the "Prev" bit.
SmallBitVector()
SmallBitVector default ctor - Creates an empty bitvector.
void clear()
clear - Clear all bits.
SmallBitVector & operator^=(const SmallBitVector &RHS)
unsigned countPopulation(T Value)
Count the number of set bits in a value.
SmallBitVector & set(unsigned I, unsigned E)
set - Efficiently set a range of bits in [I, E)
SmallBitVector(const SmallBitVector &RHS)
SmallBitVector copy ctor.
bool test(unsigned Idx) const
bool none() const
none - Returns true if none of the bits are set.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
size_t size() const
size - Returns the number of bits in this bitvector.
bool test(unsigned Idx) const
void setBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
setBitsInMask - Add '1' bits from Mask to this vector.
void swap(SmallBitVector &RHS)
SmallBitVector operator~() const
SmallBitVector & set(unsigned Idx)
bool operator==(const SmallBitVector &RHS) const
SmallBitVector operator^(const SmallBitVector &LHS, const SmallBitVector &RHS)
bool any() const
any - Returns true if any bit is set.
bool operator[](unsigned Idx) const
bool empty() const
empty - Tests whether there are no bits in this bitvector.
void setBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
setBitsInMask - Add '1' bits from Mask to this vector.
SmallBitVector & operator|=(const SmallBitVector &RHS)
SmallBitVector(SmallBitVector &&RHS)