reimplement SwapByteOrder.h in terms of overloading instead of

being in terms of excessively complex template logic.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@119992 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2010-11-23 04:04:25 +00:00
parent bf17cfa3f9
commit 3afc385042
3 changed files with 52 additions and 53 deletions

View File

@ -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

View File

@ -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 <cstddef>
#include <limits>
@ -23,24 +22,9 @@
namespace llvm {
namespace sys {
template<typename value_type>
inline
typename enable_if_c<sizeof(value_type) == 1
&& std::numeric_limits<value_type>::is_integer,
value_type>::type
SwapByteOrder(value_type Value) {
// No swapping needed.
return Value;
}
template<typename value_type>
inline
typename enable_if_c<sizeof(value_type) == 2
&& std::numeric_limits<value_type>::is_integer,
value_type>::type
SwapByteOrder(value_type Value) {
// Cast signed types to unsigned before swapping.
uint16_t value = static_cast<uint16_t>(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<typename value_type>
inline
typename enable_if_c<sizeof(value_type) == 4
&& std::numeric_limits<value_type>::is_integer,
value_type>::type
SwapByteOrder(value_type Value) {
// Cast signed types to unsigned before swapping.
uint32_t value = static_cast<uint32_t>(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<typename value_type>
inline
typename enable_if_c<sizeof(value_type) == 8
&& std::numeric_limits<value_type>::is_integer,
value_type>::type
SwapByteOrder(value_type Value) {
// Cast signed types to unsigned before swapping.
uint64_t value = static_cast<uint64_t>(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>(uint32_t(value));
uint32_t Lo = SwapByteOrder<uint32_t>(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

View File

@ -92,37 +92,37 @@ TEST(SwapByteOrder, SignedRoundTrip) {
}
TEST(SwapByteOrder, uint8_t) {
EXPECT_EQ(uint8_t(0x11), sys::SwapByteOrder<uint8_t>(0x11));
EXPECT_EQ(uint8_t(0x11), sys::SwapByteOrder(uint8_t(0x11)));
}
TEST(SwapByteOrder, uint16_t) {
EXPECT_EQ(uint16_t(0x1122), sys::SwapByteOrder<uint16_t>(0x2211));
EXPECT_EQ(uint16_t(0x1122), sys::SwapByteOrder(uint16_t(0x2211)));
}
TEST(SwapByteOrder, uint32_t) {
EXPECT_EQ(uint32_t(0x11223344), sys::SwapByteOrder<uint32_t>(0x44332211));
EXPECT_EQ(uint32_t(0x11223344), sys::SwapByteOrder(uint32_t(0x44332211)));
}
TEST(SwapByteOrder, uint64_t) {
EXPECT_EQ(uint64_t(0x1122334455667788ULL),
sys::SwapByteOrder<uint64_t>(0x8877665544332211ULL));
sys::SwapByteOrder(uint64_t(0x8877665544332211ULL)));
}
TEST(SwapByteOrder, int8_t) {
EXPECT_EQ(int8_t(0x11), sys::SwapByteOrder<int8_t>(0x11));
EXPECT_EQ(int8_t(0x11), sys::SwapByteOrder(int8_t(0x11)));
}
TEST(SwapByteOrder, int16_t) {
EXPECT_EQ(int16_t(0x1122), sys::SwapByteOrder<int16_t>(0x2211));
EXPECT_EQ(int16_t(0x1122), sys::SwapByteOrder(int16_t(0x2211)));
}
TEST(SwapByteOrder, int32_t) {
EXPECT_EQ(int32_t(0x11223344), sys::SwapByteOrder<int32_t>(0x44332211));
EXPECT_EQ(int32_t(0x11223344), sys::SwapByteOrder(int32_t(0x44332211)));
}
TEST(SwapByteOrder, int64_t) {
EXPECT_EQ(int64_t(0x1122334455667788LL),
sys::SwapByteOrder<int64_t>(0x8877665544332211LL));
sys::SwapByteOrder(int64_t(0x8877665544332211LL)));
}
}