ByteOrder.h

Go to the documentation of this file.
00001 #ifndef __LDK_BYTEORDER_H__
00002 #define __LDK_BYTEORDER_H__
00003 
00005 //   Copyright (C) 2003-2006 Lorien Dunn.
00006 //
00007 //   Contact: loriendunn AT users DOT sourceforge DOT net
00008 //
00009 //   This software is provided 'as-is', without any express or implied warranty.
00010 //   In no event will the authors be held liable for any damages arising from
00011 //   the use of this software.
00012 //
00013 //   Permission is granted to anyone to use this software for any purpose,
00014 //   including commercial applications, and to alter it and redistribute it
00015 //   freely, subject to the following restrictions:
00016 //
00017 //   1. The origin of this software must not be misrepresented; you must not
00018 //   claim that you wrote the original software. If you use this software in a
00019 //   product, an acknowledgment in the product documentation would be
00020 //   appreciated but is not required.
00021 //
00022 //   2. Altered source versions must be plainly marked as such, and must not be
00023 //   misrepresented as being the original software.
00024 //
00025 //   3. This notice may not be removed or altered from any source distribution.
00026 //
00028 
00029 
00034 #include "LDK/Types.h"
00035 
00036 /* This byteorder stuff was lifted from PhysFS, which in turn lifted it from SDL*/
00037 #define LDK_LIL_ENDIAN  1234
00038 #define LDK_BIG_ENDIAN  4321
00039 
00040 #if  defined(__i386__) || defined(__ia64__) || defined(WIN32) || \
00041     (defined(__alpha__) || defined(__alpha)) || \
00042      defined(__arm__) || defined(ARM) || \
00043     (defined(__mips__) && defined(__MIPSEL__)) || \
00044      defined(__SYMBIAN32__) || \
00045      defined(__x86_64__) || \
00046      defined(__LITTLE_ENDIAN__)
00047 #define LDK_BYTEORDER    LDK_LIL_ENDIAN
00048 #else
00049 #define LDK_BYTEORDER    LDK_BIG_ENDIAN
00050 #endif
00051 
00052 namespace LDK
00053 {
00061 
00066 inline uint16 swap16(uint16 dat)
00067 {
00068     return (dat<<8)|(dat>>8);
00069 }
00070 
00075 inline uint32 swap32(uint32 dat)
00076 {
00077     return (dat<<24)|((dat<<8)&0x00FF0000)|((dat>>8)&0x0000FF00)|(dat>>24);
00078 }
00079 
00084 inline uint64 swap64(uint64 dat)  //Separate into high and low 32-bit values and swap them
00085 {
00086     uint32 hi, lo;
00087 
00088     lo = (uint32)(dat&0xFFFFFFFF);
00089     dat >>= 32;
00090     hi = (uint32)(dat&0xFFFFFFFF);
00091     dat = swap32(lo);
00092     dat <<= 32;
00093     dat |= swap32(hi);
00094     return dat;
00095 }
00096 
00097 #if LDK_BYTEORDER == LDK_LIL_ENDIAN
00098 inline uint16 swapULE16(uint16 x) { return x; }
00099 inline int16  swapSLE16(int16 x)  { return x; }
00100 inline uint32 swapULE32(uint32 x) { return x; }
00101 inline int32  swapSLE32(int32 x)  { return x; }
00102 inline uint64 swapULE64(uint64 x) { return x; }
00103 inline int64  swapSLE64(int64 x)  { return x; }
00104 inline uint16 swapUBE16(uint16 x) { return swap16(x); }
00105 inline int16  swapSBE16(int16 x)  { return swap16(x); }
00106 inline uint32 swapUBE32(uint32 x) { return swap32(x); }
00107 inline int32  swapSBE32(int32 x)  { return swap32(x); }
00108 inline uint64 swapUBE64(uint64 x) { return swap64(x); }
00109 inline int64  swapSBE64(int64 x)  { return swap64(x); }
00110 
00111 #else //LDK_BYTEORDER == LDK_BIG_ENDIAN
00117 inline uint16 swapULE16(uint16 x) { return swap16(x); }
00118 
00124 inline int16  swapSLE16(int16 x)  { return swap16(x); }
00125 
00131 inline uint32 swapULE32(uint32 x) { return swap32(x); }
00132 
00138 inline int32  swapSLE32(int32 x)  { return swap32(x); }
00139 
00145 inline uint64 swapULE64(uint64 x) { return swap64(x); }
00146 
00152 inline int64  swapSLE64(int64 x)  { return swap64(x); }
00153 
00159 inline uint16 swapUBE16(uint16 x) { return x; }
00160 
00166 inline int16  swapSBE16(int16 x)  { return x; }
00167 
00173 inline uint32 swapUBE32(uint32 x) { return x; }
00174 
00180 inline int32  swapSBE32(int32 x)  { return x; }
00181 
00187 inline uint64 swapUBE64(uint64 x) { return x; }
00188 
00194 inline int64  swapSBE64(int64 x)  { return x; }
00195 #endif //LDK_BYTEORDER == LDK_LIL_ENDIAN
00196 
00199 } //namespace LDK
00200 
00201 
00202 #endif //__LDK_BYTEORDER_H__

Generated on Fri Aug 17 18:32:26 2007 for LDK by  doxygen 1.5.1