Endian conversion functions
authorBarret Rhoden <brho@cs.berkeley.edu>
Mon, 30 Aug 2010 00:42:47 +0000 (17:42 -0700)
committerKevin Klues <klueska@cs.berkeley.edu>
Thu, 3 Nov 2011 00:35:53 +0000 (17:35 -0700)
kern/arch/i686/endian.h [new file with mode: 0644]
kern/arch/sparc/endian.h [new file with mode: 0644]
kern/include/endian.h [new file with mode: 0644]

diff --git a/kern/arch/i686/endian.h b/kern/arch/i686/endian.h
new file mode 100644 (file)
index 0000000..a42a3ce
--- /dev/null
@@ -0,0 +1,31 @@
+/* Copyright (c) 2010 The Regents of the University of California
+ * Barret Rhoden <brho@cs.berkeley.edu>
+ * See LICENSE for details.
+ *
+ * Endian #def and byte_swapping functions */
+
+#ifndef ROS_KERN_ARCH_ENDIAN_H
+#define ROS_KERN_ARCH_ENDIAN_H
+
+#include <ros/common.h>
+#include <arch/types.h>
+
+static inline uint16_t byte_swap16(uint16_t x)
+{
+       return (uint16_t)(x << 8 | x >> 8);
+}
+
+static inline uint32_t byte_swap32(uint32_t x)
+{
+       /* the + denotes both output and input */
+       asm volatile("bswap %0" : "+r"(x));
+       return x;
+}
+
+static inline uint64_t byte_swap64(uint64_t x)
+{
+       return (uint64_t)(((uint64_t)byte_swap32(x & 0xffffffff) << 32) |
+                         (byte_swap32(x >> 32)));
+}
+
+#endif /* ROS_KERN_ARCH_ENDIAN_H */
diff --git a/kern/arch/sparc/endian.h b/kern/arch/sparc/endian.h
new file mode 100644 (file)
index 0000000..e4ed150
--- /dev/null
@@ -0,0 +1,30 @@
+/* Copyright (c) 2010 The Regents of the University of California
+ * Barret Rhoden <brho@cs.berkeley.edu>
+ * See LICENSE for details.
+ *
+ * Endian #def and byte_swapping functions */
+
+#ifndef ROS_KERN_ARCH_ENDIAN_H
+#define ROS_KERN_ARCH_ENDIAN_H
+
+#include <ros/common.h>
+#include <arch/types.h>
+
+static inline uint16_t byte_swap16(uint16_t x)
+{
+       return (uint16_t)(x << 8 | x >> 8);
+}
+
+static inline uint32_t byte_swap32(uint32_t x)
+{
+       return (uint32_t)(((uint32_t)byte_swap16(x & 0xffff) << 16) |
+                         (byte_swap16(x >> 16)));
+}
+
+static inline uint64_t byte_swap64(uint64_t x)
+{
+       return (uint64_t)(((uint64_t)byte_swap32(x & 0xffffffff) << 32) |
+                         (byte_swap32(x >> 32)));
+}
+
+#endif /* ROS_KERN_ARCH_ENDIAN_H */
diff --git a/kern/include/endian.h b/kern/include/endian.h
new file mode 100644 (file)
index 0000000..be7ddfb
--- /dev/null
@@ -0,0 +1,50 @@
+/* Copyright (c) 2010 The Regents of the University of California
+ * Barret Rhoden <brho@cs.berkeley.edu>
+ * See LICENSE for details.
+ *
+ * Macros to convert to and from endian-data */
+
+#include <ros/common.h>
+#include <arch/endian.h>
+
+#ifndef ROS_KERN_ENDIAN_H
+#define ROS_KERN_ENDIAN_H
+
+#ifdef LITTLE_ENDIAN
+#define cpu_to_le16(x) ((uint16_t)(x))
+#define cpu_to_le32(x) ((uint32_t)(x))
+#define cpu_to_le64(x) ((uint64_t)(x))
+#define le16_to_cpu(x) ((uint16_t)(x))
+#define le32_to_cpu(x) ((uint32_t)(x))
+#define le64_to_cpu(x) ((uint64_t)(x))
+
+#define cpu_to_be16(x) byte_swap16((x))
+#define cpu_to_be32(x) byte_swap32((x))
+#define cpu_to_be64(x) byte_swap64((x))
+#define be16_to_cpu(x) byte_swap16((x))
+#define be32_to_cpu(x) byte_swap32((x))
+#define be64_to_cpu(x) byte_swap64((x))
+
+#else /* big endian */
+
+# ifndef BIG_ENDIAN
+# error "Need an endianness defined"
+# endif
+
+#define cpu_to_le16(x) byte_swap16((x))
+#define cpu_to_le32(x) byte_swap32((x))
+#define cpu_to_le64(x) byte_swap64((x))
+#define le16_to_cpu(x) byte_swap16((x))
+#define le32_to_cpu(x) byte_swap32((x))
+#define le64_to_cpu(x) byte_swap64((x))
+
+#define cpu_to_be16(x) ((uint16_t)(x))
+#define cpu_to_be32(x) ((uint32_t)(x))
+#define cpu_to_be64(x) ((uint64_t)(x))
+#define be16_to_cpu(x) ((uint16_t)(x))
+#define be32_to_cpu(x) ((uint32_t)(x))
+#define be64_to_cpu(x) ((uint64_t)(x))
+
+#endif /* endian */
+
+#endif /* ROS_KERN_ENDIAN_H */