reimplement SwapByteOrder.h in terms of overloading instead of
authorChris Lattner <sabre@nondot.org>
Tue, 23 Nov 2010 04:04:25 +0000 (04:04 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 23 Nov 2010 04:04:25 +0000 (04:04 +0000)
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

include/llvm/Support/MathExtras.h
include/llvm/System/SwapByteOrder.h
unittests/Support/SwapByteOrderTest.cpp

index dafb479a99938bbae4e5a6c349415fbf89858d7d..1b58b3f83c4c0bff33ad4b4df00e94fe1906e222 100644 (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
index 64a8acb019b960a0cf032958d0a9255749a45fe8..110db341a0271e7d2ab62abfacf3d0721f758a16 100644 (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>
 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
index 073824caa4b231f2a1396f2b2b9592d2c1d5c4f1..ce8704c3ab7a6ab6f9e9e74ea75e483449ab86ae 100644 (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)));
 }
 
 }