Skip to content

Instantly share code, notes, and snippets.

@rcc
Created July 15, 2011 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcc/1085774 to your computer and use it in GitHub Desktop.
Save rcc/1085774 to your computer and use it in GitHub Desktop.
bitfields
#ifndef I__BITFIELD_H__
#define I__BITFIELD_H__
/* Bitfield Macros
*
* A bitfield is any range of bits in a word. A bitfield definition
* needs to specify the SIZE and SHIFT in the following format:
* #define BITFIELD_NAME SIZE:SHIFT
* Examples (X is don't care, F is the field):
* Register: XXXXXFFX = Bitfield 2:1
* Register: XXXXXFFF = Bitfield 3:0
* Register: XXXXFXXX = Bitfield 1:3
*/
#define _BFSIZE(bf) (1 ? bf)
#define _BFSHIFT(bf) (0 ? bf)
#ifndef BF
#define BF(bfdef, bfval) \
(((bfval) & ((1 << _BFSIZE(bfdef)) - 1)) << _BFSHIFT(bfdef))
#endif
#ifndef INSERTBF
#define INSERTBF(bfdef, bfval, targetval) \
do { \
(targetval) = ((targetval) \
& ~(((1 << _BFSIZE(bfdef)) - 1) << _BFSHIFT(bfdef))) \
| BF(bfdef, bfval); \
} while(0)
#endif
#ifndef GETBF
#define GETBF(bfdef, val) \
((val >> _BFSHIFT(bfdef)) & ((1 << _BFSIZE(bfdef)) - 1))
#endif
#endif /* I__BITFIELD_H__ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment