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