浏览代码

config: define swap16/swap32/swap64 macros for manually setting the endianness

Julius Pfrommer 9 年之前
父节点
当前提交
84bd4c3ddd
共有 1 个文件被更改,包括 16 次插入11 次删除
  1. 16 11
      include/ua_config.h.in

+ 16 - 11
include/ua_config.h.in

@@ -48,7 +48,6 @@
 // and doubles. Otherwise, they are endianness-switched similar to integers.
 // and doubles. Otherwise, they are endianness-switched similar to integers.
 
 
 #if defined(__LITTLE_ENDIAN__) || defined(_WIN32) || (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
 #if defined(__LITTLE_ENDIAN__) || defined(_WIN32) || (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
-/* little-endian is the default assumption */
 # define htole16(x) (x)
 # define htole16(x) (x)
 # define htole32(x) (x)
 # define htole32(x) (x)
 # define htole64(x) (x)
 # define htole64(x) (x)
@@ -62,7 +61,6 @@
 #  include <sys/endian.h>
 #  include <sys/endian.h>
 # elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
 # elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
 #  include <sys/endian.h>
 #  include <sys/endian.h>
-   /* the htole functions are defined correctly */
 #  define le16toh(x) letoh16(x)
 #  define le16toh(x) letoh16(x)
 #  define le32toh(x) letoh32(x)
 #  define le32toh(x) letoh32(x)
 #  define le64toh(x) letoh64(x)
 #  define le64toh(x) letoh64(x)
@@ -79,8 +77,6 @@
 #  include <gulliver.h>
 #  include <gulliver.h>
 #  if defined(__LITTLEENDIAN__)
 #  if defined(__LITTLEENDIAN__)
 #   define __BYTE_ORDER __LITTLE_ENDIAN
 #   define __BYTE_ORDER __LITTLE_ENDIAN
-#  else
-#   define __BYTE_ORDER NON_LITTLE_ENDIAN
 #  endif
 #  endif
 #  define htole16(x) ENDIAN_LE16(x)
 #  define htole16(x) ENDIAN_LE16(x)
 #  define htole32(x) ENDIAN_LE32(x)
 #  define htole32(x) ENDIAN_LE32(x)
@@ -94,18 +90,27 @@
 # endif
 # endif
 #endif
 #endif
 
 
-/* Mixed Endian Example */
+/* Manually override */
+// If required, define UA_NON_LITTLEENDIAN_ARCHITECTURE, UA_MIXED_ENDIAN, and
+// the htole / letoh functions here. Even better, post an issue on github or the
+// mailing list, so we can include the architecture in the standard release.
+#define swap16(u16) ((u16s >> 8) | (u16s << 8))
+#define swap32(u32) ((u32 >> 24) | ((u32 << 8) & 0x00FF0000) | ((u32 >> 8) & 0x0000FF00) | (u32 << 24))
+#define swap64(u64) ((u64 >> 56) | ((u64 << 40) & 0x00FF000000000000) | ((u64 << 24) & 0x0000FF0000000000) | \
+                     ((u64 << 8) & 0x000000FF00000000) | ((u64 >> 8) & 0x00000000FF000000) |                 \
+                     ((u64 >> 24) & 0x0000000000FF0000) | ((u64 >> 40) & 0x000000000000FF00) | (u64 << 56))
+
 #ifdef __ARM_ARCH_4T__
 #ifdef __ARM_ARCH_4T__
 # define UA_MIXED_ENDIAN
 # define UA_MIXED_ENDIAN
 # define UA_NON_LITTLEENDIAN_ARCHITECTURE
 # define UA_NON_LITTLEENDIAN_ARCHITECTURE
+# define htole16(x) swap16(x)
+# define htole32(x) swap32(x)
+# define htole64(x) swap64(x)
+# define le16toh(x) swap16(x)
+# define le32toh(x) swap32(x)
+# define le64toh(x) swap64(x)
 #endif
 #endif
 
 
-/* Manually override */
-// If required, manually define UA_NON_LITTLEENDIAN_ARCHITECTURE and
-// UA_MIXED_ENDIAN, as well as the htole / letoh functions here. Even better,
-// post an issue on github or the mailing list, so we can include the
-// architecture in the standard release.
-
 #ifndef htole16
 #ifndef htole16
 # error The endianness of the architecture not known
 # error The endianness of the architecture not known
 #endif
 #endif