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.

574 lines
16 KiB

  1. //===- llvm/ADT/SmallBitVector.h - 'Normally small' 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 SmallBitVector class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ADT_SMALLBITVECTOR_H
  14. #define LLVM_ADT_SMALLBITVECTOR_H
  15. #include "llvm/ADT/BitVector.h"
  16. #include "llvm/Support/Compiler.h"
  17. #include "llvm/Support/MathExtras.h"
  18. #include <cassert>
  19. namespace llvm {
  20. /// SmallBitVector - This is a 'bitvector' (really, a variable-sized bit array),
  21. /// optimized for the case when the array is small. It contains one
  22. /// pointer-sized field, which is directly used as a plain collection of bits
  23. /// when possible, or as a pointer to a larger heap-allocated array when
  24. /// necessary. This allows normal "small" cases to be fast without losing
  25. /// generality for large inputs.
  26. ///
  27. class SmallBitVector {
  28. // TODO: In "large" mode, a pointer to a BitVector is used, leading to an
  29. // unnecessary level of indirection. It would be more efficient to use a
  30. // pointer to memory containing size, allocation size, and the array of bits.
  31. uintptr_t X;
  32. enum {
  33. // The number of bits in this class.
  34. NumBaseBits = sizeof(uintptr_t) * CHAR_BIT,
  35. // One bit is used to discriminate between small and large mode. The
  36. // remaining bits are used for the small-mode representation.
  37. SmallNumRawBits = NumBaseBits - 1,
  38. // A few more bits are used to store the size of the bit set in small mode.
  39. // Theoretically this is a ceil-log2. These bits are encoded in the most
  40. // significant bits of the raw bits.
  41. SmallNumSizeBits = (NumBaseBits == 32 ? 5 :
  42. NumBaseBits == 64 ? 6 :
  43. SmallNumRawBits),
  44. // The remaining bits are used to store the actual set in small mode.
  45. SmallNumDataBits = SmallNumRawBits - SmallNumSizeBits
  46. };
  47. public:
  48. // Encapsulation of a single bit.
  49. class reference {
  50. SmallBitVector &TheVector;
  51. unsigned BitPos;
  52. public:
  53. reference(SmallBitVector &b, unsigned Idx) : TheVector(b), BitPos(Idx) {}
  54. reference& operator=(reference t) {
  55. *this = bool(t);
  56. return *this;
  57. }
  58. reference& operator=(bool t) {
  59. if (t)
  60. TheVector.set(BitPos);
  61. else
  62. TheVector.reset(BitPos);
  63. return *this;
  64. }
  65. operator bool() const {
  66. return const_cast<const SmallBitVector &>(TheVector).operator[](BitPos);
  67. }
  68. };
  69. private:
  70. bool isSmall() const {
  71. return X & uintptr_t(1);
  72. }
  73. BitVector *getPointer() const {
  74. assert(!isSmall());
  75. return reinterpret_cast<BitVector *>(X);
  76. }
  77. void switchToSmall(uintptr_t NewSmallBits, size_t NewSize) {
  78. X = 1;
  79. setSmallSize(NewSize);
  80. setSmallBits(NewSmallBits);
  81. }
  82. void switchToLarge(BitVector *BV) {
  83. X = reinterpret_cast<uintptr_t>(BV);
  84. assert(!isSmall() && "Tried to use an unaligned pointer");
  85. }
  86. // Return all the bits used for the "small" representation; this includes
  87. // bits for the size as well as the element bits.
  88. uintptr_t getSmallRawBits() const {
  89. assert(isSmall());
  90. return X >> 1;
  91. }
  92. void setSmallRawBits(uintptr_t NewRawBits) {
  93. assert(isSmall());
  94. X = (NewRawBits << 1) | uintptr_t(1);
  95. }
  96. // Return the size.
  97. size_t getSmallSize() const {
  98. return getSmallRawBits() >> SmallNumDataBits;
  99. }
  100. void setSmallSize(size_t Size) {
  101. setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
  102. }
  103. // Return the element bits.
  104. uintptr_t getSmallBits() const {
  105. return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
  106. }
  107. void setSmallBits(uintptr_t NewBits) {
  108. setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
  109. (getSmallSize() << SmallNumDataBits));
  110. }
  111. public:
  112. /// SmallBitVector default ctor - Creates an empty bitvector.
  113. SmallBitVector() : X(1) {}
  114. /// SmallBitVector ctor - Creates a bitvector of specified number of bits. All
  115. /// bits are initialized to the specified value.
  116. explicit SmallBitVector(unsigned s, bool t = false) {
  117. if (s <= SmallNumDataBits)
  118. switchToSmall(t ? ~uintptr_t(0) : 0, s);
  119. else
  120. switchToLarge(new BitVector(s, t));
  121. }
  122. /// SmallBitVector copy ctor.
  123. SmallBitVector(const SmallBitVector &RHS) {
  124. if (RHS.isSmall())
  125. X = RHS.X;
  126. else
  127. switchToLarge(new BitVector(*RHS.getPointer()));
  128. }
  129. #if LLVM_HAS_RVALUE_REFERENCES
  130. SmallBitVector(SmallBitVector &&RHS) : X(RHS.X) {
  131. RHS.X = 1;
  132. }
  133. #endif
  134. ~SmallBitVector() {
  135. if (!isSmall())
  136. delete getPointer();
  137. }
  138. /// empty - Tests whether there are no bits in this bitvector.
  139. bool empty() const {
  140. return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
  141. }
  142. /// size - Returns the number of bits in this bitvector.
  143. size_t size() const {
  144. return isSmall() ? getSmallSize() : getPointer()->size();
  145. }
  146. /// count - Returns the number of bits which are set.
  147. unsigned count() const {
  148. if (isSmall()) {
  149. uintptr_t Bits = getSmallBits();
  150. if (NumBaseBits == 32)
  151. return CountPopulation_32(Bits);
  152. if (NumBaseBits == 64)
  153. return CountPopulation_64(Bits);
  154. llvm_unreachable("Unsupported!");
  155. }
  156. return getPointer()->count();
  157. }
  158. /// any - Returns true if any bit is set.
  159. bool any() const {
  160. if (isSmall())
  161. return getSmallBits() != 0;
  162. return getPointer()->any();
  163. }
  164. /// all - Returns true if all bits are set.
  165. bool all() const {
  166. if (isSmall())
  167. return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1;
  168. return getPointer()->all();
  169. }
  170. /// none - Returns true if none of the bits are set.
  171. bool none() const {
  172. if (isSmall())
  173. return getSmallBits() == 0;
  174. return getPointer()->none();
  175. }
  176. /// find_first - Returns the index of the first set bit, -1 if none
  177. /// of the bits are set.
  178. int find_first() const {
  179. if (isSmall()) {
  180. uintptr_t Bits = getSmallBits();
  181. if (Bits == 0)
  182. return -1;
  183. if (NumBaseBits == 32)
  184. return CountTrailingZeros_32(Bits);
  185. if (NumBaseBits == 64)
  186. return CountTrailingZeros_64(Bits);
  187. llvm_unreachable("Unsupported!");
  188. }
  189. return getPointer()->find_first();
  190. }
  191. /// find_next - Returns the index of the next set bit following the
  192. /// "Prev" bit. Returns -1 if the next set bit is not found.
  193. int find_next(unsigned Prev) const {
  194. if (isSmall()) {
  195. uintptr_t Bits = getSmallBits();
  196. // Mask off previous bits.
  197. Bits &= ~uintptr_t(0) << (Prev + 1);
  198. if (Bits == 0 || Prev + 1 >= getSmallSize())
  199. return -1;
  200. if (NumBaseBits == 32)
  201. return CountTrailingZeros_32(Bits);
  202. if (NumBaseBits == 64)
  203. return CountTrailingZeros_64(Bits);
  204. llvm_unreachable("Unsupported!");
  205. }
  206. return getPointer()->find_next(Prev);
  207. }
  208. /// clear - Clear all bits.
  209. void clear() {
  210. if (!isSmall())
  211. delete getPointer();
  212. switchToSmall(0, 0);
  213. }
  214. /// resize - Grow or shrink the bitvector.
  215. void resize(unsigned N, bool t = false) {
  216. if (!isSmall()) {
  217. getPointer()->resize(N, t);
  218. } else if (SmallNumDataBits >= N) {
  219. uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
  220. setSmallSize(N);
  221. setSmallBits(NewBits | getSmallBits());
  222. } else {
  223. BitVector *BV = new BitVector(N, t);
  224. uintptr_t OldBits = getSmallBits();
  225. for (size_t i = 0, e = getSmallSize(); i != e; ++i)
  226. (*BV)[i] = (OldBits >> i) & 1;
  227. switchToLarge(BV);
  228. }
  229. }
  230. void reserve(unsigned N) {
  231. if (isSmall()) {
  232. if (N > SmallNumDataBits) {
  233. uintptr_t OldBits = getSmallRawBits();
  234. size_t SmallSize = getSmallSize();
  235. BitVector *BV = new BitVector(SmallSize);
  236. for (size_t i = 0; i < SmallSize; ++i)
  237. if ((OldBits >> i) & 1)
  238. BV->set(i);
  239. BV->reserve(N);
  240. switchToLarge(BV);
  241. }
  242. } else {
  243. getPointer()->reserve(N);
  244. }
  245. }
  246. // Set, reset, flip
  247. SmallBitVector &set() {
  248. if (isSmall())
  249. setSmallBits(~uintptr_t(0));
  250. else
  251. getPointer()->set();
  252. return *this;
  253. }
  254. SmallBitVector &set(unsigned Idx) {
  255. if (isSmall())
  256. setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
  257. else
  258. getPointer()->set(Idx);
  259. return *this;
  260. }
  261. /// set - Efficiently set a range of bits in [I, E)
  262. SmallBitVector &set(unsigned I, unsigned E) {
  263. assert(I <= E && "Attempted to set backwards range!");
  264. assert(E <= size() && "Attempted to set out-of-bounds range!");
  265. if (I == E) return *this;
  266. if (isSmall()) {
  267. uintptr_t EMask = ((uintptr_t)1) << E;
  268. uintptr_t IMask = ((uintptr_t)1) << I;
  269. uintptr_t Mask = EMask - IMask;
  270. setSmallBits(getSmallBits() | Mask);
  271. } else
  272. getPointer()->set(I, E);
  273. return *this;
  274. }
  275. SmallBitVector &reset() {
  276. if (isSmall())
  277. setSmallBits(0);
  278. else
  279. getPointer()->reset();
  280. return *this;
  281. }
  282. SmallBitVector &reset(unsigned Idx) {
  283. if (isSmall())
  284. setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
  285. else
  286. getPointer()->reset(Idx);
  287. return *this;
  288. }
  289. /// reset - Efficiently reset a range of bits in [I, E)
  290. SmallBitVector &reset(unsigned I, unsigned E) {
  291. assert(I <= E && "Attempted to reset backwards range!");
  292. assert(E <= size() && "Attempted to reset out-of-bounds range!");
  293. if (I == E) return *this;
  294. if (isSmall()) {
  295. uintptr_t EMask = ((uintptr_t)1) << E;
  296. uintptr_t IMask = ((uintptr_t)1) << I;
  297. uintptr_t Mask = EMask - IMask;
  298. setSmallBits(getSmallBits() & ~Mask);
  299. } else
  300. getPointer()->reset(I, E);
  301. return *this;
  302. }
  303. SmallBitVector &flip() {
  304. if (isSmall())
  305. setSmallBits(~getSmallBits());
  306. else
  307. getPointer()->flip();
  308. return *this;
  309. }
  310. SmallBitVector &flip(unsigned Idx) {
  311. if (isSmall())
  312. setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
  313. else
  314. getPointer()->flip(Idx);
  315. return *this;
  316. }
  317. // No argument flip.
  318. SmallBitVector operator~() const {
  319. return SmallBitVector(*this).flip();
  320. }
  321. // Indexing.
  322. reference operator[](unsigned Idx) {
  323. assert(Idx < size() && "Out-of-bounds Bit access.");
  324. return reference(*this, Idx);
  325. }
  326. bool operator[](unsigned Idx) const {
  327. assert(Idx < size() && "Out-of-bounds Bit access.");
  328. if (isSmall())
  329. return ((getSmallBits() >> Idx) & 1) != 0;
  330. return getPointer()->operator[](Idx);
  331. }
  332. bool test(unsigned Idx) const {
  333. return (*this)[Idx];
  334. }
  335. /// Test if any common bits are set.
  336. bool anyCommon(const SmallBitVector &RHS) const {
  337. if (isSmall() && RHS.isSmall())
  338. return (getSmallBits() & RHS.getSmallBits()) != 0;
  339. if (!isSmall() && !RHS.isSmall())
  340. return getPointer()->anyCommon(*RHS.getPointer());
  341. for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
  342. if (test(i) && RHS.test(i))
  343. return true;
  344. return false;
  345. }
  346. // Comparison operators.
  347. bool operator==(const SmallBitVector &RHS) const {
  348. if (size() != RHS.size())
  349. return false;
  350. if (isSmall())
  351. return getSmallBits() == RHS.getSmallBits();
  352. else
  353. return *getPointer() == *RHS.getPointer();
  354. }
  355. bool operator!=(const SmallBitVector &RHS) const {
  356. return !(*this == RHS);
  357. }
  358. // Intersection, union, disjoint union.
  359. SmallBitVector &operator&=(const SmallBitVector &RHS) {
  360. resize(std::max(size(), RHS.size()));
  361. if (isSmall())
  362. setSmallBits(getSmallBits() & RHS.getSmallBits());
  363. else if (!RHS.isSmall())
  364. getPointer()->operator&=(*RHS.getPointer());
  365. else {
  366. SmallBitVector Copy = RHS;
  367. Copy.resize(size());
  368. getPointer()->operator&=(*Copy.getPointer());
  369. }
  370. return *this;
  371. }
  372. SmallBitVector &operator|=(const SmallBitVector &RHS) {
  373. resize(std::max(size(), RHS.size()));
  374. if (isSmall())
  375. setSmallBits(getSmallBits() | RHS.getSmallBits());
  376. else if (!RHS.isSmall())
  377. getPointer()->operator|=(*RHS.getPointer());
  378. else {
  379. SmallBitVector Copy = RHS;
  380. Copy.resize(size());
  381. getPointer()->operator|=(*Copy.getPointer());
  382. }
  383. return *this;
  384. }
  385. SmallBitVector &operator^=(const SmallBitVector &RHS) {
  386. resize(std::max(size(), RHS.size()));
  387. if (isSmall())
  388. setSmallBits(getSmallBits() ^ RHS.getSmallBits());
  389. else if (!RHS.isSmall())
  390. getPointer()->operator^=(*RHS.getPointer());
  391. else {
  392. SmallBitVector Copy = RHS;
  393. Copy.resize(size());
  394. getPointer()->operator^=(*Copy.getPointer());
  395. }
  396. return *this;
  397. }
  398. // Assignment operator.
  399. const SmallBitVector &operator=(const SmallBitVector &RHS) {
  400. if (isSmall()) {
  401. if (RHS.isSmall())
  402. X = RHS.X;
  403. else
  404. switchToLarge(new BitVector(*RHS.getPointer()));
  405. } else {
  406. if (!RHS.isSmall())
  407. *getPointer() = *RHS.getPointer();
  408. else {
  409. delete getPointer();
  410. X = RHS.X;
  411. }
  412. }
  413. return *this;
  414. }
  415. #if LLVM_HAS_RVALUE_REFERENCES
  416. const SmallBitVector &operator=(SmallBitVector &&RHS) {
  417. if (this != &RHS) {
  418. clear();
  419. swap(RHS);
  420. }
  421. return *this;
  422. }
  423. #endif
  424. void swap(SmallBitVector &RHS) {
  425. std::swap(X, RHS.X);
  426. }
  427. /// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize.
  428. /// This computes "*this |= Mask".
  429. void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
  430. if (isSmall())
  431. applyMask<true, false>(Mask, MaskWords);
  432. else
  433. getPointer()->setBitsInMask(Mask, MaskWords);
  434. }
  435. /// clearBitsInMask - Clear any bits in this vector that are set in Mask.
  436. /// Don't resize. This computes "*this &= ~Mask".
  437. void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
  438. if (isSmall())
  439. applyMask<false, false>(Mask, MaskWords);
  440. else
  441. getPointer()->clearBitsInMask(Mask, MaskWords);
  442. }
  443. /// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
  444. /// Don't resize. This computes "*this |= ~Mask".
  445. void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
  446. if (isSmall())
  447. applyMask<true, true>(Mask, MaskWords);
  448. else
  449. getPointer()->setBitsNotInMask(Mask, MaskWords);
  450. }
  451. /// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
  452. /// Don't resize. This computes "*this &= Mask".
  453. void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
  454. if (isSmall())
  455. applyMask<false, true>(Mask, MaskWords);
  456. else
  457. getPointer()->clearBitsNotInMask(Mask, MaskWords);
  458. }
  459. private:
  460. template<bool AddBits, bool InvertMask>
  461. void applyMask(const uint32_t *Mask, unsigned MaskWords) {
  462. assert((NumBaseBits == 64 || NumBaseBits == 32) && "Unsupported word size");
  463. if (NumBaseBits == 64 && MaskWords >= 2) {
  464. uint64_t M = Mask[0] | (uint64_t(Mask[1]) << 32);
  465. if (InvertMask) M = ~M;
  466. if (AddBits) setSmallBits(getSmallBits() | M);
  467. else setSmallBits(getSmallBits() & ~M);
  468. } else {
  469. uint32_t M = Mask[0];
  470. if (InvertMask) M = ~M;
  471. if (AddBits) setSmallBits(getSmallBits() | M);
  472. else setSmallBits(getSmallBits() & ~M);
  473. }
  474. }
  475. };
  476. inline SmallBitVector
  477. operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
  478. SmallBitVector Result(LHS);
  479. Result &= RHS;
  480. return Result;
  481. }
  482. inline SmallBitVector
  483. operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
  484. SmallBitVector Result(LHS);
  485. Result |= RHS;
  486. return Result;
  487. }
  488. inline SmallBitVector
  489. operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
  490. SmallBitVector Result(LHS);
  491. Result ^= RHS;
  492. return Result;
  493. }
  494. } // End llvm namespace
  495. namespace std {
  496. /// Implement std::swap in terms of BitVector swap.
  497. inline void
  498. swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {
  499. LHS.swap(RHS);
  500. }
  501. }
  502. #endif