xref: /freebsd-src/contrib/llvm-project/llvm/include/llvm/ADT/PackedVector.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- llvm/ADT/PackedVector.h - Packed values vector -----------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
81fd87a68SDimitry Andric ///
91fd87a68SDimitry Andric /// \file
101fd87a68SDimitry Andric /// This file implements the PackedVector class.
111fd87a68SDimitry Andric ///
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #ifndef LLVM_ADT_PACKEDVECTOR_H
150b57cec5SDimitry Andric #define LLVM_ADT_PACKEDVECTOR_H
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
180b57cec5SDimitry Andric #include <cassert>
190b57cec5SDimitry Andric #include <limits>
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric namespace llvm {
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric template <typename T, unsigned BitNum, typename BitVectorTy, bool isSigned>
240b57cec5SDimitry Andric class PackedVectorBase;
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric // This won't be necessary if we can specialize members without specializing
270b57cec5SDimitry Andric // the parent template.
280b57cec5SDimitry Andric template <typename T, unsigned BitNum, typename BitVectorTy>
290b57cec5SDimitry Andric class PackedVectorBase<T, BitNum, BitVectorTy, false> {
300b57cec5SDimitry Andric protected:
310b57cec5SDimitry Andric   static T getValue(const BitVectorTy &Bits, unsigned Idx) {
320b57cec5SDimitry Andric     T val = T();
330b57cec5SDimitry Andric     for (unsigned i = 0; i != BitNum; ++i)
340b57cec5SDimitry Andric       val = T(val | ((Bits[(Idx << (BitNum-1)) + i] ? 1UL : 0UL) << i));
350b57cec5SDimitry Andric     return val;
360b57cec5SDimitry Andric   }
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric   static void setValue(BitVectorTy &Bits, unsigned Idx, T val) {
390b57cec5SDimitry Andric     assert((val >> BitNum) == 0 && "value is too big");
400b57cec5SDimitry Andric     for (unsigned i = 0; i != BitNum; ++i)
410b57cec5SDimitry Andric       Bits[(Idx << (BitNum-1)) + i] = val & (T(1) << i);
420b57cec5SDimitry Andric   }
430b57cec5SDimitry Andric };
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric template <typename T, unsigned BitNum, typename BitVectorTy>
460b57cec5SDimitry Andric class PackedVectorBase<T, BitNum, BitVectorTy, true> {
470b57cec5SDimitry Andric protected:
480b57cec5SDimitry Andric   static T getValue(const BitVectorTy &Bits, unsigned Idx) {
490b57cec5SDimitry Andric     T val = T();
500b57cec5SDimitry Andric     for (unsigned i = 0; i != BitNum-1; ++i)
510b57cec5SDimitry Andric       val = T(val | ((Bits[(Idx << (BitNum-1)) + i] ? 1UL : 0UL) << i));
520b57cec5SDimitry Andric     if (Bits[(Idx << (BitNum-1)) + BitNum-1])
530b57cec5SDimitry Andric       val = ~val;
540b57cec5SDimitry Andric     return val;
550b57cec5SDimitry Andric   }
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   static void setValue(BitVectorTy &Bits, unsigned Idx, T val) {
580b57cec5SDimitry Andric     if (val < 0) {
590b57cec5SDimitry Andric       val = ~val;
600b57cec5SDimitry Andric       Bits.set((Idx << (BitNum-1)) + BitNum-1);
610b57cec5SDimitry Andric     }
620b57cec5SDimitry Andric     assert((val >> (BitNum-1)) == 0 && "value is too big");
630b57cec5SDimitry Andric     for (unsigned i = 0; i != BitNum-1; ++i)
640b57cec5SDimitry Andric       Bits[(Idx << (BitNum-1)) + i] = val & (T(1) << i);
650b57cec5SDimitry Andric   }
660b57cec5SDimitry Andric };
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric /// Store a vector of values using a specific number of bits for each
690b57cec5SDimitry Andric /// value. Both signed and unsigned types can be used, e.g
700b57cec5SDimitry Andric /// @code
710b57cec5SDimitry Andric ///   PackedVector<signed, 2> vec;
720b57cec5SDimitry Andric /// @endcode
730b57cec5SDimitry Andric /// will create a vector accepting values -2, -1, 0, 1. Any other value will hit
740b57cec5SDimitry Andric /// an assertion.
750b57cec5SDimitry Andric template <typename T, unsigned BitNum, typename BitVectorTy = BitVector>
760b57cec5SDimitry Andric class PackedVector : public PackedVectorBase<T, BitNum, BitVectorTy,
770b57cec5SDimitry Andric                                             std::numeric_limits<T>::is_signed> {
780b57cec5SDimitry Andric   BitVectorTy Bits;
790b57cec5SDimitry Andric   using base = PackedVectorBase<T, BitNum, BitVectorTy,
800b57cec5SDimitry Andric                                 std::numeric_limits<T>::is_signed>;
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric public:
830b57cec5SDimitry Andric   class reference {
840b57cec5SDimitry Andric     PackedVector &Vec;
850b57cec5SDimitry Andric     const unsigned Idx;
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric   public:
880b57cec5SDimitry Andric     reference() = delete;
890b57cec5SDimitry Andric     reference(PackedVector &vec, unsigned idx) : Vec(vec), Idx(idx) {}
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric     reference &operator=(T val) {
920b57cec5SDimitry Andric       Vec.setValue(Vec.Bits, Idx, val);
930b57cec5SDimitry Andric       return *this;
940b57cec5SDimitry Andric     }
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric     operator T() const {
970b57cec5SDimitry Andric       return Vec.getValue(Vec.Bits, Idx);
980b57cec5SDimitry Andric     }
990b57cec5SDimitry Andric   };
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric   PackedVector() = default;
1020b57cec5SDimitry Andric   explicit PackedVector(unsigned size) : Bits(size << (BitNum-1)) {}
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric   bool empty() const { return Bits.empty(); }
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric   unsigned size() const { return Bits.size() >> (BitNum - 1); }
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   void clear() { Bits.clear(); }
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric   void resize(unsigned N) { Bits.resize(N << (BitNum - 1)); }
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   void reserve(unsigned N) { Bits.reserve(N << (BitNum-1)); }
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   PackedVector &reset() {
1150b57cec5SDimitry Andric     Bits.reset();
1160b57cec5SDimitry Andric     return *this;
1170b57cec5SDimitry Andric   }
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   void push_back(T val) {
1200b57cec5SDimitry Andric     resize(size()+1);
1210b57cec5SDimitry Andric     (*this)[size()-1] = val;
1220b57cec5SDimitry Andric   }
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric   reference operator[](unsigned Idx) {
1250b57cec5SDimitry Andric     return reference(*this, Idx);
1260b57cec5SDimitry Andric   }
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric   T operator[](unsigned Idx) const {
1290b57cec5SDimitry Andric     return base::getValue(Bits, Idx);
1300b57cec5SDimitry Andric   }
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric   bool operator==(const PackedVector &RHS) const {
1330b57cec5SDimitry Andric     return Bits == RHS.Bits;
1340b57cec5SDimitry Andric   }
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric   bool operator!=(const PackedVector &RHS) const {
1370b57cec5SDimitry Andric     return Bits != RHS.Bits;
1380b57cec5SDimitry Andric   }
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric   PackedVector &operator|=(const PackedVector &RHS) {
1410b57cec5SDimitry Andric     Bits |= RHS.Bits;
1420b57cec5SDimitry Andric     return *this;
1430b57cec5SDimitry Andric   }
144*0fca6ea1SDimitry Andric 
145*0fca6ea1SDimitry Andric   const BitVectorTy &raw_bits() const { return Bits; }
1460b57cec5SDimitry Andric };
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric // Leave BitNum=0 undefined.
1490b57cec5SDimitry Andric template <typename T> class PackedVector<T, 0>;
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric } // end namespace llvm
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric #endif // LLVM_ADT_PACKEDVECTOR_H
154