[llvm-commits] [llvm] r119992 - in /llvm/trunk: include/llvm/Support/MathExtras.h include/llvm/System/SwapByteOrder.h unittests/Support/SwapByteOrderTest.cpp

Chris Lattner sabre at nondot.org
Mon Nov 22 20:04:25 PST 2010


Author: lattner
Date: Mon Nov 22 22:04:25 2010
New Revision: 119992

URL: http://llvm.org/viewvc/llvm-project?rev=119992&view=rev
Log:
reimplement SwapByteOrder.h in terms of overloading instead of 
being in terms of excessively complex template logic.

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

Modified: llvm/trunk/include/llvm/Support/MathExtras.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MathExtras.h?rev=119992&r1=119991&r2=119992&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/MathExtras.h (original)
+++ llvm/trunk/include/llvm/Support/MathExtras.h Mon Nov 22 22:04:25 2010
@@ -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 @@
 /// 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

Modified: llvm/trunk/include/llvm/System/SwapByteOrder.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/System/SwapByteOrder.h?rev=119992&r1=119991&r2=119992&view=diff
==============================================================================
--- llvm/trunk/include/llvm/System/SwapByteOrder.h (original)
+++ llvm/trunk/include/llvm/System/SwapByteOrder.h Mon Nov 22 22:04:25 2010
@@ -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 @@
 #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 @@
   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
 

Modified: llvm/trunk/unittests/Support/SwapByteOrderTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/SwapByteOrderTest.cpp?rev=119992&r1=119991&r2=119992&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/SwapByteOrderTest.cpp (original)
+++ llvm/trunk/unittests/Support/SwapByteOrderTest.cpp Mon Nov 22 22:04:25 2010
@@ -92,37 +92,37 @@
 }
 
 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)));
 }
 
 }





More information about the llvm-commits mailing list