00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00035 #ifndef OW_BYTE_SWAP_HPP_
00036 #define OW_BYTE_SWAP_HPP_
00037 #include "OW_config.h"
00038 #include "OW_Types.hpp"
00039 
00040 #if !defined(OW_WORDS_BIGENDIAN) && defined(OW_HAVE_BYTESWAP_H) && !defined(OW_DEBUG_MEMORY)
00041 #include <byteswap.h>
00042 #endif
00043 
00044 namespace OW_NAMESPACE
00045 {
00046 
00047 
00048 #ifndef OW_WORDS_BIGENDIAN
00049 #if defined(OW_HAVE_BYTESWAP_H) && !defined(OW_DEBUG_MEMORY)
00050 
00051 inline UInt16 hton16(UInt16 v) { return __bswap_16(v); }
00052 inline UInt32 hton32(UInt32 v) { return __bswap_32(v); }
00053 inline UInt64 hton64(UInt64 v) { return __bswap_64(v); }
00054 inline UInt16 ntoh16(UInt16 v) { return __bswap_16(v); }
00055 inline UInt32 ntoh32(UInt32 v) { return __bswap_32(v); }
00056 inline UInt64 ntoh64(UInt64 v) { return __bswap_64(v); }
00057 #else
00058 inline UInt16 hton16(UInt16 v)
00059 {
00060    UInt16 rval;
00061    (reinterpret_cast<unsigned char*>(&rval))[1] = (reinterpret_cast<unsigned char*>(&v))[0];
00062    (reinterpret_cast<unsigned char*>(&rval))[0] = (reinterpret_cast<unsigned char*>(&v))[1];
00063    return rval;
00064 }
00065 inline UInt32 hton32(UInt32 v)
00066 {
00067    UInt32 rval;
00068    (reinterpret_cast<unsigned char*>(&rval))[3] = (reinterpret_cast<unsigned char*>(&v))[0];
00069    (reinterpret_cast<unsigned char*>(&rval))[2] = (reinterpret_cast<unsigned char*>(&v))[1];
00070    (reinterpret_cast<unsigned char*>(&rval))[1] = (reinterpret_cast<unsigned char*>(&v))[2];
00071    (reinterpret_cast<unsigned char*>(&rval))[0] = (reinterpret_cast<unsigned char*>(&v))[3];
00072    return rval;
00073 }
00074 inline UInt64 hton64(UInt64 v)
00075 {
00076    UInt64 rval;
00077    (reinterpret_cast<unsigned char*>(&rval))[7] = (reinterpret_cast<unsigned char*>(&v))[0];
00078    (reinterpret_cast<unsigned char*>(&rval))[6] = (reinterpret_cast<unsigned char*>(&v))[1];
00079    (reinterpret_cast<unsigned char*>(&rval))[5] = (reinterpret_cast<unsigned char*>(&v))[2];
00080    (reinterpret_cast<unsigned char*>(&rval))[4] = (reinterpret_cast<unsigned char*>(&v))[3];
00081    (reinterpret_cast<unsigned char*>(&rval))[3] = (reinterpret_cast<unsigned char*>(&v))[4];
00082    (reinterpret_cast<unsigned char*>(&rval))[2] = (reinterpret_cast<unsigned char*>(&v))[5];
00083    (reinterpret_cast<unsigned char*>(&rval))[1] = (reinterpret_cast<unsigned char*>(&v))[6];
00084    (reinterpret_cast<unsigned char*>(&rval))[0] = (reinterpret_cast<unsigned char*>(&v))[7];
00085    return rval;
00086 }
00087 inline UInt16 ntoh16(UInt16 v)
00088 {
00089    return hton16(v);
00090 }
00091 inline UInt32 ntoh32(UInt32 v)
00092 {
00093    return hton32(v);
00094 }
00095 inline UInt64 ntoh64(UInt64 v)
00096 {
00097    return hton64(v);
00098 }
00099 #endif
00100 #else // we're big-endian, just pass-thru
00101 inline UInt16 hton16(UInt16 v) { return v; }
00102 inline UInt32 hton32(UInt32 v) { return v; }
00103 inline UInt64 hton64(UInt64 v) { return v; }
00104 inline UInt16 ntoh16(UInt16 v) { return v; }
00105 inline UInt32 ntoh32(UInt32 v) { return v; }
00106 inline UInt64 ntoh64(UInt64 v) { return v; }
00107 #endif
00108 
00109 } 
00110 
00111 #endif