Created
July 15, 2011 23:27
-
-
Save rcc/1085774 to your computer and use it in GitHub Desktop.
bitfields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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