ByteSwap.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2004 Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2005 Novell, Inc. All rights reserved.
00004 *
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 *
00008 *  - Redistributions of source code must retain the above copyright notice,
00009 *    this list of conditions and the following disclaimer.
00010 *
00011 *  - Redistributions in binary form must reproduce the above copyright notice,
00012 *    this list of conditions and the following disclaimer in the documentation
00013 *    and/or other materials provided with the distribution.
00014 *
00015 *  - Neither the name of Vintela, Inc., Novell, Inc., nor the names of its
00016 *    contributors may be used to endorse or promote products derived from this
00017 *    software without specific prior written permission.
00018 *
00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc., Novell, Inc., OR THE 
00023 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00024 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00025 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00026 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00027 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00028 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00029 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 // this will be defined by the configure script.
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 } // end namespace BLOCXX_NAMESPACE
00142 
00143 #endif

Generated on Fri Jun 16 15:39:08 2006 for blocxx by  doxygen 1.4.6