X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fendian%2Fendian.h;h=6732e8aa8193f11a8e131a65bee1fa87fbe8495e;hp=02f5a68baf02adf94a1bcfd308c8f0a577accc48;hb=759ac0f0564104d5028acd47c3e9fdb858c96d1d;hpb=9e2c88b5b7b9b78aa16d4238ae297a0e388ffaae diff --git a/ccan/endian/endian.h b/ccan/endian/endian.h index 02f5a68b..6732e8aa 100644 --- a/ccan/endian/endian.h +++ b/ccan/endian/endian.h @@ -16,8 +16,8 @@ * }; */ #define BSWAP_16(val) \ - ((((uint16_t)val & 0x00ff) << 8) \ - | (((uint16_t)val & 0xff00) >> 8)) + ((((uint16_t)(val) & 0x00ff) << 8) \ + | (((uint16_t)(val) & 0xff00) >> 8)) /** * BSWAP_32 - reverse bytes in a constant uint32_t value. @@ -31,10 +31,10 @@ * }; */ #define BSWAP_32(val) \ - ((((uint32_t)val & 0x000000ff) << 24) \ - | (((uint32_t)val & 0x0000ff00) << 8) \ - | (((uint32_t)val & 0x00ff0000) >> 8) \ - | (((uint32_t)val & 0xff000000) >> 24)) + ((((uint32_t)(val) & 0x000000ff) << 24) \ + | (((uint32_t)(val) & 0x0000ff00) << 8) \ + | (((uint32_t)(val) & 0x00ff0000) >> 8) \ + | (((uint32_t)(val) & 0xff000000) >> 24)) /** * BSWAP_64 - reverse bytes in a constant uint64_t value. @@ -48,14 +48,14 @@ * }; */ #define BSWAP_64(val) \ - ((((uint64_t)val & 0x00000000000000ffULL) << 56) \ - | (((uint64_t)val & 0x000000000000ff00ULL) << 40) \ - | (((uint64_t)val & 0x0000000000ff0000ULL) << 24) \ - | (((uint64_t)val & 0x00000000ff000000ULL) << 8) \ - | (((uint64_t)val & 0x000000ff00000000ULL) >> 8) \ - | (((uint64_t)val & 0x0000ff0000000000ULL) >> 24) \ - | (((uint64_t)val & 0x00ff000000000000ULL) >> 40) \ - | (((uint64_t)val & 0xff00000000000000ULL) >> 56)) + ((((uint64_t)(val) & 0x00000000000000ffULL) << 56) \ + | (((uint64_t)(val) & 0x000000000000ff00ULL) << 40) \ + | (((uint64_t)(val) & 0x0000000000ff0000ULL) << 24) \ + | (((uint64_t)(val) & 0x00000000ff000000ULL) << 8) \ + | (((uint64_t)(val) & 0x000000ff00000000ULL) >> 8) \ + | (((uint64_t)(val) & 0x0000ff0000000000ULL) >> 24) \ + | (((uint64_t)(val) & 0x00ff000000000000ULL) >> 40) \ + | (((uint64_t)(val) & 0xff00000000000000ULL) >> 56)) #if HAVE_BYTESWAP_H #include @@ -103,13 +103,22 @@ static inline uint64_t bswap_64(uint64_t val) } #endif +/* Needed for Glibc like endiness check */ +#define __LITTLE_ENDIAN 1234 +#define __BIG_ENDIAN 4321 + /* Sanity check the defines. We don't handle weird endianness. */ #if !HAVE_LITTLE_ENDIAN && !HAVE_BIG_ENDIAN #error "Unknown endian" #elif HAVE_LITTLE_ENDIAN && HAVE_BIG_ENDIAN #error "Can't compile for both big and little endian." +#elif HAVE_LITTLE_ENDIAN +#define __BYTE_ORDER __LITTLE_ENDIAN +#elif HAVE_BIG_ENDIAN +#define __BYTE_ORDER __BIG_ENDIAN #endif + #ifdef __CHECKER__ /* sparse needs forcing to remove bitwise attribute from ccan/short_types */ #define ENDIAN_CAST __attribute__((force)) @@ -279,7 +288,6 @@ static inline uint16_t le16_to_cpu(leint16_t le_val) */ static inline beint64_t cpu_to_be64(uint64_t native) { - return ((ENDIAN_CAST beint64_t)BSWAP_64(native)); return CPU_TO_BE64(native); }