bitfield.hh (4258:a84b8cce90ce) | bitfield.hh (4259:ca1ca13665ba) |
---|---|
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; --- 116 unchanged lines hidden (view full) --- 125 if (bits(val, 31,16)) { msb += 16; val >>= 16; } 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 | 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; --- 116 unchanged lines hidden (view full) --- 125 if (bits(val, 31,16)) { msb += 16; val >>= 16; } 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 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 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. |
|
133namespace BitfieldBackend 134{ | 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. |
|
135 template<class Data> 136 class BitfieldBase 137 { 138 protected: 139 Data __data; 140 | 148 template<class Data> 149 class BitfieldBase 150 { 151 protected: 152 Data __data; 153 |
154 //This function returns a range of bits from the underlying storage. 155 //It relies on the "bits" function above. It's the user's 156 //responsibility to make sure that there is a properly overloaded 157 //version of this function for whatever type they want to overlay. |
|
141 inline uint64_t 142 getBits(int first, int last) 143 { 144 return bits(__data, first, last); 145 } 146 | 158 inline uint64_t 159 getBits(int first, int last) 160 { 161 return bits(__data, first, last); 162 } 163 |
164 //Similar to the above, but for settings bits with replaceBits. |
|
147 inline void 148 setBits(int first, int last, uint64_t val) 149 { 150 replaceBits(__data, first, last, val); 151 } 152 }; 153 | 165 inline void 166 setBits(int first, int last, uint64_t val) 167 { 168 replaceBits(__data, first, last, val); 169 } 170 }; 171 |
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. |
|
154 template<class Type, class Base> 155 class _BitfieldRO : public Base 156 { 157 public: 158 operator const Type () 159 { 160 return *((Base *)this); 161 } 162 }; 163 | 175 template<class Type, class Base> 176 class _BitfieldRO : public Base 177 { 178 public: 179 operator const Type () 180 { 181 return *((Base *)this); 182 } 183 }; 184 |
185 //Similar to the above, but only allows writing. |
|
164 template<class Type, class Base> 165 class _BitfieldWO : public Base 166 { 167 public: 168 const Type operator = (const Type & _data) 169 { 170 *((Base *)this) = _data; 171 return _data; 172 } 173 }; 174 | 186 template<class Type, class Base> 187 class _BitfieldWO : public Base 188 { 189 public: 190 const Type operator = (const Type & _data) 191 { 192 *((Base *)this) = _data; 193 return _data; 194 } 195 }; 196 |
197 //This class implements ordinary bitfields, that is a span of bits 198 //who's msb is "first", and who's lsb is "last". |
|
175 template<class Data, int first, int last=first> 176 class _Bitfield : public BitfieldBase<Data> 177 { 178 public: 179 operator const Data () 180 { 181 return this->getBits(first, last); 182 } 183 184 const Data 185 operator = (const Data & _data) 186 { 187 this->setBits(first, last, _data); 188 return _data; 189 } 190 }; 191 | 199 template<class Data, int first, int last=first> 200 class _Bitfield : public BitfieldBase<Data> 201 { 202 public: 203 operator const Data () 204 { 205 return this->getBits(first, last); 206 } 207 208 const Data 209 operator = (const Data & _data) 210 { 211 this->setBits(first, last, _data); 212 return _data; 213 } 214 }; 215 |
216 //When a BitUnion is set up, an underlying class is created which holds 217 //the actual union. This class then inherits from it, and provids the 218 //implementations for various operators. Setting things up this way 219 //prevents having to redefine these functions in every different BitUnion 220 //type. More operators could be implemented in the future, as the need 221 //arises. |
|
192 template <class Type, class Base> 193 class BitUnionOperators : public Base 194 { 195 public: 196 operator const Type () 197 { 198 return Base::__data; 199 } --- 13 unchanged lines hidden (view full) --- 213 bool 214 operator == (const Base & base) 215 { 216 return Base::__data == base.__data; 217 } 218 }; 219} 220 | 222 template <class Type, class Base> 223 class BitUnionOperators : public Base 224 { 225 public: 226 operator const Type () 227 { 228 return Base::__data; 229 } --- 13 unchanged lines hidden (view full) --- 243 bool 244 operator == (const Base & base) 245 { 246 return Base::__data == base.__data; 247 } 248 }; 249} 250 |
251//This macro is a backend for other macros that specialize it slightly. 252//First, it creates/extends a namespace "BitfieldUnderlyingClasses" and 253//sticks the class which has the actual union in it, which 254//BitfieldOperators above inherits from. Putting these classes in a special 255//namespace ensures that there will be no collisions with other names as long 256//as the BitUnion names themselves are all distinct and nothing else uses 257//the BitfieldUnderlyingClasses namespace, which is unlikely. The class itself 258//creates a typedef of the "type" parameter called __DataType. This allows 259//the type to propagate outside of the macro itself in a controlled way. 260//Finally, the base storage is defined which BitfieldOperators will refer to 261//in the operators it defines. This macro is intended to be followed by 262//bitfield definitions which will end up inside it's union. As explained 263//above, these is overlayed the __data member in its entirety by each of the 264//bitfields which are defined in the union, creating shared storage with no 265//overhead. |
|
221#define __BitUnion(type, name) \ 222 namespace BitfieldUnderlyingClasses \ 223 { \ 224 class name; \ 225 } \ 226 class BitfieldUnderlyingClasses::name { \ 227 public: \ 228 typedef type __DataType; \ 229 union { \ 230 type __data;\ 231 | 266#define __BitUnion(type, name) \ 267 namespace BitfieldUnderlyingClasses \ 268 { \ 269 class name; \ 270 } \ 271 class BitfieldUnderlyingClasses::name { \ 272 public: \ 273 typedef type __DataType; \ 274 union { \ 275 type __data;\ 276 |
277//This closes off the class and union started by the above macro. It is 278//followed by a typedef which makes "name" refer to a BitfieldOperator 279//class inheriting from the class and union just defined, which completes 280//building up the type for the user. |
|
232#define EndBitUnion(name) \ 233 }; \ 234 }; \ 235 typedef BitfieldBackend::BitUnionOperators< \ 236 BitfieldUnderlyingClasses::name::__DataType, \ 237 BitfieldUnderlyingClasses::name> name; 238 | 281#define EndBitUnion(name) \ 282 }; \ 283 }; \ 284 typedef BitfieldBackend::BitUnionOperators< \ 285 BitfieldUnderlyingClasses::name::__DataType, \ 286 BitfieldUnderlyingClasses::name> name; 287 |
288//This sets up a bitfield which has other bitfields nested inside of it. The 289//__data member functions like the "underlying storage" of the top level 290//BitUnion. Like everything else, it overlays with the top level storage, so 291//making it a regular bitfield type makes the entire thing function as a 292//regular bitfield when referred to by itself. The operators are defined in 293//the macro itself instead of a class for technical reasons. If someone 294//determines a way to move them to one, please do so. |
|
239#define __SubBitUnion(type, name) \ 240 union { \ 241 type __data; \ 242 inline operator const __DataType () \ 243 { return __data; } \ 244 \ 245 inline const __DataType operator = (const __DataType & _data) \ 246 { __data = _data; } 247 | 295#define __SubBitUnion(type, name) \ 296 union { \ 297 type __data; \ 298 inline operator const __DataType () \ 299 { return __data; } \ 300 \ 301 inline const __DataType operator = (const __DataType & _data) \ 302 { __data = _data; } 303 |
304//This closes off the union created above and gives it a name. Unlike the top 305//level BitUnion, we're interested in creating an object instead of a type. |
|
248#define EndSubBitUnion(name) } name; 249 | 306#define EndSubBitUnion(name) } name; 307 |
250//This is so we can send in parameters with commas | 308//The preprocessor will treat everything inside of parenthesis as a single 309//argument even if it has commas in it. This is used to pass in templated 310//classes which typically have commas to seperate their parameters. |
251#define wrap(guts) guts 252 253//Read only bitfields | 311#define wrap(guts) guts 312 313//Read only bitfields |
314//This wraps another bitfield class inside a _BitfieldRO class using 315//inheritance. As explained above, the _BitfieldRO class only passes through 316//the conversion operator, so the underlying bitfield can then only be read 317//from. |
|
254#define __BitfieldRO(base) \ 255 BitfieldBackend::_BitfieldRO<__DataType, base> 256#define __SubBitUnionRO(name, base) \ 257 __SubBitUnion(wrap(_BitfieldRO<__DataType, base>), name) 258 259//Write only bitfields | 318#define __BitfieldRO(base) \ 319 BitfieldBackend::_BitfieldRO<__DataType, base> 320#define __SubBitUnionRO(name, base) \ 321 __SubBitUnion(wrap(_BitfieldRO<__DataType, base>), name) 322 323//Write only bitfields |
324//Similar to above, but for making write only versions of bitfields with 325//_BitfieldWO. |
|
260#define __BitfieldWO(base) \ 261 BitfieldBackend::_BitfieldWO<__DataType, base> 262#define __SubBitUnionWO(name, base) \ 263 __SubBitUnion(wrap(_BitfieldWO<__DataType, base>), name) 264 265//Regular bitfields | 326#define __BitfieldWO(base) \ 327 BitfieldBackend::_BitfieldWO<__DataType, base> 328#define __SubBitUnionWO(name, base) \ 329 __SubBitUnion(wrap(_BitfieldWO<__DataType, base>), name) 330 331//Regular bitfields |
332//This uses all of the above to define macros for read/write, read only, and 333//write only versions of regular bitfields. |
|
266#define Bitfield(first, last) \ 267 BitfieldBackend::_Bitfield<__DataType, first, last> 268#define SubBitUnion(name, first, last) \ 269 __SubBitUnion(Bitfield(first, last), name) 270#define BitfieldRO(first, last) __BitfieldRO(Bitfield(first, last)) 271#define SubBitUnionRO(name, first, last) \ 272 __SubBitUnionRO(Bitfield(first, last), name) 273#define BitfieldWO(first, last) __BitfieldWO(Bitfield(first, last)) 274#define SubBitUnionWO(name, first, last) \ 275 __SubBitUnionWO(Bitfield(first, last), name) 276 | 334#define Bitfield(first, last) \ 335 BitfieldBackend::_Bitfield<__DataType, first, last> 336#define SubBitUnion(name, first, last) \ 337 __SubBitUnion(Bitfield(first, last), name) 338#define BitfieldRO(first, last) __BitfieldRO(Bitfield(first, last)) 339#define SubBitUnionRO(name, first, last) \ 340 __SubBitUnionRO(Bitfield(first, last), name) 341#define BitfieldWO(first, last) __BitfieldWO(Bitfield(first, last)) 342#define SubBitUnionWO(name, first, last) \ 343 __SubBitUnionWO(Bitfield(first, last), name) 344 |
345//Use this to define an arbitrary type overlayed with bitfields. |
|
277#define BitUnion(type, name) __BitUnion(type, name) 278 | 346#define BitUnion(type, name) __BitUnion(type, name) 347 |
348//Use this to define conveniently sized values overlayed with bitfields. |
|
279#define BitUnion64(name) __BitUnion(uint64_t, name) 280#define BitUnion32(name) __BitUnion(uint32_t, name) 281#define BitUnion16(name) __BitUnion(uint16_t, name) 282#define BitUnion8(name) __BitUnion(uint8_t, name) 283 284#endif // __BASE_BITFIELD_HH__ | 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_BITFIELD_HH__ |