bitunion.hh (11800:54436a1784dc) bitunion.hh (12450:b5a0300fc327)
1/*
2 * Copyright (c) 2007-2008 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;

--- 17 unchanged lines hidden (view full) ---

26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Gabe Black
29 */
30
31#ifndef __BASE_BITUNION_HH__
32#define __BASE_BITUNION_HH__
33
1/*
2 * Copyright (c) 2007-2008 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;

--- 17 unchanged lines hidden (view full) ---

26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Gabe Black
29 */
30
31#ifndef __BASE_BITUNION_HH__
32#define __BASE_BITUNION_HH__
33
34#include <iostream>
35#include <type_traits>
36
34#include "base/bitfield.hh"
35
36// The following implements the BitUnion system of defining bitfields
37//on top of an underlying class. This is done through the pervasive use of
38//both named and unnamed unions which all contain the same actual storage.
39//Since they're unioned with each other, all of these storage locations
40//overlap. This allows all of the bitfields to manipulate the same data
41//without having to have access to each other. More details are provided with
42//the individual components.
43
37#include "base/bitfield.hh"
38
39// The following implements the BitUnion system of defining bitfields
40//on top of an underlying class. This is done through the pervasive use of
41//both named and unnamed unions which all contain the same actual storage.
42//Since they're unioned with each other, all of these storage locations
43//overlap. This allows all of the bitfields to manipulate the same data
44//without having to have access to each other. More details are provided with
45//the individual components.
46
44//This namespace is for classes which implement the backend of the BitUnion
45//stuff. Don't use any of these directly, except for the Bitfield classes in
46//the *BitfieldTypes class(es).
47namespace BitfieldBackend
47//This class wraps around another which defines getter/setter functions which
48//manipulate the underlying data. The type of the underlying data and the type
49//of the bitfield itself are inferred from the argument types of the setter
50//function.
51template<class Base>
52class BitfieldTypeImpl : public Base
48{
53{
49 //A base class for all bitfields. It instantiates the actual storage,
50 //and provides getBits and setBits functions for manipulating it. The
51 //Data template parameter is type of the underlying storage.
52 template<class Data>
53 class BitfieldBase
54 {
55 protected:
56 Data __data;
54 static_assert(std::is_empty<Base>::value,
55 "Bitfield base class must be empty.");
57
56
58 //This function returns a range of bits from the underlying storage.
59 //It relies on the "bits" function above. It's the user's
60 //responsibility to make sure that there is a properly overloaded
61 //version of this function for whatever type they want to overlay.
62 inline uint64_t
63 getBits(int first, int last) const
64 {
65 return bits(__data, first, last);
66 }
57 private:
58 using Base::setter;
67
59
68 //Similar to the above, but for settings bits with replaceBits.
69 inline void
70 setBits(int first, int last, uint64_t val)
71 {
72 replaceBits(__data, first, last, val);
73 }
60 template<typename T>
61 struct TypeDeducer;
62
63 template<typename T>
64 friend class TypeDeducer;
65
66 template<typename Type1, typename Type2>
67 struct TypeDeducer<void (Base::*)(Type1 &, Type2)>
68 {
69 typedef Type1 Storage;
70 typedef Type2 Type;
74 };
75
71 };
72
76 //This class contains all the "regular" bitfield classes. It is inherited
77 //by all BitUnions which give them access to those types.
78 template<class Type>
79 class RegularBitfieldTypes
73 protected:
74 typedef typename TypeDeducer<
75 decltype(&BitfieldTypeImpl<Base>::setter)>::Storage Storage;
76 typedef typename TypeDeducer<
77 decltype(&BitfieldTypeImpl<Base>::setter)>::Type Type;
78
79 Type getter(const Storage &storage) const = delete;
80 void setter(Storage &storage, Type val) = delete;
81
82 Storage __storage;
83
84 operator Type () const
80 {
85 {
81 protected:
82 //This class implements ordinary bitfields, that is a span of bits
83 //who's msb is "first", and who's lsb is "last".
84 template<int first, int last=first>
85 class Bitfield : public BitfieldBase<Type>
86 {
87 static_assert(first >= last,
88 "Bitfield ranges must be specified as <msb, lsb>");
86 return Base::getter(__storage);
87 }
89
88
90 public:
91 operator uint64_t () const
92 {
93 return this->getBits(first, last);
94 }
89 Type
90 operator=(const Type val)
91 {
92 Base::setter(__storage, val);
93 return val;
94 }
95
95
96 uint64_t
97 operator=(const uint64_t _data)
98 {
99 this->setBits(first, last, _data);
100 return _data;
101 }
96 Type
97 operator=(BitfieldTypeImpl<Base> const & other)
98 {
99 return *this = (Type)other;
100 }
101};
102
102
103 uint64_t
104 operator=(Bitfield<first, last> const & other)
105 {
106 return *this = (uint64_t)other;
107 }
108 };
103//A wrapper for the above class which allows setting and getting.
104template<class Base>
105class BitfieldType : public BitfieldTypeImpl<Base>
106{
107 protected:
108 using Impl = BitfieldTypeImpl<Base>;
109 using typename Impl::Type;
109
110
110 //A class which specializes the above so that it can only be read
111 //from. This is accomplished explicitly making sure the assignment
112 //operator is blocked. The conversion operator is carried through
113 //inheritance. This will unfortunately need to be copied into each
114 //bitfield type due to limitations with how templates work
115 template<int first, int last=first>
116 class BitfieldRO : public Bitfield<first, last>
117 {
118 private:
119 uint64_t
120 operator=(const uint64_t _data);
111 public:
112 operator Type () const { return Impl::operator Type(); }
113 Type operator=(const Type val) { return Impl::operator=(val); }
114 Type
115 operator=(BitfieldType<Base> const & other)
116 {
117 return Impl::operator=(other);
118 }
119};
121
120
122 uint64_t
123 operator=(const Bitfield<first, last>& other);
124 };
121//A wrapper which only supports getting.
122template<class Base>
123class BitfieldROType : public BitfieldTypeImpl<Base>
124{
125 public:
126 using Impl = BitfieldTypeImpl<Base>;
127 using typename Impl::Type;
125
128
126 //Similar to the above, but only allows writing.
127 template<int first, int last=first>
128 class BitfieldWO : public Bitfield<first, last>
129 {
130 private:
131 operator uint64_t () const;
129 Type operator=(BitfieldROType<Base> const &other) = delete;
130 operator Type () const { return Impl::operator Type(); }
131};
132
132
133 public:
134 using Bitfield<first, last>::operator=;
135 };
136 };
133//A wrapper which only supports setting.
134template <class Base>
135class BitfieldWOType : public BitfieldTypeImpl<Base>
136{
137 protected:
138 using Impl = BitfieldTypeImpl<Base>;
139 using typename Impl::Type;
137
140
138 //This class contains all the "regular" bitfield classes. It is inherited
139 //by all BitUnions which give them access to those types.
140 template<class Type>
141 class SignedBitfieldTypes
141 public:
142 Type operator=(const Type val) { return Impl::operator=(val); }
143 Type
144 operator=(BitfieldWOType<Base> const & other)
142 {
145 {
146 return Impl::operator=(other);
147 }
148};
149
150//This namespace is for classes which implement the backend of the BitUnion
151//stuff. Don't use any of these directly.
152namespace BitfieldBackend
153{
154 template<class Storage, int first, int last>
155 class Unsigned
156 {
157 static_assert(first >= last,
158 "Bitfield ranges must be specified as <msb, lsb>");
159
143 protected:
160 protected:
144 //This class implements ordinary bitfields, that is a span of bits
145 //who's msb is "first", and who's lsb is "last".
146 template<int first, int last=first>
147 class SignedBitfield : public BitfieldBase<Type>
161 uint64_t
162 getter(const Storage &storage) const
148 {
163 {
149 public:
150 operator int64_t () const
151 {
152 return sext<first - last + 1>(this->getBits(first, last));
153 }
164 return bits(storage, first, last);
165 }
154
166
155 int64_t
156 operator=(const int64_t _data)
157 {
158 this->setBits(first, last, _data);
159 return _data;
160 }
167 void
168 setter(Storage &storage, uint64_t val)
169 {
170 replaceBits(storage, first, last, val);
171 }
172 };
161
173
162 int64_t
163 operator=(SignedBitfield<first, last> const & other)
164 {
165 return *this = (int64_t)other;
166 }
167 };
174 template<class Storage, int first, int last>
175 class Signed
176 {
177 static_assert(first >= last,
178 "Bitfield ranges must be specified as <msb, lsb>");
168
179
169 //A class which specializes the above so that it can only be read
170 //from. This is accomplished explicitly making sure the assignment
171 //operator is blocked. The conversion operator is carried through
172 //inheritance. This will unfortunately need to be copied into each
173 //bitfield type due to limitations with how templates work
174 template<int first, int last=first>
175 class SignedBitfieldRO : public SignedBitfield<first, last>
180 protected:
181 int64_t
182 getter(const Storage &storage) const
176 {
183 {
177 private:
178 int64_t
179 operator=(const int64_t _data);
184 return sext<first - last + 1>(bits(storage, first, last));
185 }
180
186
181 int64_t
182 operator=(const SignedBitfield<first, last>& other);
183 };
187 void
188 setter(Storage &storage, int64_t val)
189 {
190 replaceBits(storage, first, last, val);
191 }
192 };
184
193
185 //Similar to the above, but only allows writing.
194 //This class contains the basic bitfield types which are automatically
195 //available within a BitUnion. They inherit their Storage type from the
196 //containing BitUnion.
197 template<class Storage>
198 class BitfieldTypes
199 {
200 protected:
201
186 template<int first, int last=first>
202 template<int first, int last=first>
187 class SignedBitfieldWO : public SignedBitfield<first, last>
188 {
189 private:
190 operator int64_t () const;
203 using Bitfield = BitfieldType<Unsigned<Storage, first, last> >;
204 template<int first, int last=first>
205 using BitfieldRO =
206 BitfieldROType<Unsigned<Storage, first, last> >;
207 template<int first, int last=first>
208 using BitfieldWO =
209 BitfieldWOType<Unsigned<Storage, first, last> >;
191
210
192 public:
193 using SignedBitfield<first, last>::operator=;
194 };
211 template<int first, int last=first>
212 using SignedBitfield =
213 BitfieldType<Signed<Storage, first, last> >;
214 template<int first, int last=first>
215 using SignedBitfieldRO =
216 BitfieldROType<Signed<Storage, first, last> >;
217 template<int first, int last=first>
218 using SignedBitfieldWO =
219 BitfieldWOType<Signed<Storage, first, last> >;
195 };
196
220 };
221
197 template<class Type>
198 class BitfieldTypes : public RegularBitfieldTypes<Type>,
199 public SignedBitfieldTypes<Type>
200 {};
201
202 //When a BitUnion is set up, an underlying class is created which holds
203 //the actual union. This class then inherits from it, and provids the
204 //implementations for various operators. Setting things up this way
205 //prevents having to redefine these functions in every different BitUnion
206 //type. More operators could be implemented in the future, as the need
207 //arises.
222 //When a BitUnion is set up, an underlying class is created which holds
223 //the actual union. This class then inherits from it, and provids the
224 //implementations for various operators. Setting things up this way
225 //prevents having to redefine these functions in every different BitUnion
226 //type. More operators could be implemented in the future, as the need
227 //arises.
208 template <class Type, class Base>
228 template
209 class BitUnionOperators : public Base
210 {
229 class BitUnionOperators : public Base
230 {
231 static_assert(sizeof(Base) == sizeof(typename Base::__StorageType),
232 "BitUnion larger than its storage type.");
233
211 public:
234 public:
212 BitUnionOperators(Type const & _data)
235 BitUnionOperators(typename Base::__StorageType const &val)
213 {
236 {
214 Base::__data = _data;
237 Base::__storage = val;
215 }
216
217 BitUnionOperators() {}
218
238 }
239
240 BitUnionOperators() {}
241
219 operator const Type () const
242 operator const typename Base::__StorageType () const
220 {
243 {
221 return Base::__data;
244 return Base::__storage;
222 }
223
245 }
246
224 Type
225 operator=(Type const & _data)
247 typename Base::__StorageType
248 operator=(typename Base::__StorageType const &val)
226 {
249 {
227 Base::__data = _data;
228 return _data;
250 Base::__storage = val;
251 return val;
229 }
230
252 }
253
231 Type
232 operator=(BitUnionOperators const & other)
254 typename Base::__StorageType
255 operator=(BitUnionOperators const &other)
233 {
256 {
234 Base::__data = other;
235 return Base::__data;
257 Base::__storage = other;
258 return Base::__storage;
236 }
237
238 bool
259 }
260
261 bool
239 operator<(Base const & base) const
262 operator<(Base const &base) const
240 {
263 {
241 return Base::__data < base.__data;
264 return Base::__storage < base.__storage;
242 }
243
244 bool
265 }
266
267 bool
245 operator==(Base const & base) const
268 operator==(Base const &base) const
246 {
269 {
247 return Base::__data == base.__data;
270 return Base::__storage == base.__storage;
248 }
249 };
250}
251
252//This macro is a backend for other macros that specialize it slightly.
253//First, it creates/extends a namespace "BitfieldUnderlyingClasses" and
254//sticks the class which has the actual union in it, which
255//BitfieldOperators above inherits from. Putting these classes in a special
256//namespace ensures that there will be no collisions with other names as long
257//as the BitUnion names themselves are all distinct and nothing else uses
258//the BitfieldUnderlyingClasses namespace, which is unlikely. The class itself
271 }
272 };
273}
274
275//This macro is a backend for other macros that specialize it slightly.
276//First, it creates/extends a namespace "BitfieldUnderlyingClasses" and
277//sticks the class which has the actual union in it, which
278//BitfieldOperators above inherits from. Putting these classes in a special
279//namespace ensures that there will be no collisions with other names as long
280//as the BitUnion names themselves are all distinct and nothing else uses
281//the BitfieldUnderlyingClasses namespace, which is unlikely. The class itself
259//creates a typedef of the "type" parameter called __DataType. This allows
282//creates a typedef of the "type" parameter called __StorageType. This allows
260//the type to propagate outside of the macro itself in a controlled way.
261//Finally, the base storage is defined which BitfieldOperators will refer to
262//in the operators it defines. This macro is intended to be followed by
263//bitfield definitions which will end up inside it's union. As explained
283//the type to propagate outside of the macro itself in a controlled way.
284//Finally, the base storage is defined which BitfieldOperators will refer to
285//in the operators it defines. This macro is intended to be followed by
286//bitfield definitions which will end up inside it's union. As explained
264//above, these is overlayed the __data member in its entirety by each of the
287//above, these is overlayed the __storage member in its entirety by each of the
265//bitfields which are defined in the union, creating shared storage with no
266//overhead.
267#define __BitUnion(type, name) \
268 class BitfieldUnderlyingClasses##name : \
269 public BitfieldBackend::BitfieldTypes<type> \
270 { \
271 public: \
288//bitfields which are defined in the union, creating shared storage with no
289//overhead.
290#define __BitUnion(type, name) \
291 class BitfieldUnderlyingClasses##name : \
292 public BitfieldBackend::BitfieldTypes<type> \
293 { \
294 public: \
272 typedef type __DataType; \
295 typedef type __StorageType; \
273 union { \
296 union { \
274 type __data;\
297 type __storage;
275
276//This closes off the class and union started by the above macro. It is
277//followed by a typedef which makes "name" refer to a BitfieldOperator
278//class inheriting from the class and union just defined, which completes
279//building up the type for the user.
280#define EndBitUnion(name) \
281 }; \
282 }; \
283 typedef BitfieldBackend::BitUnionOperators< \
298
299//This closes off the class and union started by the above macro. It is
300//followed by a typedef which makes "name" refer to a BitfieldOperator
301//class inheriting from the class and union just defined, which completes
302//building up the type for the user.
303#define EndBitUnion(name) \
304 }; \
305 }; \
306 typedef BitfieldBackend::BitUnionOperators< \
284 BitfieldUnderlyingClasses##name::__DataType, \
285 BitfieldUnderlyingClasses##name> name;
286
287//This sets up a bitfield which has other bitfields nested inside of it. The
307 BitfieldUnderlyingClasses##name> name;
308
309//This sets up a bitfield which has other bitfields nested inside of it. The
288//__data member functions like the "underlying storage" of the top level
310//__storage member functions like the "underlying storage" of the top level
289//BitUnion. Like everything else, it overlays with the top level storage, so
290//making it a regular bitfield type makes the entire thing function as a
291//regular bitfield when referred to by itself.
311//BitUnion. Like everything else, it overlays with the top level storage, so
312//making it a regular bitfield type makes the entire thing function as a
313//regular bitfield when referred to by itself.
292#define __SubBitUnion(fieldType, first, last, name) \
293 class : public BitfieldBackend::BitfieldTypes<__DataType> \
314#define __SubBitUnion(name, fieldType, ...) \
315 class \
294 { \
295 public: \
296 union { \
316 { \
317 public: \
318 union { \
297 fieldType<first, last> __data;
319 fieldType<__VA_ARGS__> __storage;
298
299//This closes off the union created above and gives it a name. Unlike the top
300//level BitUnion, we're interested in creating an object instead of a type.
301//The operators are defined in the macro itself instead of a class for
302//technical reasons. If someone determines a way to move them to one, please
303//do so.
304#define EndSubBitUnion(name) \
305 }; \
320
321//This closes off the union created above and gives it a name. Unlike the top
322//level BitUnion, we're interested in creating an object instead of a type.
323//The operators are defined in the macro itself instead of a class for
324//technical reasons. If someone determines a way to move them to one, please
325//do so.
326#define EndSubBitUnion(name) \
327 }; \
306 inline operator __DataType () const \
307 { return __data; } \
328 inline operator __StorageType () const \
329 { return __storage; } \
308 \
330 \
309 inline __DataType operator = (const __DataType & _data) \
310 { return __data = _data;} \
331 inline __StorageType operator = (const __StorageType & _storage) \
332 { return __storage = _storage;} \
311 } name;
312
313//Regular bitfields
314//These define macros for read/write regular bitfield based subbitfields.
315#define SubBitUnion(name, first, last) \
333 } name;
334
335//Regular bitfields
336//These define macros for read/write regular bitfield based subbitfields.
337#define SubBitUnion(name, first, last) \
316 __SubBitUnion(Bitfield, first, last, name)
338 __SubBitUnion(name, Bitfield, first, last)
317
318//Regular bitfields
319//These define macros for read/write regular bitfield based subbitfields.
320#define SignedSubBitUnion(name, first, last) \
339
340//Regular bitfields
341//These define macros for read/write regular bitfield based subbitfields.
342#define SignedSubBitUnion(name, first, last) \
321 __SubBitUnion(SignedBitfield, first, last, name)
343 __SubBitUnion(name, SignedBitfield, first, last)
322
323//Use this to define an arbitrary type overlayed with bitfields.
324#define BitUnion(type, name) __BitUnion(type, name)
325
326//Use this to define conveniently sized values overlayed with bitfields.
327#define BitUnion64(name) __BitUnion(uint64_t, name)
328#define BitUnion32(name) __BitUnion(uint32_t, name)
329#define BitUnion16(name) __BitUnion(uint16_t, name)
330#define BitUnion8(name) __BitUnion(uint8_t, name)
331
332#endif // __BASE_BITUNION_HH__
344
345//Use this to define an arbitrary type overlayed with bitfields.
346#define BitUnion(type, name) __BitUnion(type, name)
347
348//Use this to define conveniently sized values overlayed with bitfields.
349#define BitUnion64(name) __BitUnion(uint64_t, name)
350#define BitUnion32(name) __BitUnion(uint32_t, name)
351#define BitUnion16(name) __BitUnion(uint16_t, name)
352#define BitUnion8(name) __BitUnion(uint8_t, name)
353
354#endif // __BASE_BITUNION_HH__