From 3e6d911ce9c01729af3b5d3093ccba250dfdfe2e Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 14 May 2019 12:54:29 +0700 Subject: more clean up use inline bit funciton instead of macros --- src/common/tusb_common.h | 20 +++----------------- src/common/tusb_compiler.h | 24 +++++++++++++++--------- 2 files changed, 18 insertions(+), 26 deletions(-) (limited to 'src/common') diff --git a/src/common/tusb_common.h b/src/common/tusb_common.h index 7c5f11dc2..82793d042 100644 --- a/src/common/tusb_common.h +++ b/src/common/tusb_common.h @@ -55,11 +55,7 @@ #define U32_TO_U8S_BE(u32) U32_B1_U8(u32), U32_B2_U8(u32), U32_B3_U8(u32), U32_B4_U8(u32) #define U32_TO_U8S_LE(u32) U32_B4_U8(u32), U32_B3_U8(u32), U32_B2_U8(u32), U32_B1_U8(u32) -//------------- Bit -------------// -#define TU_BIT(n) (1U << (n)) ///< n-th Bit -#define TU_BIT_SET(x, n) ( (x) | TU_BIT(n) ) ///< set n-th bit of x to 1 -#define TU_BIT_CLEAR(x, n) ( (x) & (~TU_BIT(n)) ) ///< clear n-th bit of x -#define TU_BIT_TEST(x, n) ( ((x) & TU_BIT(n)) ? true : false ) ///< check if n-th bit of x is 1 +#define TU_BIT(n) (1U << (n)) // for declaration of reserved field, make use of _TU_COUNTER_ #define TU_RESERVED XSTRING_CONCAT_(reserved, _TU_COUNTER_) @@ -89,14 +85,6 @@ #define tu_memclr(buffer, size) memset((buffer), 0, (size)) #define tu_varclr(_var) tu_memclr(_var, sizeof(*(_var))) -static inline bool tu_mem_test_zero (void const* buffer, uint32_t size) -{ - uint8_t const* p_mem = (uint8_t const*) buffer; - for(uint32_t i=0; i y) ? x : // Align static inline uint32_t tu_align32 (uint32_t value) { return (value & 0xFFFFFFE0UL); } static inline uint32_t tu_align16 (uint32_t value) { return (value & 0xFFFFFFF0UL); } -static inline uint32_t tu_align_n (uint32_t alignment, uint32_t value) { return value & ((uint32_t) ~(alignment-1)); } static inline uint32_t tu_align4k (uint32_t value) { return (value & 0xFFFFF000UL); } - static inline uint32_t tu_offset4k(uint32_t value) { return (value & 0xFFFUL); } //------------- Mathematics -------------// @@ -164,9 +150,9 @@ static inline uint8_t tu_log2(uint32_t value) } // Bit -static inline uint32_t tu_bit_set(uint32_t value, uint8_t n) { return value | TU_BIT(n); } +static inline uint32_t tu_bit_set (uint32_t value, uint8_t n) { return value | TU_BIT(n); } static inline uint32_t tu_bit_clear(uint32_t value, uint8_t n) { return value & (~TU_BIT(n)); } -static inline bool tu_bit_test(uint32_t value, uint8_t n) { return (value & TU_BIT(n)) ? true : false; } +static inline bool tu_bit_test (uint32_t value, uint8_t n) { return (value & TU_BIT(n)) ? true : false; } /*------------------------------------------------------------------*/ /* Count number of arguments of __VA_ARGS__ diff --git a/src/common/tusb_compiler.h b/src/common/tusb_compiler.h index 9536873cf..0978d1c0f 100644 --- a/src/common/tusb_compiler.h +++ b/src/common/tusb_compiler.h @@ -43,15 +43,16 @@ #define _TU_COUNTER_ __LINE__ #endif -//--------------------------------------------------------------------+ -// Compile-time Assert (use TU_VERIFY_STATIC to avoid name conflict) -//--------------------------------------------------------------------+ +// Compile-time Assert #if __STDC_VERSION__ >= 201112L #define TU_VERIFY_STATIC _Static_assert #else #define TU_VERIFY_STATIC(const_expr, _mess) enum { XSTRING_CONCAT_(_verify_static_, _TU_COUNTER_) = 1/(!!(const_expr)) } #endif +//--------------------------------------------------------------------+ +// Compiler porting with Attribute and Endian +//--------------------------------------------------------------------+ #if defined(__GNUC__) #define ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes))) #define ATTR_SECTION(sec_name) __attribute__ ((section(#sec_name))) @@ -61,15 +62,20 @@ #define ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess))) // warn if function with this attribute is used #define ATTR_UNUSED __attribute__ ((unused)) // Function/Variable is meant to be possibly unused - #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ // Endian conversion use well-known host to network (big endian) naming - #define tu_htonl(x) __builtin_bswap32(x) - #define tu_ntohl(x) tu_htonl(x) + #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + #define tu_htonl(u32) __builtin_bswap32(u32) + #define tu_ntohl(u32) __builtin_bswap32(u32) - #define tu_htons(u16) __builtin_bswap16(u16) - #define tu_ntohs(u16) tu_htons(u16) - #endif + #define tu_htons(u16) __builtin_bswap16(u16) + #define tu_ntohs(u16) __builtin_bswap16(u16) + #else + #define tu_htonl(u32) (u32) + #define tu_ntohl(u32) (u32) + #define tu_htons(u16) (u16) + #define tu_ntohs(u16) (u16) + #endif #else #error "Compiler attribute porting are required" #endif -- cgit v1.3.1