LLVM 22.0.0git
Bitfields.h
Go to the documentation of this file.
1//===-- llvm/ADT/Bitfield.h - Get and Set bits in an integer ---*- C++ -*--===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements methods to test, set and extract typed bits from packed
11/// unsigned integers.
12///
13/// Why not C++ bitfields?
14/// ----------------------
15/// C++ bitfields do not offer control over the bit layout nor consistent
16/// behavior when it comes to out of range values.
17/// For instance, the layout is implementation defined and adjacent bits may be
18/// packed together but are not required to. This is problematic when storage is
19/// sparse and data must be stored in a particular integer type.
20///
21/// The methods provided in this file ensure precise control over the
22/// layout/storage as well as protection against out of range values.
23///
24/// Usage example
25/// -------------
26/// \code{.cpp}
27/// uint8_t Storage = 0;
28///
29/// // Store and retrieve a single bit as bool.
30/// using Bool = Bitfield::Element<bool, 0, 1>;
31/// Bitfield::set<Bool>(Storage, true);
32/// EXPECT_EQ(Storage, 0b00000001);
33/// // ^
34/// EXPECT_EQ(Bitfield::get<Bool>(Storage), true);
35///
36/// // Store and retrieve a 2 bit typed enum.
37/// // Note: enum underlying type must be unsigned.
38/// enum class SuitEnum : uint8_t { CLUBS, DIAMONDS, HEARTS, SPADES };
39/// // Note: enum maximum value needs to be passed in as last parameter.
40/// using Suit = Bitfield::Element<SuitEnum, 1, 2, SuitEnum::SPADES>;
41/// Bitfield::set<Suit>(Storage, SuitEnum::HEARTS);
42/// EXPECT_EQ(Storage, 0b00000101);
43/// // ^^
44/// EXPECT_EQ(Bitfield::get<Suit>(Storage), SuitEnum::HEARTS);
45///
46/// // Store and retrieve a 5 bit value as unsigned.
47/// using Value = Bitfield::Element<unsigned, 3, 5>;
48/// Bitfield::set<Value>(Storage, 10);
49/// EXPECT_EQ(Storage, 0b01010101);
50/// // ^^^^^
51/// EXPECT_EQ(Bitfield::get<Value>(Storage), 10U);
52///
53/// // Interpret the same 5 bit value as signed.
54/// using SignedValue = Bitfield::Element<int, 3, 5>;
55/// Bitfield::set<SignedValue>(Storage, -2);
56/// EXPECT_EQ(Storage, 0b11110101);
57/// // ^^^^^
58/// EXPECT_EQ(Bitfield::get<SignedValue>(Storage), -2);
59///
60/// // Ability to efficiently test if a field is non zero.
61/// EXPECT_TRUE(Bitfield::test<Value>(Storage));
62///
63/// // Alter Storage changes value.
64/// Storage = 0;
65/// EXPECT_EQ(Bitfield::get<Bool>(Storage), false);
66/// EXPECT_EQ(Bitfield::get<Suit>(Storage), SuitEnum::CLUBS);
67/// EXPECT_EQ(Bitfield::get<Value>(Storage), 0U);
68/// EXPECT_EQ(Bitfield::get<SignedValue>(Storage), 0);
69///
70/// Storage = 255;
71/// EXPECT_EQ(Bitfield::get<Bool>(Storage), true);
72/// EXPECT_EQ(Bitfield::get<Suit>(Storage), SuitEnum::SPADES);
73/// EXPECT_EQ(Bitfield::get<Value>(Storage), 31U);
74/// EXPECT_EQ(Bitfield::get<SignedValue>(Storage), -1);
75/// \endcode
76///
77//===----------------------------------------------------------------------===//
78
79#ifndef LLVM_ADT_BITFIELDS_H
80#define LLVM_ADT_BITFIELDS_H
81
82#include <cassert>
83#include <climits> // CHAR_BIT
84#include <cstddef> // size_t
85#include <cstdint> // uintXX_t
86#include <limits> // numeric_limits
87#include <type_traits>
88
90
91namespace llvm {
92
94
95/// Impl is where Bifield description and Storage are put together to interact
96/// with values.
97template <typename Bitfield, typename StorageType> struct Impl {
98 static_assert(std::is_unsigned<StorageType>::value,
99 "Storage must be unsigned");
100 using IntegerType = typename Bitfield::IntegerType;
101
102 static constexpr size_t StorageBits = sizeof(StorageType) * CHAR_BIT;
103 static_assert(Bitfield::FirstBit < StorageBits, "Data must fit in mask");
104 static_assert(Bitfield::LastBit < StorageBits, "Data must fit in mask");
105 static constexpr StorageType LowMask =
106 maskTrailingOnes<StorageType>(Bitfield::Bits);
107 static constexpr StorageType Mask = LowMask << Bitfield::Shift;
108
109 /// Validates that `UserValue` fits within the bitfield's range.
110 static void checkValue(IntegerType UserValue, IntegerType UserMaxValue) {
111 assert(UserValue <= UserMaxValue && "value is too big");
112 if constexpr (std::is_unsigned_v<IntegerType>) {
113 assert(isUInt<Bitfield::Bits>(UserValue) && "value is too big");
114 } else {
115 static_assert(std::is_signed_v<IntegerType>,
116 "IntegerType must be signed");
117 assert(isInt<Bitfield::Bits>(UserValue) && "value is out of range");
118 }
119 }
120
121 /// Checks `UserValue` is within bounds and packs it between `FirstBit` and
122 /// `LastBit` of `Packed` leaving the rest unchanged.
123 static void update(StorageType &Packed, IntegerType UserValue) {
124 checkValue(UserValue, Bitfield::UserMaxValue);
125 const StorageType StorageValue = UserValue & LowMask;
126 Packed &= ~Mask;
127 Packed |= StorageValue << Bitfield::Shift;
128 }
129
130 /// Interprets bits between `FirstBit` and `LastBit` of `Packed` as
131 /// an`IntegerType`.
132 static IntegerType extract(StorageType Packed) {
133 const StorageType StorageValue = (Packed & Mask) >> Bitfield::Shift;
134 if constexpr (std::is_signed_v<IntegerType>)
135 return SignExtend64<Bitfield::Bits>(StorageValue);
136 return StorageValue;
137 }
138
139 /// Interprets bits between `FirstBit` and `LastBit` of `Packed` as
140 /// an`IntegerType`.
141 static StorageType test(StorageType Packed) { return Packed & Mask; }
142};
143
144/// `Bitfield` deals with the following type:
145/// - unsigned enums
146/// - signed and unsigned integer
147/// - `bool`
148/// Internally though we only manipulate integer with well defined and
149/// consistent semantics, this excludes typed enums and `bool` that are replaced
150/// with their unsigned counterparts. The correct type is restored in the public
151/// API.
152template <typename T, bool = std::is_enum<T>::value>
154 using type = std::underlying_type_t<T>;
155};
156template <typename T> struct ResolveUnderlyingType<T, false> {
157 static_assert(!std::is_same_v<T, bool> || sizeof(bool) == 1,
158 "T being bool requires sizeof(bool) == 1.");
159 using type = std::conditional_t<std::is_same_v<T, bool>, uint8_t, T>;
160};
161
162} // namespace bitfields_details
163
164/// Holds functions to get, set or test bitfields.
165struct Bitfield {
166 /// Describes an element of a Bitfield. This type is then used with the
167 /// Bitfield static member functions.
168 /// \tparam T The type of the field once in unpacked form.
169 /// \tparam Offset The position of the first bit.
170 /// \tparam Size The size of the field.
171 /// \tparam MaxValue For enums the maximum enum allowed.
172 template <typename T, unsigned Offset, unsigned Size,
173 T MaxValue = std::is_enum<T>::value
174 ? T(0) // coupled with static_assert below
175 : std::numeric_limits<T>::max()>
176 struct Element {
177 using Type = T;
180 static constexpr unsigned Shift = Offset;
181 static constexpr unsigned Bits = Size;
182 static constexpr unsigned FirstBit = Offset;
183 static constexpr unsigned LastBit = Shift + Bits - 1;
184 static constexpr unsigned NextBit = Shift + Bits;
185
186 private:
187 template <typename, typename> friend struct bitfields_details::Impl;
188
189 static_assert(Bits > 0, "Bits must be non zero");
190 static constexpr size_t TypeBits = sizeof(IntegerType) * CHAR_BIT;
191 static_assert(Bits <= TypeBits, "Bits may not be greater than T size");
192 static_assert(!std::is_enum<T>::value || MaxValue != T(0),
193 "Enum Bitfields must provide a MaxValue");
194 static_assert(!std::is_enum<T>::value ||
195 std::is_unsigned<IntegerType>::value,
196 "Enum must be unsigned");
197 static_assert(std::is_integral<IntegerType>::value &&
198 std::numeric_limits<IntegerType>::is_integer,
199 "IntegerType must be an integer type");
200
201 static constexpr IntegerType UserMaxValue =
202 static_cast<IntegerType>(MaxValue);
203 };
204
205 /// Unpacks the field from the `Packed` value.
206 template <typename Bitfield, typename StorageType>
207 static typename Bitfield::Type get(StorageType Packed) {
209 return static_cast<typename Bitfield::Type>(I::extract(Packed));
210 }
211
212 /// Return a non-zero value if the field is non-zero.
213 /// It is more efficient than `getField`.
214 template <typename Bitfield, typename StorageType>
215 static StorageType test(StorageType Packed) {
217 return I::test(Packed);
218 }
219
220 /// Sets the typed value in the provided `Packed` value.
221 /// The method will asserts if the provided value is too big to fit in.
222 template <typename Bitfield, typename StorageType>
223 static void set(StorageType &Packed, typename Bitfield::Type Value) {
225 I::update(Packed, static_cast<typename Bitfield::IntegerType>(Value));
226 }
227
228 /// Returns whether the two bitfields share common bits.
229 template <typename A, typename B> static constexpr bool isOverlapping() {
230 return A::LastBit >= B::FirstBit && B::LastBit >= A::FirstBit;
231 }
232
233 template <typename A> static constexpr bool areContiguous() { return true; }
234 template <typename A, typename B, typename... Others>
235 static constexpr bool areContiguous() {
236 return A::NextBit == B::FirstBit && areContiguous<B, Others...>();
237 }
238};
239
240} // namespace llvm
241
242#endif // LLVM_ADT_BITFIELDS_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define I(x, y, z)
Definition MD5.cpp:58
#define T
LLVM Value Representation.
Definition Value.h:75
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:165
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:189
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
constexpr T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
Definition MathExtras.h:77
Describes an element of a Bitfield.
Definition Bitfields.h:176
static constexpr unsigned Shift
Definition Bitfields.h:180
typename bitfields_details::ResolveUnderlyingType< T >::type IntegerType
Definition Bitfields.h:178
static constexpr unsigned FirstBit
Definition Bitfields.h:182
static constexpr unsigned NextBit
Definition Bitfields.h:184
static constexpr unsigned LastBit
Definition Bitfields.h:183
Holds functions to get, set or test bitfields.
Definition Bitfields.h:165
static constexpr bool areContiguous()
Definition Bitfields.h:233
static Bitfield::Type get(StorageType Packed)
Unpacks the field from the Packed value.
Definition Bitfields.h:207
static constexpr bool areContiguous()
Definition Bitfields.h:235
static constexpr bool isOverlapping()
Returns whether the two bitfields share common bits.
Definition Bitfields.h:229
static void set(StorageType &Packed, typename Bitfield::Type Value)
Sets the typed value in the provided Packed value.
Definition Bitfields.h:223
static StorageType test(StorageType Packed)
Return a non-zero value if the field is non-zero.
Definition Bitfields.h:215
Impl is where Bifield description and Storage are put together to interact with values.
Definition Bitfields.h:97
static constexpr size_t StorageBits
Definition Bitfields.h:102
static IntegerType extract(StorageType Packed)
Interprets bits between FirstBit and LastBit of Packed as anIntegerType.
Definition Bitfields.h:132
static void checkValue(IntegerType UserValue, IntegerType UserMaxValue)
Validates that UserValue fits within the bitfield's range.
Definition Bitfields.h:110
static void update(StorageType &Packed, IntegerType UserValue)
Checks UserValue is within bounds and packs it between FirstBit and LastBit of Packed leaving the res...
Definition Bitfields.h:123
static constexpr StorageType LowMask
Definition Bitfields.h:105
static StorageType test(StorageType Packed)
Interprets bits between FirstBit and LastBit of Packed as anIntegerType.
Definition Bitfields.h:141
static constexpr StorageType Mask
Definition Bitfields.h:107
typename Bitfield::IntegerType IntegerType
Definition Bitfields.h:100
std::conditional_t< std::is_same_v< T, bool >, uint8_t, T > type
Definition Bitfields.h:159
Bitfield deals with the following type:
Definition Bitfields.h:153