BitFields.h
Go to the documentation of this file.
1 #ifndef BITFIELDS_H
2 #define BITFIELDS_H
3 
4 #include <sys/param.h>
5 #ifndef DARWINBUILD
6  #include <endian.h> /* Use the system's endian header */
7 #endif
8 
9 // Bit Field Macros
10 //
11 // These macros allow for the correct setting and extracting of bit fields
12 // from the given size word.
13 #if __BYTE_ORDER == __LITTLE_ENDIAN
14 #define SWAB32(word) (word)
15 #define SWAB64(word) (word)
16 #elif __BYTE_ORDER == __BIG_ENDIAN
17 #define SWAB32(word) ( (((word) & 0xFF000000) >> 24) | (((word) & 0x00FF0000) >> 8) | (((word) & 0x0000FF00) << 8 | (((word) & 0x000000FF) << 24)) )
18 #define SWAB64(word) ( SWAB32( (uint32_t)((word)>>32) ) | (uint64_t)SWAB32( (uint32_t)(word) )<<32 )
19 #endif
20 
21 // #if __BYTE_ORDER == __LITTLE_ENDIAN
22 // #warning "Using Little Endian Byte Order for Unpacking"
23 #define BITFIELDGET(block, mask, shift) ((SWAB32(block )& mask) >> shift)
24 #define BITFIELDSET(block, data, mask, shift) block = SWAB32((SWAB32(block) & ~mask)| ((data << shift ) & mask))
25 
26 
27 
28 
29 #endif /* BITFIELDS_H */