tinyusb
binary.h
Go to the documentation of this file.
1 /**************************************************************************/
37 /**************************************************************************/
38 
43 #ifndef _TUSB_BINARY_H_
44 #define _TUSB_BINARY_H_
45 
46 #ifdef __cplusplus
47  extern "C" {
48 #endif
49 
50 #include "primitive_types.h"
51 #include "compiler/compiler.h"
52 
53 //------------- Bit manipulation -------------//
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 )
58 
59 static inline uint32_t bit_set(uint32_t value, uint8_t n) ATTR_CONST ATTR_ALWAYS_INLINE;
60 static inline uint32_t bit_set(uint32_t value, uint8_t n)
61 {
62  return value | BIT_(n);
63 }
64 
65 static inline uint32_t bit_clear(uint32_t value, uint8_t n) ATTR_CONST ATTR_ALWAYS_INLINE;
66 static inline uint32_t bit_clear(uint32_t value, uint8_t n)
67 {
68  return value & (~BIT_(n));
69 }
70 
71 static inline bool bit_test(uint32_t value, uint8_t n) ATTR_CONST ATTR_ALWAYS_INLINE;
72 static inline bool bit_test(uint32_t value, uint8_t n)
73 {
74  return (value & BIT_(n)) ? true : false;
75 }
76 
78 static inline uint32_t bit_mask(uint8_t n) ATTR_CONST ATTR_ALWAYS_INLINE;
79 static inline uint32_t bit_mask(uint8_t n)
80 {
81  return (n < 32) ? ( BIT_(n) - 1 ) : UINT32_MAX;
82 }
83 
84 static inline uint32_t bit_mask_range(uint8_t start, uint32_t end) ATTR_CONST ATTR_ALWAYS_INLINE;
85 static inline uint32_t bit_mask_range(uint8_t start, uint32_t end)
86 {
87  return bit_mask(end+1) & ~ bit_mask(start);
88 }
89 
90 static inline uint32_t bit_set_range(uint32_t value, uint8_t start, uint8_t end, uint32_t pattern) ATTR_CONST ATTR_ALWAYS_INLINE;
91 static inline uint32_t bit_set_range(uint32_t value, uint8_t start, uint8_t end, uint32_t pattern)
92 {
93  return ( value & ~bit_mask_range(start, end) ) | (pattern << start);
94 }
95 
96 
97 //------------- Binary Constant -------------//
98 #if defined(__GNUC__) && !defined(__CC_ARM)
99 
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))
103 
104 #else
105 
106 // internal macro of B8, B16, B32
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))
115 
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) \
122  + BIN8(dlsb))
123 #endif
124 
125 #ifdef __cplusplus
126 }
127 #endif
128 
129 #endif /* _TUSB_BINARY_H_ */
130 
#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