bitfield.hh (4261:0a667162b5fa) bitfield.hh (4262:e851cdcf279b)
1/*
2 * Copyright (c) 2003-2005 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;

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

126 if (bits(val, 15,8)) { msb += 8; val >>= 8; }
127 if (bits(val, 7,4)) { msb += 4; val >>= 4; }
128 if (bits(val, 3,2)) { msb += 2; val >>= 2; }
129 if (bits(val, 1,1)) { msb += 1; }
130 return msb;
131}
132
133// The following implements the BitUnion system of defining bitfields
1/*
2 * Copyright (c) 2003-2005 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;

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

126 if (bits(val, 15,8)) { msb += 8; val >>= 8; }
127 if (bits(val, 7,4)) { msb += 4; val >>= 4; }
128 if (bits(val, 3,2)) { msb += 2; val >>= 2; }
129 if (bits(val, 1,1)) { msb += 1; }
130 return msb;
131}
132
133// The following implements the BitUnion system of defining bitfields
134//on top of an underlying class. This is done through the extensive use of
134//on top of an underlying class. This is done through the pervasive use of
135//both named and unnamed unions which all contain the same actual storage.
136//Since they're unioned with each other, all of these storage locations
137//overlap. This allows all of the bitfields to manipulate the same data
135//both named and unnamed unions which all contain the same actual storage.
136//Since they're unioned with each other, all of these storage locations
137//overlap. This allows all of the bitfields to manipulate the same data
138//without having to know about each other. More details are provided with the
138//without having to have access to each other. More details are provided with the
139//individual components.
140
141//This namespace is for classes which implement the backend of the BitUnion
139//individual components.
140
141//This namespace is for classes which implement the backend of the BitUnion
142//stuff. Don't use any of this directly! Use the macros at the end instead.
142//stuff. Don't use any of these directly, except for the Bitfield classes in
143//the *BitfieldTypes class(es).
143namespace BitfieldBackend
144{
145 //A base class for all bitfields. It instantiates the actual storage,
146 //and provides getBits and setBits functions for manipulating it. The
147 //Data template parameter is type of the underlying storage.
148 template<class Data>
149 class BitfieldBase
150 {

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

164 //Similar to the above, but for settings bits with replaceBits.
165 inline void
166 setBits(int first, int last, uint64_t val)
167 {
168 replaceBits(__data, first, last, val);
169 }
170 };
171
144namespace BitfieldBackend
145{
146 //A base class for all bitfields. It instantiates the actual storage,
147 //and provides getBits and setBits functions for manipulating it. The
148 //Data template parameter is type of the underlying storage.
149 template<class Data>
150 class BitfieldBase
151 {

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

165 //Similar to the above, but for settings bits with replaceBits.
166 inline void
167 setBits(int first, int last, uint64_t val)
168 {
169 replaceBits(__data, first, last, val);
170 }
171 };
172
172 //A class which specializes a given base so that it can only be read
173 //from. This is accomplished by only passing through the conversion
174 //operator and explicitly making sure the assignment operator is blocked.
175 template<class Type, class Base>
176 class _BitfieldRO : public Base
173 //This class contains all the "regular" bitfield classes. It is inherited
174 //by all BitUnions which give them access to those types.
175 template<class Type>
176 class RegularBitfieldTypes
177 {
177 {
178 private:
179 const Type
180 operator=(const Type & _data);
181
182 public:
183 operator const Type ()
178 protected:
179 //This class implements ordinary bitfields, that is a span of bits
180 //who's msb is "first", and who's lsb is "last".
181 template<int first, int last=first>
182 class Bitfield : public BitfieldBase<Type>
184 {
183 {
185 return *((Base *)this);
186 }
187 };
184 public:
185 operator const Type ()
186 {
187 return this->getBits(first, last);
188 }
188
189
189 //Similar to the above, but only allows writing.
190 template<class Type, class Base>
191 class _BitfieldWO : public Base
192 {
193 private:
194 operator const Type ();
190 const Type
191 operator=(const Type & _data)
192 {
193 this->setBits(first, last, _data);
194 return _data;
195 }
196 };
195
197
196 public:
197 const Type operator=(const Type & _data)
198 //A class which specializes the above so that it can only be read
199 //from. This is accomplished explicitly making sure the assignment
200 //operator is blocked. The conversion operator is carried through
201 //inheritance. This will unfortunately need to be copied into each
202 //bitfield type due to limitations with how templates work
203 template<int first, int last=first>
204 class BitfieldRO : public Bitfield<first, last>
198 {
205 {
199 *((Base *)this) = _data;
200 return _data;
201 }
202 };
206 private:
207 const Type
208 operator=(const Type & _data);
209 };
203
210
204 //This class implements ordinary bitfields, that is a span of bits
205 //who's msb is "first", and who's lsb is "last".
206 template<class Data, int first, int last=first>
207 class _Bitfield : public BitfieldBase<Data>
208 {
209 public:
210 operator const Data ()
211 //Similar to the above, but only allows writing.
212 template<int first, int last=first>
213 class BitfieldWO : public Bitfield<first, last>
211 {
214 {
212 return this->getBits(first, last);
213 }
215 private:
216 operator const Type ();
214
217
215 const Data
216 operator=(const Data & _data)
217 {
218 this->setBits(first, last, _data);
219 return _data;
220 }
218 public:
219 const Type operator=(const Type & _data)
220 {
221 *((Bitfield<first, last> *)this) = _data;
222 return _data;
223 }
224 };
221 };
222
225 };
226
227 template<class Type>
228 class BitfieldTypes : public RegularBitfieldTypes<Type>
229 {};
230
223 //When a BitUnion is set up, an underlying class is created which holds
224 //the actual union. This class then inherits from it, and provids the
225 //implementations for various operators. Setting things up this way
226 //prevents having to redefine these functions in every different BitUnion
227 //type. More operators could be implemented in the future, as the need
228 //arises.
229 template <class Type, class Base>
230 class BitUnionOperators : public Base

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

270//above, these is overlayed the __data member in its entirety by each of the
271//bitfields which are defined in the union, creating shared storage with no
272//overhead.
273#define __BitUnion(type, name) \
274 namespace BitfieldUnderlyingClasses \
275 { \
276 class name; \
277 } \
231 //When a BitUnion is set up, an underlying class is created which holds
232 //the actual union. This class then inherits from it, and provids the
233 //implementations for various operators. Setting things up this way
234 //prevents having to redefine these functions in every different BitUnion
235 //type. More operators could be implemented in the future, as the need
236 //arises.
237 template <class Type, class Base>
238 class BitUnionOperators : public Base

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

278//above, these is overlayed the __data member in its entirety by each of the
279//bitfields which are defined in the union, creating shared storage with no
280//overhead.
281#define __BitUnion(type, name) \
282 namespace BitfieldUnderlyingClasses \
283 { \
284 class name; \
285 } \
278 class BitfieldUnderlyingClasses::name { \
286 class BitfieldUnderlyingClasses::name : \
287 public BitfieldBackend::BitfieldTypes<type> \
288 { \
279 public: \
280 typedef type __DataType; \
281 union { \
282 type __data;\
283
284//This closes off the class and union started by the above macro. It is
285//followed by a typedef which makes "name" refer to a BitfieldOperator
286//class inheriting from the class and union just defined, which completes

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

291 typedef BitfieldBackend::BitUnionOperators< \
292 BitfieldUnderlyingClasses::name::__DataType, \
293 BitfieldUnderlyingClasses::name> name;
294
295//This sets up a bitfield which has other bitfields nested inside of it. The
296//__data member functions like the "underlying storage" of the top level
297//BitUnion. Like everything else, it overlays with the top level storage, so
298//making it a regular bitfield type makes the entire thing function as a
289 public: \
290 typedef type __DataType; \
291 union { \
292 type __data;\
293
294//This closes off the class and union started by the above macro. It is
295//followed by a typedef which makes "name" refer to a BitfieldOperator
296//class inheriting from the class and union just defined, which completes

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

301 typedef BitfieldBackend::BitUnionOperators< \
302 BitfieldUnderlyingClasses::name::__DataType, \
303 BitfieldUnderlyingClasses::name> name;
304
305//This sets up a bitfield which has other bitfields nested inside of it. The
306//__data member functions like the "underlying storage" of the top level
307//BitUnion. Like everything else, it overlays with the top level storage, so
308//making it a regular bitfield type makes the entire thing function as a
299//regular bitfield when referred to by itself. The operators are defined in
300//the macro itself instead of a class for technical reasons. If someone
301//determines a way to move them to one, please do so.
302#define __SubBitUnion(type, name) \
309//regular bitfield when referred to by itself.
310#define __SubBitUnion(fieldType, first, last, name) \
311 class : public BitfieldBackend::BitfieldTypes<__DataType> \
312 { \
313 public: \
303 union { \
314 union { \
304 type __data; \
305 inline operator const __DataType () \
306 { return __data; } \
307 \
308 inline const __DataType operator = (const __DataType & _data) \
309 { __data = _data; }
315 fieldType<first, last> __data;
310
311//This closes off the union created above and gives it a name. Unlike the top
312//level BitUnion, we're interested in creating an object instead of a type.
316
317//This closes off the union created above and gives it a name. Unlike the top
318//level BitUnion, we're interested in creating an object instead of a type.
313#define EndSubBitUnion(name) } name;
319//The operators are defined in the macro itself instead of a class for
320//technical reasons. If someone determines a way to move them to one, please
321//do so.
322#define EndSubBitUnion(name) \
323 }; \
324 inline operator const __DataType () \
325 { return __data; } \
326 \
327 inline const __DataType operator = (const __DataType & _data) \
328 { __data = _data; } \
329 } name;
314
330
315//The preprocessor will treat everything inside of parenthesis as a single
316//argument even if it has commas in it. This is used to pass in templated
317//classes which typically have commas to seperate their parameters.
318#define wrap(guts) guts
319
320//Read only bitfields
321//This wraps another bitfield class inside a _BitfieldRO class using
322//inheritance. As explained above, the _BitfieldRO class only passes through
323//the conversion operator, so the underlying bitfield can then only be read
324//from.
325#define __BitfieldRO(base) \
326 BitfieldBackend::_BitfieldRO<__DataType, base>
327#define __SubBitUnionRO(name, base) \
328 __SubBitUnion(wrap(_BitfieldRO<__DataType, base>), name)
329
330//Write only bitfields
331//Similar to above, but for making write only versions of bitfields with
332//_BitfieldWO.
333#define __BitfieldWO(base) \
334 BitfieldBackend::_BitfieldWO<__DataType, base>
335#define __SubBitUnionWO(name, base) \
336 __SubBitUnion(wrap(_BitfieldWO<__DataType, base>), name)
337
338//Regular bitfields
331//Regular bitfields
339//This uses all of the above to define macros for read/write, read only, and
340//write only versions of regular bitfields.
341#define Bitfield(first, last) \
342 BitfieldBackend::_Bitfield<__DataType, first, last>
332//These define macros for read/write regular bitfield based subbitfields.
343#define SubBitUnion(name, first, last) \
333#define SubBitUnion(name, first, last) \
344 __SubBitUnion(Bitfield(first, last), name)
345#define BitfieldRO(first, last) __BitfieldRO(Bitfield(first, last))
346#define SubBitUnionRO(name, first, last) \
347 __SubBitUnionRO(Bitfield(first, last), name)
348#define BitfieldWO(first, last) __BitfieldWO(Bitfield(first, last))
349#define SubBitUnionWO(name, first, last) \
350 __SubBitUnionWO(Bitfield(first, last), name)
334 __SubBitUnion(Bitfield, first, last, name)
351
352//Use this to define an arbitrary type overlayed with bitfields.
353#define BitUnion(type, name) __BitUnion(type, name)
354
355//Use this to define conveniently sized values overlayed with bitfields.
356#define BitUnion64(name) __BitUnion(uint64_t, name)
357#define BitUnion32(name) __BitUnion(uint32_t, name)
358#define BitUnion16(name) __BitUnion(uint16_t, name)
359#define BitUnion8(name) __BitUnion(uint8_t, name)
360
361#endif // __BASE_BITFIELD_HH__
335
336//Use this to define an arbitrary type overlayed with bitfields.
337#define BitUnion(type, name) __BitUnion(type, name)
338
339//Use this to define conveniently sized values overlayed with bitfields.
340#define BitUnion64(name) __BitUnion(uint64_t, name)
341#define BitUnion32(name) __BitUnion(uint32_t, name)
342#define BitUnion16(name) __BitUnion(uint16_t, name)
343#define BitUnion8(name) __BitUnion(uint8_t, name)
344
345#endif // __BASE_BITFIELD_HH__