diff options
author | Benjamin Barenblat <bbaren@mit.edu> | 2015-09-05 09:32:30 -0400 |
---|---|---|
committer | Benjamin Barenblat <bbaren@mit.edu> | 2015-09-05 09:32:30 -0400 |
commit | ea76b6988ccafaa6a4d4ed90f2489d0e49e1f180 (patch) | |
tree | b01dd19f5c53406d9d636b18bc49916bfdd6d3bc /g_src/endian.h |
Imported Upstream version 0.40.24upstream/0.40.24upstream
Diffstat (limited to 'g_src/endian.h')
-rwxr-xr-x | g_src/endian.h | 42 |
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 |