[opengtl-commits] [575] add functions to convert to/from endianess

[ Thread Index | Date Index | More lists.tuxfamily.org/opengtl-commits Archives ]


Revision: 575
Author:   cyrille
Date:     2009-03-01 11:13:42 +0100 (Sun, 01 Mar 2009)

Log Message:
-----------
add functions to convert to/from endianess

Modified Paths:
--------------
    trunk/OpenGTL/OpenGTL/GTLCore/Utils_p.h


Modified: trunk/OpenGTL/OpenGTL/GTLCore/Utils_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Utils_p.h	2009-02-28 16:38:26 UTC (rev 574)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Utils_p.h	2009-03-01 10:13:42 UTC (rev 575)
@@ -20,6 +20,10 @@
 #ifndef _GTLCORE_UTILS_P_H_
 #define _GTLCORE_UTILS_P_H_
 
+#include <config-endian.h>
+
+#include <stdint.h>
+
 namespace GTLCore {
 
   /**
@@ -60,7 +64,94 @@
   {
     return t1 > t2 ? t1 : t2;
   }
+  
+template <typename T> inline void toUnaligned(const T src, unsigned char *dest)
+{
+    memcpy(dest, &src, sizeof(T));
+}
 
+#define MAKE_UINT64(c) c ## LL
+
+template <typename T> T bswap(T source);
+template <> inline uint64_t bswap<uint64_t>(uint64_t source)
+{
+    return 0
+        | ((source & MAKE_UINT64(0x00000000000000ff)) << 56)
+        | ((source & MAKE_UINT64(0x000000000000ff00)) << 40)
+        | ((source & MAKE_UINT64(0x0000000000ff0000)) << 24)
+        | ((source & MAKE_UINT64(0x00000000ff000000)) << 8)
+        | ((source & MAKE_UINT64(0x000000ff00000000)) >> 8)
+        | ((source & MAKE_UINT64(0x0000ff0000000000)) >> 24)
+        | ((source & MAKE_UINT64(0x00ff000000000000)) >> 40)
+        | ((source & MAKE_UINT64(0xff00000000000000)) >> 56);
 }
 
+template <> inline uint32_t bswap<uint32_t>(uint32_t source)
+{
+    return 0
+        | ((source & 0x000000ff) << 24)
+        | ((source & 0x0000ff00) << 8)
+        | ((source & 0x00ff0000) >> 8)
+        | ((source & 0xff000000) >> 24);
+}
+
+template <> inline uint16_t bswap<uint16_t>(uint16_t source)
+{
+    return 0
+        | ((source & 0x00ff) << 8)
+        | ((source & 0xff00) >> 8);
+}
+
+// signed specializations
+template <> inline int64_t bswap<int64_t>(int64_t source)
+{
+    return bswap<uint64_t>(uint64_t(source));
+}
+
+template <> inline int32_t bswap<int32_t>(int32_t source)
+{
+    return bswap<uint32_t>(uint32_t(source));
+}
+
+template <> inline int16_t bswap<int16_t>(int16_t source)
+{
+    return bswap<uint16_t>(uint16_t(source));
+}
+
+#ifdef WORDS_BIGENDIAN
+
+template <typename T> inline T toBigEndian(T source)
+{ return source; }
+template <typename T> inline T fromBigEndian(T source)
+{ return source; }
+template <typename T> inline T toLittleEndian(T source)
+{ return bswap<T>(source); }
+template <typename T> inline T fromLittleEndian(T source)
+{ return bswap<T>(source); }
+template <typename T> inline void toBigEndian(T src, uchar *dest)
+{ toUnaligned<T>(src, dest); }
+template <typename T> inline void toLittleEndian(T src, uchar *dest)
+{ bswap<T>(src, dest); }
+#else // WORDS_BIGENDIAN
+
+template <typename T> inline T toBigEndian(T source)
+{ return bswap<T>(source); }
+/**
+ * Convert a value from big endian, when the cpu is little-endian (like x86).
+ */
+template <typename T> inline T fromBigEndian(T source)
+{ return bswap<T>(source); }
+template <typename T> inline T toLittleEndian(T source)
+{ return source; }
+template <typename T> inline T fromLittleEndian(T source)
+{ return source; }
+template <typename T> inline void toBigEndian(T src, unsigned char *dest)
+{ bswap<T>(src, dest); }
+template <typename T> inline void toLittleEndian(T src, unsigned char *dest)
+{ toUnaligned<T>(src, dest); }
+
+#endif // WORDS_BIGENDIAN  
+
+}
+
 #endif


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/