X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fcpu%2Fbyteorder.h;h=37d7a84167d97b24d3da9e150f878f461f207b97;hb=b9e1e52093d33494bf4a8ca27c14a56a25b6e0bc;hp=3dd7825e0d04b076a9fc6983398fad45b4ddc3af;hpb=4b681f454badd9055c35b60d9a3ecd8da11c4b80;p=bertos.git diff --git a/bertos/cpu/byteorder.h b/bertos/cpu/byteorder.h index 3dd7825e..37d7a841 100644 --- a/bertos/cpu/byteorder.h +++ b/bertos/cpu/byteorder.h @@ -27,13 +27,10 @@ * the GNU General Public License. * * Copyright 2004 Develer S.r.l. (http://www.develer.com/) - * * --> * * \brief Functions to convert integers to/from host byte-order. * - * \version $Id$ - * * \author Bernie Innocenti * \author Stefano Fedrigo */ @@ -43,35 +40,89 @@ #include #include +#include +#include +#include /** * Swap upper and lower bytes in a 16-bit value. */ -INLINE uint16_t swab16(uint16_t x) -{ - return ((x & (uint16_t)0x00FFU) << 8) - | ((x & (uint16_t)0xFF00U) >> 8); -} +#define SWAB16(x) ((uint16_t)(ROTR((x), 8) + \ + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint16_t)))) +/* + * On Cortex-M3, GCC 4.4 builtin implementation is slower than our own + * rot-based implementation. + */ +#if GNUC_PREREQ(4, 3) && !CPU_CM3 +#define SWAB32(x) ((uint32_t)(__builtin_bswap32((x) + \ + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint32_t))))) +#else /** * Reverse bytes in a 32-bit value (e.g.: 0x12345678 -> 0x78563412). */ -INLINE uint32_t swab32(uint32_t x) -{ - return ((x & (uint32_t)0x000000FFUL) << 24) - | ((x & (uint32_t)0x0000FF00UL) << 8) - | ((x & (uint32_t)0x00FF0000UL) >> 8) - | ((x & (uint32_t)0xFF000000UL) >> 24); -} - +#define SWAB32(x) ((uint32_t)(( \ + (ROTR(x, 8) & 0xFF00FF00) | \ + (ROTL(x, 8) & 0x00FF00FF))) + \ + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint32_t))) +#endif + +#if GNUC_PREREQ(4, 3) +#define SWAB64(x) ((uint64_t)(__builtin_bswap64((x) + \ + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint64_t))))) +#else /** * Reverse bytes in a 64-bit value. */ -INLINE uint64_t swab64(uint64_t x) -{ - return (uint64_t)swab32(x >> 32) - | ((uint64_t)swab32(x & 0xFFFFFFFFUL) << 32); -} +#define SWAB64(x) ((uint64_t)( \ + (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \ + (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \ + (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \ + (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \ + (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \ + (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \ + (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \ + (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56) + \ + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint64_t)))) +#endif + +#if CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN +#define cpu_to_le16(x) ((uint16_t)(x + \ + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint16_t)))) +#define cpu_to_le32(x) ((uint32_t)(x + \ + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint32_t)))) +#define cpu_to_le64(x) ((uint64_t)(x + \ + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint64_t)))) +#define cpu_to_be16(x) SWAB16(x) +#define cpu_to_be32(x) SWAB32(x) +#define cpu_to_be64(x) SWAB64(x) +#elif CPU_BYTE_ORDER == CPU_BIG_ENDIAN +#define cpu_to_le16(x) SWAB16(x) +#define cpu_to_le32(x) SWAB32(x) +#define cpu_to_le64(x) SWAB64(x) +#define cpu_to_be16(x) ((uint16_t)(x + \ + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint16_t)))) +#define cpu_to_be32(x) ((uint32_t)(x + \ + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint32_t)))) +#define cpu_to_be64(x) ((uint64_t)(x + \ + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint64_t)))) +#else +#error "unrecognized CPU endianness" +#endif + +#define be16_to_cpu(x) cpu_to_be16(x) +#define le16_to_cpu(x) cpu_to_le16(x) +#define be32_to_cpu(x) cpu_to_be32(x) +#define le32_to_cpu(x) cpu_to_le32(x) +#define be64_to_cpu(x) cpu_to_be64(x) +#define le64_to_cpu(x) cpu_to_le64(x) + +#define host_to_net16(x) cpu_to_be16(x) +#define net_to_host16(x) be16_to_cpu(x) +#define host_to_net32(x) cpu_to_be32(x) +#define net_to_host32(x) be32_to_cpu(x) +#define host_to_net64(x) cpu_to_be64(x) +#define net_to_host64(x) be64_to_cpu(x) /** * Reverse bytes in a float value. @@ -81,43 +132,13 @@ INLINE float swab_float(float x) /* Avoid breaking strict aliasing rules. */ char *cx = (char *)(&x); STATIC_ASSERT(sizeof(float) == 4); - #define BYTEORDER_SWAP(a, b) ((a) ^= (b) ^= (a) ^= (b)) + #define BYTEORDER_SWAP(a, b) do { (a) ^= (b); (b) ^= (a); (a) ^= (b); } while(0) BYTEORDER_SWAP(cx[0], cx[3]); BYTEORDER_SWAP(cx[1], cx[2]); #undef BYTEORDER_SWAP return x; } -INLINE uint16_t cpu_to_be16(uint16_t x) -{ - return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab16(x) : x; -} - -INLINE uint16_t cpu_to_le16(uint16_t x) -{ - return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab16(x) : x; -} - -INLINE uint32_t cpu_to_be32(uint32_t x) -{ - return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab32(x) : x; -} - -INLINE uint32_t cpu_to_le32(uint32_t x) -{ - return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab32(x) : x; -} - -INLINE uint64_t cpu_to_be64(uint64_t x) -{ - return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab64(x) : x; -} - -INLINE uint64_t cpu_to_le64(uint64_t x) -{ - return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab64(x) : x; -} - INLINE float cpu_to_be_float(float x) { return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab_float(x) : x; @@ -128,36 +149,6 @@ INLINE float cpu_to_le_float(float x) return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab_float(x) : x; } -INLINE uint16_t be16_to_cpu(uint16_t x) -{ - return cpu_to_be16(x); -} - -INLINE uint16_t le16_to_cpu(uint16_t x) -{ - return cpu_to_le16(x); -} - -INLINE uint32_t be32_to_cpu(uint32_t x) -{ - return cpu_to_be32(x); -} - -INLINE uint32_t le32_to_cpu(uint32_t x) -{ - return cpu_to_le32(x); -} - -INLINE uint64_t be64_to_cpu(uint64_t x) -{ - return cpu_to_be64(x); -} - -INLINE uint64_t le64_to_cpu(uint64_t x) -{ - return cpu_to_le64(x); -} - INLINE float be_float_to_cpu(float x) { return cpu_to_be_float(x); @@ -168,45 +159,46 @@ INLINE float le_float_to_cpu(float x) return cpu_to_le_float(x); } -INLINE uint16_t host_to_net16(uint16_t x) +INLINE float host_to_net_float(float x) { - return cpu_to_be16(x); + return cpu_to_be_float(x); } -INLINE uint16_t net_to_host16(uint16_t x) +INLINE float net_to_host_float(float x) { - return be16_to_cpu(x); + return be_float_to_cpu(x); } -INLINE uint32_t host_to_net32(uint32_t x) +#if CPU_ARM +INLINE cpu_atomic_t +cpu_atomic_xchg(volatile cpu_atomic_t *ptr, cpu_atomic_t val) { - return cpu_to_be32(x); -} + cpu_atomic_t ret; -INLINE uint32_t net_to_host32(uint32_t x) -{ - return be32_to_cpu(x); -} + asm volatile( + "swp %0, %1, [%2]" -INLINE uint64_t host_to_net64(uint64_t x) -{ - return cpu_to_be64(x); -} + : "=&r" (ret) + : "r" (val), "r" (ptr) + : "memory", "cc"); -INLINE uint64_t net_to_host64(uint64_t x) -{ - return be64_to_cpu(x); + return ret; } +#else /* CPU_ARM */ +#include -INLINE float host_to_net_float(float x) +INLINE cpu_atomic_t +cpu_atomic_xchg(volatile cpu_atomic_t *ptr, cpu_atomic_t val) { - return cpu_to_be_float(x); -} + cpu_atomic_t ret; -INLINE float net_to_host_float(float x) -{ - return be_float_to_cpu(x); + ATOMIC( + ret = *ptr; + *ptr = val; + ); + return ret; } +#endif /* CPU_ARM */ #ifdef __cplusplus @@ -214,12 +206,12 @@ INLINE float net_to_host_float(float x) template INLINE T swab(T x); -template<> INLINE uint16_t swab(uint16_t x) { return swab16(x); } -template<> INLINE uint32_t swab(uint32_t x) { return swab32(x); } -template<> INLINE uint64_t swab(uint64_t x) { return swab64(x); } -template<> INLINE int16_t swab(int16_t x) { return static_cast(swab16(static_cast(x))); } -template<> INLINE int32_t swab(int32_t x) { return static_cast(swab32(static_cast(x))); } -template<> INLINE int64_t swab(int64_t x) { return static_cast(swab64(static_cast(x))); } +template<> INLINE uint16_t swab(uint16_t x) { return SWAB16(x); } +template<> INLINE uint32_t swab(uint32_t x) { return SWAB32(x); } +template<> INLINE uint64_t swab(uint64_t x) { return SWAB64(x); } +template<> INLINE int16_t swab(int16_t x) { return static_cast(SWAB16(static_cast(x))); } +template<> INLINE int32_t swab(int32_t x) { return static_cast(SWAB32(static_cast(x))); } +template<> INLINE int64_t swab(int64_t x) { return static_cast(SWAB64(static_cast(x))); } template<> INLINE float swab(float x) { return swab_float(x); } /// Type generic conversion from CPU byte order to big-endian byte order.