$treeview $search $mathjax
|
Palabos
Version 1.1
$projectbrief
|
$projectbrief
|
$searchbox |
00001 /* This file is part of the Palabos library. 00002 * 00003 * Copyright (C) 2011 FlowKit Sarl 00004 * Avenue de Chailly 23 00005 * 1012 Lausanne, Switzerland 00006 * E-mail contact: contact@flowkit.com 00007 * 00008 * The most recent release of Palabos can be downloaded at 00009 * <http://www.palabos.org/> 00010 * 00011 * The library Palabos is free software: you can redistribute it and/or 00012 * modify it under the terms of the GNU Affero General Public License as 00013 * published by the Free Software Foundation, either version 3 of the 00014 * License, or (at your option) any later version. 00015 * 00016 * The library is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Affero General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU Affero General Public License 00022 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00023 */ 00024 00025 00026 #ifndef ENDIANNESS_H 00027 #define ENDIANNESS_H 00028 00029 #include "core/globalDefs.h" 00030 #include <algorithm> 00031 00032 namespace plb { 00033 00034 template<int nBytes> inline void endianByteSwapImpl(char const* src, char* dest); 00035 template<int nBytes> inline void endianByteSwapImpl(char* value); 00036 00037 template<typename T> inline void endianByteSwap(T const& src, T& dest) { 00038 endianByteSwapImpl<sizeof(T)> ( 00039 reinterpret_cast< char const* > (&src), reinterpret_cast< char* > (&dest) ); 00040 } 00041 00042 template<typename T> inline void endianByteSwap(T& value) { 00043 endianByteSwapImpl<sizeof(T)> ( reinterpret_cast< char* > (&value) ); 00044 } 00045 00046 // Specialization for 1-byte types. 00047 template<> 00048 inline void endianByteSwapImpl<1>(char const* src, char* dest) 00049 { 00050 dest[0] = src[0]; 00051 } 00052 00053 // Specialization for 1-byte types. 00054 template<> 00055 inline void endianByteSwapImpl<1>(char* value) 00056 { } 00057 00058 // Specialization for 2-byte types. 00059 template<> 00060 inline void endianByteSwapImpl<2>(char const* src, char* dest) 00061 { 00062 dest[0] = src[1]; 00063 dest[1] = src[0]; 00064 } 00065 00066 // Specialization for 2-byte types. 00067 template<> 00068 inline void endianByteSwapImpl<2>(char* value) 00069 { 00070 std::swap(value[0], value[1]); 00071 } 00072 00073 // Specialization for 4-byte types. 00074 template<> 00075 inline void endianByteSwapImpl<4>(char const* src, char* dest) 00076 { 00077 dest[0] = src[3]; 00078 dest[1] = src[2]; 00079 dest[2] = src[1]; 00080 dest[3] = src[0]; 00081 } 00082 00083 // Specialization for 4-byte types. 00084 template<> 00085 inline void endianByteSwapImpl<4>(char* value) 00086 { 00087 std::swap(value[0], value[3]); 00088 std::swap(value[1], value[2]); 00089 } 00090 00091 // Specialization for 8-byte types. 00092 template<> 00093 inline void endianByteSwapImpl<8>(char const* src, char* dest) 00094 { 00095 dest[0] = src[7]; 00096 dest[1] = src[6]; 00097 dest[2] = src[5]; 00098 dest[3] = src[4]; 00099 dest[4] = src[3]; 00100 dest[5] = src[2]; 00101 dest[6] = src[1]; 00102 dest[7] = src[0]; 00103 } 00104 00105 // Specialization for 8-byte types. 00106 template<> 00107 inline void endianByteSwapImpl<8>(char* value) 00108 { 00109 std::swap(value[0], value[7]); 00110 std::swap(value[1], value[6]); 00111 std::swap(value[2], value[5]); 00112 std::swap(value[3], value[4]); 00113 } 00114 00115 00116 // Specialization for 12-byte types. 00117 template<> 00118 inline void endianByteSwapImpl<12>(char const* src, char* dest) 00119 { 00120 dest[0] = src[11]; 00121 dest[1] = src[10]; 00122 dest[2] = src[9]; 00123 dest[3] = src[8]; 00124 dest[4] = src[7]; 00125 dest[5] = src[6]; 00126 dest[6] = src[5]; 00127 dest[7] = src[4]; 00128 dest[8] = src[3]; 00129 dest[9] = src[2]; 00130 dest[10] = src[1]; 00131 dest[11] = src[0]; 00132 } 00133 00134 // Specialization for 12-byte types. 00135 template<> 00136 inline void endianByteSwapImpl<12>(char* value) 00137 { 00138 std::swap(value[0], value[11]); 00139 std::swap(value[1], value[10]); 00140 std::swap(value[2], value[9]); 00141 std::swap(value[3], value[8]); 00142 std::swap(value[4], value[7]); 00143 std::swap(value[5], value[6]); 00144 } 00145 00146 // Specialization for 16-byte types. 00147 template<> 00148 inline void endianByteSwapImpl<16>(char const* src, char* dest) 00149 { 00150 dest[0] = src[15]; 00151 dest[1] = src[14]; 00152 dest[2] = src[13]; 00153 dest[3] = src[12]; 00154 dest[4] = src[11]; 00155 dest[5] = src[10]; 00156 dest[6] = src[9]; 00157 dest[7] = src[8]; 00158 dest[8] = src[7]; 00159 dest[9] = src[6]; 00160 dest[10] = src[5]; 00161 dest[11] = src[4]; 00162 dest[12] = src[3]; 00163 dest[13] = src[2]; 00164 dest[14] = src[1]; 00165 dest[15] = src[0]; 00166 } 00167 00168 // Specialization for 16-byte types. 00169 template<> 00170 inline void endianByteSwapImpl<16>(char* value) 00171 { 00172 std::swap(value[0], value[15]); 00173 std::swap(value[1], value[14]); 00174 std::swap(value[2], value[13]); 00175 std::swap(value[3], value[12]); 00176 std::swap(value[4], value[11]); 00177 std::swap(value[5], value[10]); 00178 std::swap(value[6], value[9]); 00179 std::swap(value[7], value[8]); 00180 } 00181 00182 } // namespace plb 00183 00184 #endif // ENDIANNESS_H
1.6.3
1.6.3