summaryrefslogtreecommitdiff
path: root/g_src/endian.h
diff options
context:
space:
mode:
Diffstat (limited to 'g_src/endian.h')
-rwxr-xr-xg_src/endian.h42
1 files changed, 42 insertions, 0 deletions
diff --git a/g_src/endian.h b/g_src/endian.h
new file mode 100755
index 0000000..b56dee4
--- /dev/null
+++ b/g_src/endian.h
@@ -0,0 +1,42 @@
+#ifndef ENDIAN_H
+#define ENDIAN_H
+
+inline unsigned short byteswap(unsigned short x)
+{
+#if defined(__ppc__) || defined(__ppc64__)
+ return (x << 8 | x >> 8);
+#else
+ return x;
+#endif
+}
+
+inline unsigned long byteswap(unsigned long x)
+{
+#if defined(__ppc__) || defined(__ppc64__)
+ return
+ ( (x << 24) & 0xFF000000) |
+ ( (x << 8) & 0x00FF0000) |
+ ( (x >> 8) & 0x0000FF00) |
+ ( (x >> 24) & 0x000000FF) ;
+#else
+ return x;
+#endif
+}
+
+inline unsigned int byteswap(unsigned int x)
+{
+#if defined(__ppc__) || defined(__ppc64__)
+ return
+ ( (x << 24) & 0xFF000000) |
+ ( (x << 8) & 0x00FF0000) |
+ ( (x >> 8) & 0x0000FF00) |
+ ( (x >> 24) & 0x000000FF) ;
+#else
+ return x;
+#endif
+}
+
+inline short byteswap(short x) { return byteswap( (unsigned short) x ); }
+inline long byteswap(long x) { return byteswap( (unsigned long) x ); }
+inline int byteswap(int x) { return byteswap( (unsigned int) x ); }
+#endif