diff --git a/include/llvm/Support/MathExtras.h b/include/llvm/Support/MathExtras.h index dafb479a999..1b58b3f83c4 100644 --- a/include/llvm/Support/MathExtras.h +++ b/include/llvm/Support/MathExtras.h @@ -14,7 +14,6 @@ #ifndef LLVM_SUPPORT_MATHEXTRAS_H #define LLVM_SUPPORT_MATHEXTRAS_H -#include "llvm/System/DataTypes.h" #include "llvm/System/SwapByteOrder.h" namespace llvm { @@ -119,19 +118,19 @@ inline bool isPowerOf2_64(uint64_t Value) { /// ByteSwap_16 - This function returns a byte-swapped representation of the /// 16-bit argument, Value. inline uint16_t ByteSwap_16(uint16_t Value) { - return sys::SwapByteOrder(Value); + return sys::SwapByteOrder_16(Value); } /// ByteSwap_32 - This function returns a byte-swapped representation of the /// 32-bit argument, Value. inline uint32_t ByteSwap_32(uint32_t Value) { - return sys::SwapByteOrder(Value); + return sys::SwapByteOrder_32(Value); } /// ByteSwap_64 - This function returns a byte-swapped representation of the /// 64-bit argument, Value. inline uint64_t ByteSwap_64(uint64_t Value) { - return sys::SwapByteOrder(Value); + return sys::SwapByteOrder_64(Value); } /// CountLeadingZeros_32 - this function performs the platform optimal form of diff --git a/include/llvm/System/SwapByteOrder.h b/include/llvm/System/SwapByteOrder.h index 64a8acb019b..110db341a02 100644 --- a/include/llvm/System/SwapByteOrder.h +++ b/include/llvm/System/SwapByteOrder.h @@ -15,7 +15,6 @@ #ifndef LLVM_SYSTEM_SWAP_BYTE_ORDER_H #define LLVM_SYSTEM_SWAP_BYTE_ORDER_H -#include "llvm/Support/type_traits.h" #include "llvm/System/DataTypes.h" #include #include @@ -23,24 +22,9 @@ namespace llvm { namespace sys { -template -inline -typename enable_if_c::is_integer, - value_type>::type -SwapByteOrder(value_type Value) { - // No swapping needed. - return Value; -} - -template -inline -typename enable_if_c::is_integer, - value_type>::type -SwapByteOrder(value_type Value) { - // Cast signed types to unsigned before swapping. - uint16_t value = static_cast(Value); +/// SwapByteOrder_16 - This function returns a byte-swapped representation of +/// the 16-bit argument. +inline uint16_t SwapByteOrder_16(uint16_t value) { #if defined(_MSC_VER) && !defined(_DEBUG) // The DLL version of the runtime lacks these functions (bug!?), but in a // release build they're replaced with BSWAP instructions anyway. @@ -48,20 +32,15 @@ SwapByteOrder(value_type Value) { #else uint16_t Hi = value << 8; uint16_t Lo = value >> 8; - return value_type(Hi | Lo); + return Hi | Lo; #endif } -template -inline -typename enable_if_c::is_integer, - value_type>::type -SwapByteOrder(value_type Value) { - // Cast signed types to unsigned before swapping. - uint32_t value = static_cast(Value); +/// SwapByteOrder_32 - This function returns a byte-swapped representation of +/// the 32-bit argument. +inline uint32_t SwapByteOrder_32(uint32_t value) { #if defined(__llvm__) || \ - (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) && !defined(__ICC) +(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) && !defined(__ICC) return __builtin_bswap32(value); #elif defined(_MSC_VER) && !defined(_DEBUG) return _byteswap_ulong(value); @@ -70,31 +49,52 @@ SwapByteOrder(value_type Value) { uint32_t Byte1 = value & 0x0000FF00; uint32_t Byte2 = value & 0x00FF0000; uint32_t Byte3 = value & 0xFF000000; - return value_type( - (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24)); + return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24); #endif } -template -inline -typename enable_if_c::is_integer, - value_type>::type -SwapByteOrder(value_type Value) { - // Cast signed types to unsigned before swapping. - uint64_t value = static_cast(Value); +/// SwapByteOrder_64 - This function returns a byte-swapped representation of +/// the 64-bit argument. +inline uint64_t SwapByteOrder_64(uint64_t value) { #if defined(__llvm__) || \ - (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) && !defined(__ICC) +(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) && !defined(__ICC) return __builtin_bswap64(value); #elif defined(_MSC_VER) && !defined(_DEBUG) return _byteswap_uint64(value); #else uint64_t Hi = SwapByteOrder(uint32_t(value)); uint32_t Lo = SwapByteOrder(uint32_t(value >> 32)); - return value_type((Hi << 32) | Lo); + return (Hi << 32) | Lo; #endif } +inline unsigned char SwapByteOrder(unsigned char C) { return C; } +inline signed char SwapByteOrder(signed char C) { return C; } +inline char SwapByteOrder(char C) { return C; } + +inline unsigned short SwapByteOrder(unsigned short C) { return SwapByteOrder_16(C); } +inline signed short SwapByteOrder( signed short C) { return SwapByteOrder_16(C); } + +inline unsigned int SwapByteOrder(unsigned int C) { return SwapByteOrder_32(C); } +inline signed int SwapByteOrder( signed int C) { return SwapByteOrder_32(C); } + +#if __LONG_MAX__ == __INT_MAX__ +inline unsigned long SwapByteOrder(unsigned long C) { return SwapByteOrder_32(C); } +inline signed long SwapByteOrder( signed long C) { return SwapByteOrder_32(C); } +#elif __LONG_MAX__ == __LONG_LONG_MAX__ +inline unsigned long SwapByteOrder(unsigned long C) { return SwapByteOrder_64(C); } +inline signed long SwapByteOrder( signed long C) { return SwapByteOrder_64(C); } +#else +#error "Unknown long size!" +#endif + +inline unsigned long long SwapByteOrder(unsigned long long C) { + return SwapByteOrder_64(C); +} +inline signed long long SwapByteOrder(signed long long C) { + return SwapByteOrder_64(C); +} + } // end namespace sys } // end namespace llvm diff --git a/unittests/Support/SwapByteOrderTest.cpp b/unittests/Support/SwapByteOrderTest.cpp index 073824caa4b..ce8704c3ab7 100644 --- a/unittests/Support/SwapByteOrderTest.cpp +++ b/unittests/Support/SwapByteOrderTest.cpp @@ -92,37 +92,37 @@ TEST(SwapByteOrder, SignedRoundTrip) { } TEST(SwapByteOrder, uint8_t) { - EXPECT_EQ(uint8_t(0x11), sys::SwapByteOrder(0x11)); + EXPECT_EQ(uint8_t(0x11), sys::SwapByteOrder(uint8_t(0x11))); } TEST(SwapByteOrder, uint16_t) { - EXPECT_EQ(uint16_t(0x1122), sys::SwapByteOrder(0x2211)); + EXPECT_EQ(uint16_t(0x1122), sys::SwapByteOrder(uint16_t(0x2211))); } TEST(SwapByteOrder, uint32_t) { - EXPECT_EQ(uint32_t(0x11223344), sys::SwapByteOrder(0x44332211)); + EXPECT_EQ(uint32_t(0x11223344), sys::SwapByteOrder(uint32_t(0x44332211))); } TEST(SwapByteOrder, uint64_t) { EXPECT_EQ(uint64_t(0x1122334455667788ULL), - sys::SwapByteOrder(0x8877665544332211ULL)); + sys::SwapByteOrder(uint64_t(0x8877665544332211ULL))); } TEST(SwapByteOrder, int8_t) { - EXPECT_EQ(int8_t(0x11), sys::SwapByteOrder(0x11)); + EXPECT_EQ(int8_t(0x11), sys::SwapByteOrder(int8_t(0x11))); } TEST(SwapByteOrder, int16_t) { - EXPECT_EQ(int16_t(0x1122), sys::SwapByteOrder(0x2211)); + EXPECT_EQ(int16_t(0x1122), sys::SwapByteOrder(int16_t(0x2211))); } TEST(SwapByteOrder, int32_t) { - EXPECT_EQ(int32_t(0x11223344), sys::SwapByteOrder(0x44332211)); + EXPECT_EQ(int32_t(0x11223344), sys::SwapByteOrder(int32_t(0x44332211))); } TEST(SwapByteOrder, int64_t) { EXPECT_EQ(int64_t(0x1122334455667788LL), - sys::SwapByteOrder(0x8877665544332211LL)); + sys::SwapByteOrder(int64_t(0x8877665544332211LL))); } }