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
00031
00032
00033
00038 #ifndef BLOCXX_BYTE_SWAP_HPP_
00039 #define BLOCXX_BYTE_SWAP_HPP_
00040 #include "blocxx/BLOCXX_config.h"
00041 #include "blocxx/Types.hpp"
00042
00043 #if !defined(BLOCXX_WORDS_BIGENDIAN) && defined(BLOCXX_HAVE_BYTESWAP_H) && !defined(BLOCXX_DEBUG_MEMORY)
00044 #include <byteswap.h>
00045 #endif
00046
00047 namespace BLOCXX_NAMESPACE
00048 {
00049
00050
00051 #ifndef BLOCXX_WORDS_BIGENDIAN
00052 #if defined(BLOCXX_HAVE_BYTESWAP_H) && !defined(BLOCXX_DEBUG_MEMORY)
00053
00059 inline UInt16 hton16(UInt16 v) { return __bswap_16(v); }
00065 inline UInt32 hton32(UInt32 v) { return __bswap_32(v); }
00071 inline UInt64 hton64(UInt64 v) { return __bswap_64(v); }
00077 inline UInt16 ntoh16(UInt16 v) { return __bswap_16(v); }
00083 inline UInt32 ntoh32(UInt32 v) { return __bswap_32(v); }
00089 inline UInt64 ntoh64(UInt64 v) { return __bswap_64(v); }
00090 #else
00091 inline UInt16 hton16(UInt16 v)
00092 {
00093 UInt16 rval;
00094 (reinterpret_cast<unsigned char*>(&rval))[1] = (reinterpret_cast<unsigned char*>(&v))[0];
00095 (reinterpret_cast<unsigned char*>(&rval))[0] = (reinterpret_cast<unsigned char*>(&v))[1];
00096 return rval;
00097 }
00098 inline UInt32 hton32(UInt32 v)
00099 {
00100 UInt32 rval;
00101 (reinterpret_cast<unsigned char*>(&rval))[3] = (reinterpret_cast<unsigned char*>(&v))[0];
00102 (reinterpret_cast<unsigned char*>(&rval))[2] = (reinterpret_cast<unsigned char*>(&v))[1];
00103 (reinterpret_cast<unsigned char*>(&rval))[1] = (reinterpret_cast<unsigned char*>(&v))[2];
00104 (reinterpret_cast<unsigned char*>(&rval))[0] = (reinterpret_cast<unsigned char*>(&v))[3];
00105 return rval;
00106 }
00107 inline UInt64 hton64(UInt64 v)
00108 {
00109 UInt64 rval;
00110 (reinterpret_cast<unsigned char*>(&rval))[7] = (reinterpret_cast<unsigned char*>(&v))[0];
00111 (reinterpret_cast<unsigned char*>(&rval))[6] = (reinterpret_cast<unsigned char*>(&v))[1];
00112 (reinterpret_cast<unsigned char*>(&rval))[5] = (reinterpret_cast<unsigned char*>(&v))[2];
00113 (reinterpret_cast<unsigned char*>(&rval))[4] = (reinterpret_cast<unsigned char*>(&v))[3];
00114 (reinterpret_cast<unsigned char*>(&rval))[3] = (reinterpret_cast<unsigned char*>(&v))[4];
00115 (reinterpret_cast<unsigned char*>(&rval))[2] = (reinterpret_cast<unsigned char*>(&v))[5];
00116 (reinterpret_cast<unsigned char*>(&rval))[1] = (reinterpret_cast<unsigned char*>(&v))[6];
00117 (reinterpret_cast<unsigned char*>(&rval))[0] = (reinterpret_cast<unsigned char*>(&v))[7];
00118 return rval;
00119 }
00120 inline UInt16 ntoh16(UInt16 v)
00121 {
00122 return hton16(v);
00123 }
00124 inline UInt32 ntoh32(UInt32 v)
00125 {
00126 return hton32(v);
00127 }
00128 inline UInt64 ntoh64(UInt64 v)
00129 {
00130 return hton64(v);
00131 }
00132 #endif
00133 #else // we're big-endian, just pass-thru
00134 inline UInt16 hton16(UInt16 v) { return v; }
00135 inline UInt32 hton32(UInt32 v) { return v; }
00136 inline UInt64 hton64(UInt64 v) { return v; }
00137 inline UInt16 ntoh16(UInt16 v) { return v; }
00138 inline UInt32 ntoh32(UInt32 v) { return v; }
00139 inline UInt64 ntoh64(UInt64 v) { return v; }
00140 #endif
00141
00142 }
00143
00144 #endif