43 #ifndef _TUSB_BINARY_H_
44 #define _TUSB_BINARY_H_
54 #define BIT_(n) (1U << (n))
55 #define BIT_SET_(x, n) ( (x) | BIT_(n) )
56 #define BIT_CLR_(x, n) ( (x) & (~BIT_(n)) )
57 #define BIT_TEST_(x, n) ( ((x) & BIT_(n)) ? true : false )
60 static inline uint32_t bit_set(uint32_t value, uint8_t n)
62 return value |
BIT_(n);
66 static inline uint32_t bit_clear(uint32_t value, uint8_t n)
68 return value & (~
BIT_(n));
72 static inline bool bit_test(uint32_t value, uint8_t n)
74 return (value &
BIT_(n)) ?
true :
false;
79 static inline uint32_t bit_mask(uint8_t n)
81 return (n < 32) ? (
BIT_(n) - 1 ) : UINT32_MAX;
85 static inline uint32_t bit_mask_range(uint8_t start, uint32_t end)
87 return bit_mask(end+1) & ~ bit_mask(start);
91 static inline uint32_t bit_set_range(uint32_t value, uint8_t start, uint8_t end, uint32_t pattern)
93 return ( value & ~bit_mask_range(start, end) ) | (pattern << start);
98 #if defined(__GNUC__) && !defined(__CC_ARM)
100 #define BIN8(x) ((uint8_t) (0b##x))
101 #define BIN16(b1, b2) ((uint16_t) (0b##b1##b2))
102 #define BIN32(b1, b2, b3, b4) ((uint32_t) (0b##b1##b2##b3##b4))
107 #define _B8__(x) (((x&0x0000000FUL)?1:0) \
108 +((x&0x000000F0UL)?2:0) \
109 +((x&0x00000F00UL)?4:0) \
110 +((x&0x0000F000UL)?8:0) \
111 +((x&0x000F0000UL)?16:0) \
112 +((x&0x00F00000UL)?32:0) \
113 +((x&0x0F000000UL)?64:0) \
114 +((x&0xF0000000UL)?128:0))
116 #define BIN8(d) ((uint8_t) _B8__(0x##d##UL))
117 #define BIN16(dmsb,dlsb) (((uint16_t)BIN8(dmsb)<<8) + BIN8(dlsb))
118 #define BIN32(dmsb,db2,db3,dlsb) \
119 (((uint32_t)BIN8(dmsb)<<24) \
120 + ((uint32_t)BIN8(db2)<<16) \
121 + ((uint32_t)BIN8(db3)<<8) \
#define BIT_(n)
n-th Bit
Definition: binary.h:54
#define ATTR_ALWAYS_INLINE
Generally, functions are not inlined unless optimization is specified. For functions declared inline...
Definition: compiler_gcc.h:89
#define ATTR_CONST
Many functions do not examine any values except their arguments, and have no effects except the retur...
Definition: compiler_gcc.h:100
static bool bit_test(uint32_t value, uint8_t n) ATTR_CONST ATTR_ALWAYS_INLINE
create a mask with n-bit lsb set to 1
Definition: binary.h:72