43 #ifndef _TUSB_COMMON_H_
44 #define _TUSB_COMMON_H_
53 #define STRING_(x) #x // stringify without expand
54 #define XSTRING_(x) STRING_(x) // expand then stringify
55 #define STRING_CONCAT_(a, b) a##b // concat without expand
56 #define XSTRING_CONCAT_(a, b) STRING_CONCAT_(a, b) // expand then concat
88 #define XSTRING_(x) STRING_(x)
89 #define STRING_CONCAT_(a, b) a##b
90 #define XSTRING_CONCAT_(a, b) STRING_CONCAT_(a, b)
92 #define MAX_OF(a, b) ( (a) > (b) ? (a) : (b) )
93 #define MIN_OF(a, b) ( (a) < (b) ? (a) : (b) )
95 #define U16_HIGH_U8(u16) ((uint8_t) (((u16) >> 8) & 0x00ff))
96 #define U16_LOW_U8(u16) ((uint8_t) ((u16) & 0x00ff))
97 #define U16_TO_U8S_BE(u16) U16_HIGH_U8(u16), U16_LOW_U8(u16)
98 #define U16_TO_U8S_LE(u16) U16_LOW_U8(u16), U16_HIGH_U8(u16)
100 #define U32_B1_U8(u32) ((uint8_t) (((u32) >> 24) & 0x000000ff)) // MSB
101 #define U32_B2_U8(u32) ((uint8_t) (((u32) >> 16) & 0x000000ff))
102 #define U32_B3_U8(u32) ((uint8_t) (((u32) >> 8) & 0x000000ff))
103 #define U32_B4_U8(u32) ((uint8_t) ((u32) & 0x000000ff)) // LSB
105 #define U32_TO_U8S_BE(u32) U32_B1_U8(u32), U32_B2_U8(u32), U32_B3_U8(u32), U32_B4_U8(u32)
106 #define U32_TO_U8S_LE(u32) U32_B4_U8(u32), U32_B3_U8(u32), U32_B2_U8(u32), U32_B1_U8(u32)
109 #define ENDIAN_BE(u32) \
110 (uint32_t) ( (((u32) & 0xFF) << 24) | (((u32) & 0xFF00) << 8) | (((u32) >> 8) & 0xFF00) | (((u32) >> 24) & 0xFF) )
112 #define ENDIAN_BE16(le16) ((uint16_t) ((U16_LOW_U8(le16) << 8) | U16_HIGH_U8(le16)) )
115 #define __n2be_16(u16) ((uint16_t) ((U16_LOW_U8(u16) << 8) | U16_HIGH_U8(u16)) )
116 #define __be2n_16(u16) __n2be_16(u16)
122 #define memclr_(buffer, size) memset((buffer), 0, (size))
126 static inline uint8_t
const * descriptor_next(uint8_t
const p_desc[])
128 return p_desc + p_desc[DESCRIPTOR_OFFSET_LENGTH];
132 static inline uint8_t descriptor_typeof(uint8_t
const p_desc[])
134 return p_desc[DESCRIPTOR_OFFSET_TYPE];
140 static inline uint32_t
u32_from_u8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4)
142 return ( ((uint32_t) b1) << 24) + ( ((uint32_t) b2) << 16) + ( ((uint32_t) b3) << 8) + b4;
146 static inline uint8_t u16_high_u8(uint16_t u16)
148 return (uint8_t) ( ((uint16_t) (u16 >> 8)) & 0x00ff);
152 static inline uint8_t u16_low_u8(uint16_t u16)
154 return (uint8_t) (u16 & 0x00ff);
158 static inline uint16_t u16_le2be(uint16_t u16)
160 return ((uint16_t)(u16_low_u8(u16) << 8)) | u16_high_u8(u16);
164 static inline uint8_t min8_of(uint8_t x, uint8_t y) ATTR_ALWAYS_INLINE
ATTR_CONST;
165 static inline uint8_t min8_of(uint8_t x, uint8_t y)
167 return (x < y) ? x : y;
170 static inline uint16_t min16_of(uint16_t x, uint16_t y) ATTR_ALWAYS_INLINE
ATTR_CONST;
171 static inline uint16_t min16_of(uint16_t x, uint16_t y)
173 return (x < y) ? x : y;
176 static inline uint32_t min32_of(uint32_t x, uint32_t y) ATTR_ALWAYS_INLINE
ATTR_CONST;
177 static inline uint32_t min32_of(uint32_t x, uint32_t y)
179 return (x < y) ? x : y;
183 static inline uint32_t max32_of(uint32_t x, uint32_t y) ATTR_ALWAYS_INLINE
ATTR_CONST;
184 static inline uint32_t max32_of(uint32_t x, uint32_t y)
186 return (x > y) ? x : y;
189 static inline uint16_t max16_of(uint16_t x, uint16_t y) ATTR_ALWAYS_INLINE
ATTR_CONST;
190 static inline uint16_t max16_of(uint16_t x, uint16_t y)
192 return (x > y) ? x : y;
196 static inline uint32_t align32 (uint32_t value) ATTR_ALWAYS_INLINE
ATTR_CONST;
197 static inline uint32_t align32 (uint32_t value)
199 return (value & 0xFFFFFFE0UL);
202 static inline uint32_t align16 (uint32_t value) ATTR_ALWAYS_INLINE
ATTR_CONST;
203 static inline uint32_t align16 (uint32_t value)
205 return (value & 0xFFFFFFF0UL);
208 static inline uint32_t align_n (uint32_t alignment, uint32_t value) ATTR_ALWAYS_INLINE
ATTR_CONST;
209 static inline uint32_t align_n (uint32_t alignment, uint32_t value)
211 return value & ((uint32_t) ~(alignment-1));
214 static inline uint32_t align4k (uint32_t value) ATTR_ALWAYS_INLINE
ATTR_CONST;
215 static inline uint32_t align4k (uint32_t value)
217 return (value & 0xFFFFF000UL);
220 static inline uint32_t offset4k(uint32_t value) ATTR_ALWAYS_INLINE
ATTR_CONST;
221 static inline uint32_t offset4k(uint32_t value)
223 return (value & 0xFFFUL);
227 static inline uint32_t abs_of(int32_t value) ATTR_ALWAYS_INLINE
ATTR_CONST;
228 static inline uint32_t abs_of(int32_t value)
230 return (value < 0) ? (-value) : value;
235 static inline bool is_in_range(uint32_t lower, uint32_t value, uint32_t upper) ATTR_ALWAYS_INLINE
ATTR_CONST;
236 static inline bool is_in_range(uint32_t lower, uint32_t value, uint32_t upper)
238 return (lower <= value) && (value <= upper);
245 return (lower < value) && (value < upper);
249 static inline uint8_t log2_of(uint32_t value) ATTR_ALWAYS_INLINE
ATTR_CONST;
250 static inline uint8_t log2_of(uint32_t value)
262 static inline uint8_t cardinality_of(uint32_t value) ATTR_ALWAYS_INLINE
ATTR_CONST;
263 static inline uint8_t cardinality_of(uint32_t value)
272 for (count = 0; value; count++)
#define ATTR_PURE
Many functions have no effects except the return value and their return value depends only on the par...
static bool is_in_range(uint32_t lower, uint32_t value, uint32_t upper) ATTR_ALWAYS_INLINE ATTR_CONST
inclusive range checking
static uint32_t u32_from_u8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4) ATTR_ALWAYS_INLINE ATTR_CONST
form an uint32_t from 4 x uint8_t
static bool is_in_range_exclusive(uint32_t lower, uint32_t value, uint32_t upper) ATTR_ALWAYS_INLINE ATTR_CONST
exclusive range checking
#define ATTR_ALWAYS_INLINE
Generally, functions are not inlined unless optimization is specified. For functions declared inline...
#define ATTR_CONST
Many functions do not examine any values except their arguments, and have no effects except the retur...