This commit is contained in:
Bertrand Songis
2019-02-11 14:51:52 +01:00
parent bf3bba3254
commit 4a6fc430ec
3 changed files with 39 additions and 21 deletions
+30 -7
View File
@@ -23,8 +23,16 @@
#ifndef BITFIELD_H
#define BITFIELD_H
#include <inttypes.h>
// A set of bitfield handling macros
#define BF_BIT(n) ( 1<<(n) )
template <typename T>
inline T BF_BIT(uint8_t n)
{
return T(1) << n;
}
#define BF_BIT_GET(y, mask) ( y & (mask) )
#define BF_BIT_SET(y, mask) ( y |= (mask) )
#define BF_BIT_CLEAR(y, mask) ( y &= ~(mask) )
@@ -33,19 +41,34 @@
#define BF_SINGLE_BIT_SET(y, i) BF_BIT_SET(y, BF_BIT(i))
//! Create a bitmask of length 'len'.
#define BF_BITMASK(len) ( BF_BIT(len)-1 )
template <typename T>
inline T BF_BITMASK(uint8_t len)
{
return BF_BIT<T>(len) - 1;
}
//! Create a bitfield mask of length 'len' starting at bit 'start'.
#define BF_MASK(start, len) ( BF_BITMASK(len)<<(start) )
template <typename T>
inline T BF_MASK(uint8_t start, uint8_t len)
{
return BF_BITMASK<T>(len) << start;
}
//! Prepare a bitmask for insertion or combining.
#define BF_PREP(x, start, len) ( ((x)&BF_BITMASK(len)) << (start) )
template <typename T>
inline T BF_PREP(T x, uint8_t start, uint8_t len)
{
return (x & BF_BITMASK<T>(len)) << start;
}
//! Extract a bitfield of length 'len' starting at bit 'start' from 'y'.
#define BF_GET(y, start, len) ( ((y)>>(start)) & BF_BITMASK(len) )
//! Insert 'len' bits of 'x 'into 'y', starting at bit 'start' from 'y'.
#define BF_SET(y, x, start, len) \
( y= ((y) &~ BF_MASK(start, len)) | BF_PREP(x, start, len) )
template <class T>
inline T BF_SET(T to, T from, uint8_t start, uint8_t len)
{
return (to & ~BF_MASK<T>(start, len)) | BF_PREP<T>(from, start, len);
}
#endif //BITFIELD_H
#endif //BITFIELD_H