From 4b9a4ae6953c29fb4fb46a048ce9736f92db3e25 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 19 Aug 2019 20:25:53 +0100 Subject: [PATCH] Use consistent casts/types for byte swap functions --- src/core/bitmath_func.hpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/core/bitmath_func.hpp b/src/core/bitmath_func.hpp index 5bbb4ca016..90688d0c05 100644 --- a/src/core/bitmath_func.hpp +++ b/src/core/bitmath_func.hpp @@ -402,14 +402,14 @@ static inline T ROR(const T x, const uint8 n) * (since it will use hardware swapping if available). * Even though they should return uint16 and uint32, we get * warnings if we don't cast those (why?) */ - #define BSWAP64(x) ((uint64)CFSwapInt64(x)) - #define BSWAP32(x) ((uint32)CFSwapInt32(x)) - #define BSWAP16(x) ((uint16)CFSwapInt16(x)) + #define BSWAP64(x) ((uint64)CFSwapInt64((uint64)x)) + #define BSWAP32(x) ((uint32)CFSwapInt32((uint32)x)) + #define BSWAP16(x) ((uint16)CFSwapInt16((uint16)x)) #elif defined(_MSC_VER) /* MSVC has intrinsics for swapping, resulting in faster code */ - #define BSWAP64(x) (_byteswap_uint64(x)) - #define BSWAP32(x) (_byteswap_ulong(x)) - #define BSWAP16(x) (_byteswap_ushort(x)) + #define BSWAP64(x) ((uint64)_byteswap_uint64((uint64)x)) + #define BSWAP32(x) ((uint32)_byteswap_ulong((uint32)x)) + #define BSWAP16(x) ((uint16)_byteswap_ushort((uint16)x)) #else /** * Perform a 64 bits endianness bitswap on x. @@ -437,7 +437,7 @@ static inline T ROR(const T x, const uint8 n) { #if !defined(__ICC) && (defined(__GNUC__) || defined(__clang__)) /* GCC >= 4.3 provides a builtin, resulting in faster code */ - return (uint32)__builtin_bswap32((int32)x); + return (uint32)__builtin_bswap32((uint32)x); #else return ((x >> 24) & 0xFF) | ((x >> 8) & 0xFF00) | ((x << 8) & 0xFF0000) | ((x << 24) & 0xFF000000); #endif /* __GNUC__ || __clang__ */ @@ -450,7 +450,12 @@ static inline T ROR(const T x, const uint8 n) */ static inline uint16 BSWAP16(uint16 x) { +#if !defined(__ICC) && (defined(__GNUC__) || defined(__clang__)) + /* GCC >= 4.3 provides a builtin, resulting in faster code */ + return (uint16)__builtin_bswap16((uint16)x); +#else return (x >> 8) | (x << 8); +#endif /* __GNUC__ || __clang__ */ } #endif /* __APPLE__ */