aboutsummaryrefslogtreecommitdiffstats
path: root/include/llvm
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2010-11-23 04:04:25 +0000
committerChris Lattner <sabre@nondot.org>2010-11-23 04:04:25 +0000
commit3afc385042fb0d121e9454347f975e4f1a5f5bfd (patch)
tree256314b5350f1634d712b97090442bcb824f8309 /include/llvm
parentbf17cfa3f904e488e898ac2e3af706fd1a892f08 (diff)
downloadexternal_llvm-3afc385042fb0d121e9454347f975e4f1a5f5bfd.zip
external_llvm-3afc385042fb0d121e9454347f975e4f1a5f5bfd.tar.gz
external_llvm-3afc385042fb0d121e9454347f975e4f1a5f5bfd.tar.bz2
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
Diffstat (limited to 'include/llvm')
-rw-r--r--include/llvm/Support/MathExtras.h7
-rw-r--r--include/llvm/System/SwapByteOrder.h82
2 files changed, 44 insertions, 45 deletions
diff --git a/include/llvm/Support/MathExtras.h b/include/llvm/Support/MathExtras.h
index dafb479..1b58b3f 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 64a8acb..110db34 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 <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,29 +49,50 @@ 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