Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

589 lines
16 KiB

  1. //===- llvm/ADT/BitVector.h - Bit vectors -----------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the BitVector class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ADT_BITVECTOR_H
  14. #define LLVM_ADT_BITVECTOR_H
  15. #include "llvm/Support/Compiler.h"
  16. #include "llvm/Support/ErrorHandling.h"
  17. #include "llvm/Support/MathExtras.h"
  18. #include <algorithm>
  19. #include <cassert>
  20. #include <climits>
  21. #include <cstdlib>
  22. namespace llvm {
  23. class BitVector {
  24. typedef unsigned long BitWord;
  25. enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * CHAR_BIT };
  26. BitWord *Bits; // Actual bits.
  27. unsigned Size; // Size of bitvector in bits.
  28. unsigned Capacity; // Size of allocated memory in BitWord.
  29. public:
  30. // Encapsulation of a single bit.
  31. class reference {
  32. friend class BitVector;
  33. BitWord *WordRef;
  34. unsigned BitPos;
  35. reference(); // Undefined
  36. public:
  37. reference(BitVector &b, unsigned Idx) {
  38. WordRef = &b.Bits[Idx / BITWORD_SIZE];
  39. BitPos = Idx % BITWORD_SIZE;
  40. }
  41. ~reference() {}
  42. reference &operator=(reference t) {
  43. *this = bool(t);
  44. return *this;
  45. }
  46. reference& operator=(bool t) {
  47. if (t)
  48. *WordRef |= 1L << BitPos;
  49. else
  50. *WordRef &= ~(1L << BitPos);
  51. return *this;
  52. }
  53. operator bool() const {
  54. return ((*WordRef) & (1L << BitPos)) ? true : false;
  55. }
  56. };
  57. /// BitVector default ctor - Creates an empty bitvector.
  58. BitVector() : Size(0), Capacity(0) {
  59. Bits = 0;
  60. }
  61. /// BitVector ctor - Creates a bitvector of specified number of bits. All
  62. /// bits are initialized to the specified value.
  63. explicit BitVector(unsigned s, bool t = false) : Size(s) {
  64. Capacity = NumBitWords(s);
  65. Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
  66. init_words(Bits, Capacity, t);
  67. if (t)
  68. clear_unused_bits();
  69. }
  70. /// BitVector copy ctor.
  71. BitVector(const BitVector &RHS) : Size(RHS.size()) {
  72. if (Size == 0) {
  73. Bits = 0;
  74. Capacity = 0;
  75. return;
  76. }
  77. Capacity = NumBitWords(RHS.size());
  78. Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
  79. std::memcpy(Bits, RHS.Bits, Capacity * sizeof(BitWord));
  80. }
  81. #if LLVM_HAS_RVALUE_REFERENCES
  82. BitVector(BitVector &&RHS)
  83. : Bits(RHS.Bits), Size(RHS.Size), Capacity(RHS.Capacity) {
  84. RHS.Bits = 0;
  85. }
  86. #endif
  87. ~BitVector() {
  88. std::free(Bits);
  89. }
  90. /// empty - Tests whether there are no bits in this bitvector.
  91. bool empty() const { return Size == 0; }
  92. /// size - Returns the number of bits in this bitvector.
  93. unsigned size() const { return Size; }
  94. /// count - Returns the number of bits which are set.
  95. unsigned count() const {
  96. unsigned NumBits = 0;
  97. for (unsigned i = 0; i < NumBitWords(size()); ++i)
  98. if (sizeof(BitWord) == 4)
  99. NumBits += CountPopulation_32((uint32_t)Bits[i]);
  100. else if (sizeof(BitWord) == 8)
  101. NumBits += CountPopulation_64(Bits[i]);
  102. else
  103. llvm_unreachable("Unsupported!");
  104. return NumBits;
  105. }
  106. /// any - Returns true if any bit is set.
  107. bool any() const {
  108. for (unsigned i = 0; i < NumBitWords(size()); ++i)
  109. if (Bits[i] != 0)
  110. return true;
  111. return false;
  112. }
  113. /// all - Returns true if all bits are set.
  114. bool all() const {
  115. // TODO: Optimize this.
  116. return count() == size();
  117. }
  118. /// none - Returns true if none of the bits are set.
  119. bool none() const {
  120. return !any();
  121. }
  122. /// find_first - Returns the index of the first set bit, -1 if none
  123. /// of the bits are set.
  124. int find_first() const {
  125. for (unsigned i = 0; i < NumBitWords(size()); ++i)
  126. if (Bits[i] != 0) {
  127. if (sizeof(BitWord) == 4)
  128. return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]);
  129. if (sizeof(BitWord) == 8)
  130. return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
  131. llvm_unreachable("Unsupported!");
  132. }
  133. return -1;
  134. }
  135. /// find_next - Returns the index of the next set bit following the
  136. /// "Prev" bit. Returns -1 if the next set bit is not found.
  137. int find_next(unsigned Prev) const {
  138. ++Prev;
  139. if (Prev >= Size)
  140. return -1;
  141. unsigned WordPos = Prev / BITWORD_SIZE;
  142. unsigned BitPos = Prev % BITWORD_SIZE;
  143. BitWord Copy = Bits[WordPos];
  144. // Mask off previous bits.
  145. Copy &= ~0UL << BitPos;
  146. if (Copy != 0) {
  147. if (sizeof(BitWord) == 4)
  148. return WordPos * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Copy);
  149. if (sizeof(BitWord) == 8)
  150. return WordPos * BITWORD_SIZE + CountTrailingZeros_64(Copy);
  151. llvm_unreachable("Unsupported!");
  152. }
  153. // Check subsequent words.
  154. for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
  155. if (Bits[i] != 0) {
  156. if (sizeof(BitWord) == 4)
  157. return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]);
  158. if (sizeof(BitWord) == 8)
  159. return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
  160. llvm_unreachable("Unsupported!");
  161. }
  162. return -1;
  163. }
  164. /// clear - Clear all bits.
  165. void clear() {
  166. Size = 0;
  167. }
  168. /// resize - Grow or shrink the bitvector.
  169. void resize(unsigned N, bool t = false) {
  170. if (N > Capacity * BITWORD_SIZE) {
  171. unsigned OldCapacity = Capacity;
  172. grow(N);
  173. init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
  174. }
  175. // Set any old unused bits that are now included in the BitVector. This
  176. // may set bits that are not included in the new vector, but we will clear
  177. // them back out below.
  178. if (N > Size)
  179. set_unused_bits(t);
  180. // Update the size, and clear out any bits that are now unused
  181. unsigned OldSize = Size;
  182. Size = N;
  183. if (t || N < OldSize)
  184. clear_unused_bits();
  185. }
  186. void reserve(unsigned N) {
  187. if (N > Capacity * BITWORD_SIZE)
  188. grow(N);
  189. }
  190. // Set, reset, flip
  191. BitVector &set() {
  192. init_words(Bits, Capacity, true);
  193. clear_unused_bits();
  194. return *this;
  195. }
  196. BitVector &set(unsigned Idx) {
  197. Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
  198. return *this;
  199. }
  200. /// set - Efficiently set a range of bits in [I, E)
  201. BitVector &set(unsigned I, unsigned E) {
  202. assert(I <= E && "Attempted to set backwards range!");
  203. assert(E <= size() && "Attempted to set out-of-bounds range!");
  204. if (I == E) return *this;
  205. if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
  206. BitWord EMask = 1UL << (E % BITWORD_SIZE);
  207. BitWord IMask = 1UL << (I % BITWORD_SIZE);
  208. BitWord Mask = EMask - IMask;
  209. Bits[I / BITWORD_SIZE] |= Mask;
  210. return *this;
  211. }
  212. BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
  213. Bits[I / BITWORD_SIZE] |= PrefixMask;
  214. I = RoundUpToAlignment(I, BITWORD_SIZE);
  215. for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
  216. Bits[I / BITWORD_SIZE] = ~0UL;
  217. BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
  218. Bits[I / BITWORD_SIZE] |= PostfixMask;
  219. return *this;
  220. }
  221. BitVector &reset() {
  222. init_words(Bits, Capacity, false);
  223. return *this;
  224. }
  225. BitVector &reset(unsigned Idx) {
  226. Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
  227. return *this;
  228. }
  229. /// reset - Efficiently reset a range of bits in [I, E)
  230. BitVector &reset(unsigned I, unsigned E) {
  231. assert(I <= E && "Attempted to reset backwards range!");
  232. assert(E <= size() && "Attempted to reset out-of-bounds range!");
  233. if (I == E) return *this;
  234. if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
  235. BitWord EMask = 1UL << (E % BITWORD_SIZE);
  236. BitWord IMask = 1UL << (I % BITWORD_SIZE);
  237. BitWord Mask = EMask - IMask;
  238. Bits[I / BITWORD_SIZE] &= ~Mask;
  239. return *this;
  240. }
  241. BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
  242. Bits[I / BITWORD_SIZE] &= ~PrefixMask;
  243. I = RoundUpToAlignment(I, BITWORD_SIZE);
  244. for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
  245. Bits[I / BITWORD_SIZE] = 0UL;
  246. BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
  247. Bits[I / BITWORD_SIZE] &= ~PostfixMask;
  248. return *this;
  249. }
  250. BitVector &flip() {
  251. for (unsigned i = 0; i < NumBitWords(size()); ++i)
  252. Bits[i] = ~Bits[i];
  253. clear_unused_bits();
  254. return *this;
  255. }
  256. BitVector &flip(unsigned Idx) {
  257. Bits[Idx / BITWORD_SIZE] ^= 1L << (Idx % BITWORD_SIZE);
  258. return *this;
  259. }
  260. // Indexing.
  261. reference operator[](unsigned Idx) {
  262. assert (Idx < Size && "Out-of-bounds Bit access.");
  263. return reference(*this, Idx);
  264. }
  265. bool operator[](unsigned Idx) const {
  266. assert (Idx < Size && "Out-of-bounds Bit access.");
  267. BitWord Mask = 1L << (Idx % BITWORD_SIZE);
  268. return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
  269. }
  270. bool test(unsigned Idx) const {
  271. return (*this)[Idx];
  272. }
  273. /// Test if any common bits are set.
  274. bool anyCommon(const BitVector &RHS) const {
  275. unsigned ThisWords = NumBitWords(size());
  276. unsigned RHSWords = NumBitWords(RHS.size());
  277. for (unsigned i = 0, e = std::min(ThisWords, RHSWords); i != e; ++i)
  278. if (Bits[i] & RHS.Bits[i])
  279. return true;
  280. return false;
  281. }
  282. // Comparison operators.
  283. bool operator==(const BitVector &RHS) const {
  284. unsigned ThisWords = NumBitWords(size());
  285. unsigned RHSWords = NumBitWords(RHS.size());
  286. unsigned i;
  287. for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
  288. if (Bits[i] != RHS.Bits[i])
  289. return false;
  290. // Verify that any extra words are all zeros.
  291. if (i != ThisWords) {
  292. for (; i != ThisWords; ++i)
  293. if (Bits[i])
  294. return false;
  295. } else if (i != RHSWords) {
  296. for (; i != RHSWords; ++i)
  297. if (RHS.Bits[i])
  298. return false;
  299. }
  300. return true;
  301. }
  302. bool operator!=(const BitVector &RHS) const {
  303. return !(*this == RHS);
  304. }
  305. /// Intersection, union, disjoint union.
  306. BitVector &operator&=(const BitVector &RHS) {
  307. unsigned ThisWords = NumBitWords(size());
  308. unsigned RHSWords = NumBitWords(RHS.size());
  309. unsigned i;
  310. for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
  311. Bits[i] &= RHS.Bits[i];
  312. // Any bits that are just in this bitvector become zero, because they aren't
  313. // in the RHS bit vector. Any words only in RHS are ignored because they
  314. // are already zero in the LHS.
  315. for (; i != ThisWords; ++i)
  316. Bits[i] = 0;
  317. return *this;
  318. }
  319. /// reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
  320. BitVector &reset(const BitVector &RHS) {
  321. unsigned ThisWords = NumBitWords(size());
  322. unsigned RHSWords = NumBitWords(RHS.size());
  323. unsigned i;
  324. for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
  325. Bits[i] &= ~RHS.Bits[i];
  326. return *this;
  327. }
  328. /// test - Check if (This - RHS) is zero.
  329. /// This is the same as reset(RHS) and any().
  330. bool test(const BitVector &RHS) const {
  331. unsigned ThisWords = NumBitWords(size());
  332. unsigned RHSWords = NumBitWords(RHS.size());
  333. unsigned i;
  334. for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
  335. if ((Bits[i] & ~RHS.Bits[i]) != 0)
  336. return true;
  337. for (; i != ThisWords ; ++i)
  338. if (Bits[i] != 0)
  339. return true;
  340. return false;
  341. }
  342. BitVector &operator|=(const BitVector &RHS) {
  343. if (size() < RHS.size())
  344. resize(RHS.size());
  345. for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
  346. Bits[i] |= RHS.Bits[i];
  347. return *this;
  348. }
  349. BitVector &operator^=(const BitVector &RHS) {
  350. if (size() < RHS.size())
  351. resize(RHS.size());
  352. for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
  353. Bits[i] ^= RHS.Bits[i];
  354. return *this;
  355. }
  356. // Assignment operator.
  357. const BitVector &operator=(const BitVector &RHS) {
  358. if (this == &RHS) return *this;
  359. Size = RHS.size();
  360. unsigned RHSWords = NumBitWords(Size);
  361. if (Size <= Capacity * BITWORD_SIZE) {
  362. if (Size)
  363. std::memcpy(Bits, RHS.Bits, RHSWords * sizeof(BitWord));
  364. clear_unused_bits();
  365. return *this;
  366. }
  367. // Grow the bitvector to have enough elements.
  368. Capacity = RHSWords;
  369. BitWord *NewBits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
  370. std::memcpy(NewBits, RHS.Bits, Capacity * sizeof(BitWord));
  371. // Destroy the old bits.
  372. std::free(Bits);
  373. Bits = NewBits;
  374. return *this;
  375. }
  376. #if LLVM_HAS_RVALUE_REFERENCES
  377. const BitVector &operator=(BitVector &&RHS) {
  378. if (this == &RHS) return *this;
  379. std::free(Bits);
  380. Bits = RHS.Bits;
  381. Size = RHS.Size;
  382. Capacity = RHS.Capacity;
  383. RHS.Bits = 0;
  384. return *this;
  385. }
  386. #endif
  387. void swap(BitVector &RHS) {
  388. std::swap(Bits, RHS.Bits);
  389. std::swap(Size, RHS.Size);
  390. std::swap(Capacity, RHS.Capacity);
  391. }
  392. //===--------------------------------------------------------------------===//
  393. // Portable bit mask operations.
  394. //===--------------------------------------------------------------------===//
  395. //
  396. // These methods all operate on arrays of uint32_t, each holding 32 bits. The
  397. // fixed word size makes it easier to work with literal bit vector constants
  398. // in portable code.
  399. //
  400. // The LSB in each word is the lowest numbered bit. The size of a portable
  401. // bit mask is always a whole multiple of 32 bits. If no bit mask size is
  402. // given, the bit mask is assumed to cover the entire BitVector.
  403. /// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize.
  404. /// This computes "*this |= Mask".
  405. void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
  406. applyMask<true, false>(Mask, MaskWords);
  407. }
  408. /// clearBitsInMask - Clear any bits in this vector that are set in Mask.
  409. /// Don't resize. This computes "*this &= ~Mask".
  410. void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
  411. applyMask<false, false>(Mask, MaskWords);
  412. }
  413. /// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
  414. /// Don't resize. This computes "*this |= ~Mask".
  415. void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
  416. applyMask<true, true>(Mask, MaskWords);
  417. }
  418. /// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
  419. /// Don't resize. This computes "*this &= Mask".
  420. void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
  421. applyMask<false, true>(Mask, MaskWords);
  422. }
  423. private:
  424. unsigned NumBitWords(unsigned S) const {
  425. return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
  426. }
  427. // Set the unused bits in the high words.
  428. void set_unused_bits(bool t = true) {
  429. // Set high words first.
  430. unsigned UsedWords = NumBitWords(Size);
  431. if (Capacity > UsedWords)
  432. init_words(&Bits[UsedWords], (Capacity-UsedWords), t);
  433. // Then set any stray high bits of the last used word.
  434. unsigned ExtraBits = Size % BITWORD_SIZE;
  435. if (ExtraBits) {
  436. BitWord ExtraBitMask = ~0UL << ExtraBits;
  437. if (t)
  438. Bits[UsedWords-1] |= ExtraBitMask;
  439. else
  440. Bits[UsedWords-1] &= ~ExtraBitMask;
  441. }
  442. }
  443. // Clear the unused bits in the high words.
  444. void clear_unused_bits() {
  445. set_unused_bits(false);
  446. }
  447. void grow(unsigned NewSize) {
  448. Capacity = std::max(NumBitWords(NewSize), Capacity * 2);
  449. Bits = (BitWord *)std::realloc(Bits, Capacity * sizeof(BitWord));
  450. clear_unused_bits();
  451. }
  452. void init_words(BitWord *B, unsigned NumWords, bool t) {
  453. memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
  454. }
  455. template<bool AddBits, bool InvertMask>
  456. void applyMask(const uint32_t *Mask, unsigned MaskWords) {
  457. assert(BITWORD_SIZE % 32 == 0 && "Unsupported BitWord size.");
  458. MaskWords = std::min(MaskWords, (size() + 31) / 32);
  459. const unsigned Scale = BITWORD_SIZE / 32;
  460. unsigned i;
  461. for (i = 0; MaskWords >= Scale; ++i, MaskWords -= Scale) {
  462. BitWord BW = Bits[i];
  463. // This inner loop should unroll completely when BITWORD_SIZE > 32.
  464. for (unsigned b = 0; b != BITWORD_SIZE; b += 32) {
  465. uint32_t M = *Mask++;
  466. if (InvertMask) M = ~M;
  467. if (AddBits) BW |= BitWord(M) << b;
  468. else BW &= ~(BitWord(M) << b);
  469. }
  470. Bits[i] = BW;
  471. }
  472. for (unsigned b = 0; MaskWords; b += 32, --MaskWords) {
  473. uint32_t M = *Mask++;
  474. if (InvertMask) M = ~M;
  475. if (AddBits) Bits[i] |= BitWord(M) << b;
  476. else Bits[i] &= ~(BitWord(M) << b);
  477. }
  478. if (AddBits)
  479. clear_unused_bits();
  480. }
  481. };
  482. } // End llvm namespace
  483. namespace std {
  484. /// Implement std::swap in terms of BitVector swap.
  485. inline void
  486. swap(llvm::BitVector &LHS, llvm::BitVector &RHS) {
  487. LHS.swap(RHS);
  488. }
  489. }
  490. #endif