diff options
| author | Nathan Conrad <[email protected]> | 2019-09-13 20:28:26 -0400 |
|---|---|---|
| committer | Nathan Conrad <[email protected]> | 2019-09-13 20:28:26 -0400 |
| commit | bb7f581b6d046c1c358fab70f3640d0312527804 (patch) | |
| tree | c02b62575e7cb8c531b9b8e3e0a486b2ca2e978c /src | |
| parent | de386751b5a0e2823624c41a56b87d866314b838 (diff) | |
TI compiler quirks, and le byte swapping functions.
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/tusb_compiler.h | 83 |
1 files changed, 73 insertions, 10 deletions
diff --git a/src/common/tusb_compiler.h b/src/common/tusb_compiler.h index 09768ef88..ca294c20f 100644 --- a/src/common/tusb_compiler.h +++ b/src/common/tusb_compiler.h @@ -53,6 +53,9 @@ // for declaration of reserved field, make use of _TU_COUNTER_
#define TU_RESERVED TU_XSTRCAT(reserved, _TU_COUNTER_)
+#define TU_LITTLE_ENDIAN (0x12u)
+#define TU_BIG_ENDIAN (0x21u)
+
//--------------------------------------------------------------------+
// Compiler porting with Attribute and Endian
//--------------------------------------------------------------------+
@@ -67,20 +70,80 @@ // Endian conversion use well-known host to network (big endian) naming
#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) __builtin_bswap16(u16)
+ #define TU_BYTE_ORDER TU_LITTLE_ENDIAN
#else
- #define tu_htonl(u32) (u32)
- #define tu_ntohl(u32) (u32)
+ #define TU_BYTE_ORDER TU_BIG_ENDIAN
+ #endif
+
+ static inline uint16_t tu_bswap16(uint16_t u16)
+ {
+ return __builtin_bswap16(u16);
+ }
+
+ static inline uint16_t tu_bswap32(uint16_t u32)
+ {
+ return __builtin_bswap32(u32);
+ }
+
+ #define TU_BSWAP16
+
+#elif defined(__TI_COMPILER_VERSION__)
+ #define TU_ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes)))
+ #define TU_ATTR_SECTION(sec_name) __attribute__ ((section(#sec_name)))
+ #define TU_ATTR_PACKED __attribute__ ((packed))
+ #define TU_ATTR_PREPACKED
+ #define TU_ATTR_WEAK __attribute__ ((weak))
+ #define TU_ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess))) // warn if function with this attribute is used
+ #define TU_ATTR_UNUSED __attribute__ ((unused)) // Function/Variable is meant to be possibly unused
- #define tu_htons(u16) (u16)
- #define tu_ntohs(u16) (u16)
+ // __BYTE_ORDER is defined in the TI ARM compiler, but not MSP430 (which is little endian)
+ #if ((__BYTE_ORDER__) == (__ORDER_LITTLE_ENDIAN__)) || defined(__MSP430__)
+ #define TU_BYTE_ORDER TU_LITTLE_ENDIAN
+ #else
+ #define TU_BYTE_ORDER TU_BIG_ENDIAN
#endif
+
+ static inline uint16_t tu_bswap16(uint16_t u16)
+ {
+ return __builtin_bswap16(u16);
+ }
+
+ static inline uint16_t tu_bswap32(uint16_t u32)
+ {
+ return __builtin_bswap32(u32);
+ }
+#else
+ #error "Compiler attribute porting is required"
+#endif
+
+#if (TU_BYTE_ORDER == TU_LITTLE_ENDIAN)
+ #define tu_htonl(u32) tu_bswap32(u32)
+ #define tu_ntohl(u32) tu_bswap32(u32)
+
+ #define tu_htons(u16) tu_bswap16(u16)
+ #define tu_ntohs(u16) tu_bswap16(u16)
+
+ #define tu_htole16(x) (x)
+ #define tu_le16toh(x) (x)
+
+ #define tu_htole32(x) (x)
+ #define tu_le32toh(x) (x)
+
+#elif (TU_BYTE_ORDER == TU_BIG_ENDIAN)
+ #define tu_htonl(u32) (x)
+ #define tu_ntohl(u32) (x)
+
+ #define tu_htons(u16) (x)
+ #define tu_ntohs(u16) (x)
+
+ #define tu_htole16(x) tu_bswap16(u32)
+ #define tu_le16toh(x) tu_bswap16(u32)
+
+ #define tu_htole32(x) tu_bswap32(u32)
+ #define tu_le32toh(x) tu_bswap32(u32)
+
#else
- #error "Compiler attribute porting are required"
+ #error Byte order is undefined
#endif
#endif /* _TUSB_COMPILER_H_ */
|
