#ifndef LLVM_ADT_BITFIELDS_H
#define LLVM_ADT_BITFIELDS_H
#include <cassert>
#include <climits>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <type_traits>
namespace llvm {
namespace bitfields_details {
template <typename T, unsigned Bits> struct BitPatterns {
using Unsigned = typename std::make_unsigned<T>::type;
static_assert(sizeof(Unsigned) == sizeof(T), "Types must have same size");
static constexpr unsigned TypeBits = sizeof(Unsigned) * CHAR_BIT;
static_assert(TypeBits >= Bits, "n-bit must fit in T");
static constexpr Unsigned AllZeros = Unsigned(0); static constexpr Unsigned AllOnes = ~Unsigned(0); static constexpr Unsigned Umin = AllZeros; static constexpr Unsigned Umax = AllOnes >> (TypeBits - Bits); static constexpr Unsigned SignBitMask = Unsigned(1) << (Bits - 1); static constexpr Unsigned Smax = Umax >> 1U; static constexpr Unsigned Smin = ~Smax; static constexpr Unsigned SignExtend = Unsigned(Smin << 1U); };
template <typename T, unsigned Bits, bool = std::is_unsigned<T>::value>
struct Compressor {
static_assert(std::is_unsigned<T>::value, "T is unsigned");
using BP = BitPatterns<T, Bits>;
static T pack(T UserValue, T UserMaxValue) {
assert(UserValue <= UserMaxValue && "value is too big");
assert(UserValue <= BP::Umax && "value is too big");
return UserValue;
}
static T unpack(T StorageValue) { return StorageValue; }
};
template <typename T, unsigned Bits> struct Compressor<T, Bits, false> {
static_assert(std::is_signed<T>::value, "T is signed");
using BP = BitPatterns<T, Bits>;
static T pack(T UserValue, T UserMaxValue) {
assert(UserValue <= UserMaxValue && "value is too big");
assert(UserValue <= T(BP::Smax) && "value is too big");
assert(UserValue >= T(BP::Smin) && "value is too small");
if (UserValue < 0)
UserValue &= ~BP::SignExtend;
return UserValue;
}
static T unpack(T StorageValue) {
if (StorageValue >= T(BP::SignBitMask))
StorageValue |= BP::SignExtend;
return StorageValue;
}
};
template <typename Bitfield, typename StorageType> struct Impl {
static_assert(std::is_unsigned<StorageType>::value,
"Storage must be unsigned");
using IntegerType = typename Bitfield::IntegerType;
using C = Compressor<IntegerType, Bitfield::Bits>;
using BP = BitPatterns<StorageType, Bitfield::Bits>;
static constexpr size_t StorageBits = sizeof(StorageType) * CHAR_BIT;
static_assert(Bitfield::FirstBit <= StorageBits, "Data must fit in mask");
static_assert(Bitfield::LastBit <= StorageBits, "Data must fit in mask");
static constexpr StorageType Mask = BP::Umax << Bitfield::Shift;
static void update(StorageType &Packed, IntegerType UserValue) {
const StorageType StorageValue = C::pack(UserValue, Bitfield::UserMaxValue);
Packed &= ~Mask;
Packed |= StorageValue << Bitfield::Shift;
}
static IntegerType extract(StorageType Packed) {
const StorageType StorageValue = (Packed & Mask) >> Bitfield::Shift;
return C::unpack(StorageValue);
}
static StorageType test(StorageType Packed) { return Packed & Mask; }
};
template <typename T, bool = std::is_enum<T>::value>
struct ResolveUnderlyingType {
using type = typename std::underlying_type<T>::type;
};
template <typename T> struct ResolveUnderlyingType<T, false> {
using type = T;
};
template <> struct ResolveUnderlyingType<bool, false> {
using type = std::conditional<sizeof(bool) == 1, uint8_t, void>::type;
};
}
struct Bitfield {
template <typename T, unsigned Offset, unsigned Size,
T MaxValue = std::is_enum<T>::value
? T(0) : std::numeric_limits<T>::max()>
struct Element {
using Type = T;
using IntegerType =
typename bitfields_details::ResolveUnderlyingType<T>::type;
static constexpr unsigned Shift = Offset;
static constexpr unsigned Bits = Size;
static constexpr unsigned FirstBit = Offset;
static constexpr unsigned LastBit = Shift + Bits - 1;
static constexpr unsigned NextBit = Shift + Bits;
private:
template <typename, typename> friend struct bitfields_details::Impl;
static_assert(Bits > 0, "Bits must be non zero");
static constexpr size_t TypeBits = sizeof(IntegerType) * CHAR_BIT;
static_assert(Bits <= TypeBits, "Bits may not be greater than T size");
static_assert(!std::is_enum<T>::value || MaxValue != T(0),
"Enum Bitfields must provide a MaxValue");
static_assert(!std::is_enum<T>::value ||
std::is_unsigned<IntegerType>::value,
"Enum must be unsigned");
static_assert(std::is_integral<IntegerType>::value &&
std::numeric_limits<IntegerType>::is_integer,
"IntegerType must be an integer type");
static constexpr IntegerType UserMaxValue =
static_cast<IntegerType>(MaxValue);
};
template <typename Bitfield, typename StorageType>
static typename Bitfield::Type get(StorageType Packed) {
using I = bitfields_details::Impl<Bitfield, StorageType>;
return static_cast<typename Bitfield::Type>(I::extract(Packed));
}
template <typename Bitfield, typename StorageType>
static StorageType test(StorageType Packed) {
using I = bitfields_details::Impl<Bitfield, StorageType>;
return I::test(Packed);
}
template <typename Bitfield, typename StorageType>
static void set(StorageType &Packed, typename Bitfield::Type Value) {
using I = bitfields_details::Impl<Bitfield, StorageType>;
I::update(Packed, static_cast<typename Bitfield::IntegerType>(Value));
}
template <typename A, typename B> static constexpr bool isOverlapping() {
return A::LastBit >= B::FirstBit && B::LastBit >= A::FirstBit;
}
template <typename A> static constexpr bool areContiguous() { return true; }
template <typename A, typename B, typename... Others>
static constexpr bool areContiguous() {
return A::NextBit == B::FirstBit && areContiguous<B, Others...>();
}
};
}
#endif