| 1 |
// boost/endian/conversion.hpp -------------------------------------------------------// |
| 2 |
|
| 3 |
// Copyright Beman Dawes 2010, 2011, 2014 |
| 4 |
|
| 5 |
// Distributed under the Boost Software License, Version 1.0. |
| 6 |
// http://www.boost.org/LICENSE_1_0.txt |
| 7 |
|
| 8 |
#ifndef BOOST_ENDIAN_CONVERSION_HPP |
| 9 |
#define BOOST_ENDIAN_CONVERSION_HPP |
| 10 |
|
| 11 |
#include <boost/config.hpp> |
| 12 |
#include <boost/predef/other/endian.h> |
| 13 |
#include <boost/cstdint.hpp> |
| 14 |
#include <boost/endian/detail/intrinsic.hpp> |
| 15 |
#include <boost/core/scoped_enum.hpp> |
| 16 |
#include <boost/static_assert.hpp> |
| 17 |
#include <algorithm> |
| 18 |
#include <cstring> // for memcpy |
| 19 |
|
| 20 |
//------------------------------------- synopsis ---------------------------------------// |
| 21 |
|
| 22 |
namespace boost |
| 23 |
{ |
| 24 |
namespace endian |
| 25 |
{ |
| 26 |
#ifndef BOOST_ENDIAN_ORDER_ENUM_DEFINED |
| 27 |
BOOST_SCOPED_ENUM_START(order) |
| 28 |
{ |
| 29 |
big, little, |
| 30 |
# ifdef BOOST_BIG_ENDIAN |
| 31 |
native = big |
| 32 |
# else |
| 33 |
native = little |
| 34 |
# endif |
| 35 |
}; BOOST_SCOPED_ENUM_END |
| 36 |
# define BOOST_ENDIAN_ORDER_ENUM_DEFINED |
| 37 |
#endif |
| 38 |
|
| 39 |
//--------------------------------------------------------------------------------------// |
| 40 |
// // |
| 41 |
// return-by-value interfaces // |
| 42 |
// suggested by Phil Endecott // |
| 43 |
// // |
| 44 |
// user-defined types (UDTs) // |
| 45 |
// // |
| 46 |
// All return-by-value conversion function templates are required to be implemented in // |
| 47 |
// terms of an unqualified call to "endian_reverse(x)", a function returning the // |
| 48 |
// value of x with endianness reversed. This provides a customization point for any // |
| 49 |
// UDT that provides a "endian_reverse" free-function meeting the requirements. // |
| 50 |
// It must be defined in the same namespace as the UDT itself so that it will be found // |
| 51 |
// by argument dependent lookup (ADL). // |
| 52 |
// // |
| 53 |
//--------------------------------------------------------------------------------------// |
| 54 |
|
| 55 |
// customization for exact-length arithmetic types. See doc/conversion.html/#FAQ |
| 56 |
inline int8_t endian_reverse(int8_t x) BOOST_NOEXCEPT; |
| 57 |
inline int16_t endian_reverse(int16_t x) BOOST_NOEXCEPT; |
| 58 |
inline int32_t endian_reverse(int32_t x) BOOST_NOEXCEPT; |
| 59 |
inline int64_t endian_reverse(int64_t x) BOOST_NOEXCEPT; |
| 60 |
inline uint8_t endian_reverse(uint8_t x) BOOST_NOEXCEPT; |
| 61 |
inline uint16_t endian_reverse(uint16_t x) BOOST_NOEXCEPT; |
| 62 |
inline uint32_t endian_reverse(uint32_t x) BOOST_NOEXCEPT; |
| 63 |
inline uint64_t endian_reverse(uint64_t x) BOOST_NOEXCEPT; |
| 64 |
|
| 65 |
// reverse byte order unless native endianness is big |
| 66 |
template <class EndianReversible > |
| 67 |
inline EndianReversible big_to_native(EndianReversible x) BOOST_NOEXCEPT; |
| 68 |
// Returns: x if native endian order is big, otherwise endian_reverse(x) |
| 69 |
template <class EndianReversible > |
| 70 |
inline EndianReversible native_to_big(EndianReversible x) BOOST_NOEXCEPT; |
| 71 |
// Returns: x if native endian order is big, otherwise endian_reverse(x) |
| 72 |
|
| 73 |
// reverse byte order unless native endianness is little |
| 74 |
template <class EndianReversible > |
| 75 |
inline EndianReversible little_to_native(EndianReversible x) BOOST_NOEXCEPT; |
| 76 |
// Returns: x if native endian order is little, otherwise endian_reverse(x) |
| 77 |
template <class EndianReversible > |
| 78 |
inline EndianReversible native_to_little(EndianReversible x) BOOST_NOEXCEPT; |
| 79 |
// Returns: x if native endian order is little, otherwise endian_reverse(x) |
| 80 |
|
| 81 |
// generic conditional reverse byte order |
| 82 |
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, |
| 83 |
class EndianReversible> |
| 84 |
inline EndianReversible conditional_reverse(EndianReversible from) BOOST_NOEXCEPT; |
| 85 |
// Returns: If From == To have different values, from. |
| 86 |
// Otherwise endian_reverse(from). |
| 87 |
// Remarks: The From == To test, and as a consequence which form the return takes, is |
| 88 |
// is determined at compile time. |
| 89 |
|
| 90 |
// runtime conditional reverse byte order |
| 91 |
template <class EndianReversible > |
| 92 |
inline EndianReversible conditional_reverse(EndianReversible from, |
| 93 |
BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order) |
| 94 |
BOOST_NOEXCEPT; |
| 95 |
// Returns: from_order == to_order ? from : endian_reverse(from). |
| 96 |
|
| 97 |
//------------------------------------------------------------------------------------// |
| 98 |
|
| 99 |
|
| 100 |
// Q: What happended to bswap, htobe, and the other synonym functions based on names |
| 101 |
// popularized by BSD, OS X, and Linux? |
| 102 |
// A: Turned out these may be implemented as macros on some systems. Ditto POSIX names |
| 103 |
// for such functionality. Since macros would cause endless problems with functions |
| 104 |
// of the same names, and these functions are just synonyms anyhow, they have been |
| 105 |
// removed. |
| 106 |
|
| 107 |
|
| 108 |
//------------------------------------------------------------------------------------// |
| 109 |
// // |
| 110 |
// reverse in place interfaces // |
| 111 |
// // |
| 112 |
// user-defined types (UDTs) // |
| 113 |
// // |
| 114 |
// All reverse in place function templates are required to be implemented in terms // |
| 115 |
// of an unqualified call to "endian_reverse_inplace(x)", a function reversing // |
| 116 |
// the endianness of x, which is a non-const reference. This provides a // |
| 117 |
// customization point for any UDT that provides a "reverse_inplace" free-function // |
| 118 |
// meeting the requirements. The free-function must be declared in the same // |
| 119 |
// namespace as the UDT itself so that it will be found by argument-dependent // |
| 120 |
// lookup (ADL). // |
| 121 |
// // |
| 122 |
//------------------------------------------------------------------------------------// |
| 123 |
|
| 124 |
// reverse in place |
| 125 |
template <class EndianReversible> |
| 126 |
inline void endian_reverse_inplace(EndianReversible& x) BOOST_NOEXCEPT; |
| 127 |
// Effects: x = endian_reverse(x) |
| 128 |
|
| 129 |
// reverse in place unless native endianness is big |
| 130 |
template <class EndianReversibleInplace> |
| 131 |
inline void big_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT; |
| 132 |
// Effects: none if native byte-order is big, otherwise endian_reverse_inplace(x) |
| 133 |
template <class EndianReversibleInplace> |
| 134 |
inline void native_to_big_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT; |
| 135 |
// Effects: none if native byte-order is big, otherwise endian_reverse_inplace(x) |
| 136 |
|
| 137 |
// reverse in place unless native endianness is little |
| 138 |
template <class EndianReversibleInplace> |
| 139 |
inline void little_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT; |
| 140 |
// Effects: none if native byte-order is little, otherwise endian_reverse_inplace(x); |
| 141 |
template <class EndianReversibleInplace> |
| 142 |
inline void native_to_little_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT; |
| 143 |
// Effects: none if native byte-order is little, otherwise endian_reverse_inplace(x); |
| 144 |
|
| 145 |
// generic conditional reverse in place |
| 146 |
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, |
| 147 |
class EndianReversibleInplace> |
| 148 |
inline void conditional_reverse_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT; |
| 149 |
|
| 150 |
// runtime reverse in place |
| 151 |
template <class EndianReversibleInplace> |
| 152 |
inline void conditional_reverse_inplace(EndianReversibleInplace& x, |
| 153 |
BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order) |
| 154 |
BOOST_NOEXCEPT; |
| 155 |
|
| 156 |
//----------------------------------- end synopsis -------------------------------------// |
| 157 |
|
| 158 |
namespace detail |
| 159 |
{ |
| 160 |
// generic reverse function template implementation approach using std::reverse |
| 161 |
// suggested by Mathias Gaunard. Primary motivation for inclusion is to have an |
| 162 |
// independent implementation to test against. |
| 163 |
|
| 164 |
template <class T> |
| 165 |
inline T std_endian_reverse(T x) BOOST_NOEXCEPT |
| 166 |
{ |
| 167 |
T tmp(x); |
| 168 |
std::reverse( |
| 169 |
reinterpret_cast<unsigned char*>(&tmp), |
| 170 |
reinterpret_cast<unsigned char*>(&tmp) + sizeof(T)); |
| 171 |
return tmp; |
| 172 |
} |
| 173 |
|
| 174 |
// conditional unaligned reverse copy, patterned after std::reverse_copy |
| 175 |
template <class T> |
| 176 |
inline void big_reverse_copy(T from, char* to) BOOST_NOEXCEPT; |
| 177 |
template <class T> |
| 178 |
inline void big_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT; |
| 179 |
template <class T> |
| 180 |
inline void little_reverse_copy(T from, char* to) BOOST_NOEXCEPT; |
| 181 |
template <class T> |
| 182 |
inline void little_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT; |
| 183 |
} // namespace detail |
| 184 |
|
| 185 |
//--------------------------------------------------------------------------------------// |
| 186 |
// // |
| 187 |
// return-by-value implementation // |
| 188 |
// // |
| 189 |
// -- portable approach suggested by tymofey, with avoidance of undefined behavior // |
| 190 |
// as suggested by Giovanni Piero Deretta, with a further refinement suggested // |
| 191 |
// by Pyry Jahkola. // |
| 192 |
// -- intrinsic approach suggested by reviewers, and by David Stone, who provided // |
| 193 |
// his Boost licensed macro implementation (detail/intrinsic.hpp) // |
| 194 |
// // |
| 195 |
//--------------------------------------------------------------------------------------// |
| 196 |
|
| 197 |
inline int8_t endian_reverse(int8_t x) BOOST_NOEXCEPT |
| 198 |
{ |
| 199 |
return x; |
| 200 |
} |
| 201 |
|
| 202 |
inline int16_t endian_reverse(int16_t x) BOOST_NOEXCEPT |
| 203 |
{ |
| 204 |
# ifdef BOOST_ENDIAN_NO_INTRINSICS |
| 205 |
return (static_cast<uint16_t>(x) << 8) |
| 206 |
| (static_cast<uint16_t>(x) >> 8); |
| 207 |
# else |
| 208 |
return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(static_cast<uint16_t>(x)); |
| 209 |
# endif |
| 210 |
} |
| 211 |
|
| 212 |
inline int32_t endian_reverse(int32_t x) BOOST_NOEXCEPT |
| 213 |
{ |
| 214 |
# ifdef BOOST_ENDIAN_NO_INTRINSICS |
| 215 |
uint32_t step16; |
| 216 |
step16 = static_cast<uint32_t>(x) << 16 | static_cast<uint32_t>(x) >> 16; |
| 217 |
return |
| 218 |
((static_cast<uint32_t>(step16) << 8) & 0xff00ff00) |
| 219 |
| ((static_cast<uint32_t>(step16) >> 8) & 0x00ff00ff); |
| 220 |
# else |
| 221 |
return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(static_cast<uint32_t>(x)); |
| 222 |
# endif |
| 223 |
} |
| 224 |
|
| 225 |
inline int64_t endian_reverse(int64_t x) BOOST_NOEXCEPT |
| 226 |
{ |
| 227 |
# ifdef BOOST_ENDIAN_NO_INTRINSICS |
| 228 |
uint64_t step32, step16; |
| 229 |
step32 = static_cast<uint64_t>(x) << 32 | static_cast<uint64_t>(x) >> 32; |
| 230 |
step16 = (step32 & 0x0000FFFF0000FFFFULL) << 16 |
| 231 |
| (step32 & 0xFFFF0000FFFF0000ULL) >> 16; |
| 232 |
return static_cast<int64_t>((step16 & 0x00FF00FF00FF00FFULL) << 8 |
| 233 |
| (step16 & 0xFF00FF00FF00FF00ULL) >> 8); |
| 234 |
# else |
| 235 |
return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(static_cast<uint64_t>(x)); |
| 236 |
# endif |
| 237 |
} |
| 238 |
|
| 239 |
inline uint8_t endian_reverse(uint8_t x) BOOST_NOEXCEPT |
| 240 |
{ |
| 241 |
return x; |
| 242 |
} |
| 243 |
|
| 244 |
inline uint16_t endian_reverse(uint16_t x) BOOST_NOEXCEPT |
| 245 |
{ |
| 246 |
# ifdef BOOST_ENDIAN_NO_INTRINSICS |
| 247 |
return (x << 8) |
| 248 |
| (x >> 8); |
| 249 |
# else |
| 250 |
return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x); |
| 251 |
# endif |
| 252 |
} |
| 253 |
|
| 254 |
inline uint32_t endian_reverse(uint32_t x) BOOST_NOEXCEPT |
| 255 |
{ |
| 256 |
# ifdef BOOST_ENDIAN_NO_INTRINSICS |
| 257 |
uint32_t step16; |
| 258 |
step16 = x << 16 | x >> 16; |
| 259 |
return |
| 260 |
((step16 << 8) & 0xff00ff00) |
| 261 |
| ((step16 >> 8) & 0x00ff00ff); |
| 262 |
# else |
| 263 |
return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x); |
| 264 |
# endif |
| 265 |
} |
| 266 |
|
| 267 |
inline uint64_t endian_reverse(uint64_t x) BOOST_NOEXCEPT |
| 268 |
{ |
| 269 |
# ifdef BOOST_ENDIAN_NO_INTRINSICS |
| 270 |
uint64_t step32, step16; |
| 271 |
step32 = x << 32 | x >> 32; |
| 272 |
step16 = (step32 & 0x0000FFFF0000FFFFULL) << 16 |
| 273 |
| (step32 & 0xFFFF0000FFFF0000ULL) >> 16; |
| 274 |
return (step16 & 0x00FF00FF00FF00FFULL) << 8 |
| 275 |
| (step16 & 0xFF00FF00FF00FF00ULL) >> 8; |
| 276 |
# else |
| 277 |
return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x); |
| 278 |
# endif |
| 279 |
} |
| 280 |
|
| 281 |
template <class EndianReversible > |
| 282 |
inline EndianReversible big_to_native(EndianReversible x) BOOST_NOEXCEPT |
| 283 |
{ |
| 284 |
# ifdef BOOST_BIG_ENDIAN |
| 285 |
return x; |
| 286 |
# else |
| 287 |
return endian_reverse(x); |
| 288 |
# endif |
| 289 |
} |
| 290 |
|
| 291 |
template <class EndianReversible > |
| 292 |
inline EndianReversible native_to_big(EndianReversible x) BOOST_NOEXCEPT |
| 293 |
{ |
| 294 |
# ifdef BOOST_BIG_ENDIAN |
| 295 |
return x; |
| 296 |
# else |
| 297 |
return endian_reverse(x); |
| 298 |
# endif |
| 299 |
} |
| 300 |
|
| 301 |
template <class EndianReversible > |
| 302 |
inline EndianReversible little_to_native(EndianReversible x) BOOST_NOEXCEPT |
| 303 |
{ |
| 304 |
# ifdef BOOST_LITTLE_ENDIAN |
| 305 |
return x; |
| 306 |
# else |
| 307 |
return endian_reverse(x); |
| 308 |
# endif |
| 309 |
} |
| 310 |
|
| 311 |
template <class EndianReversible > |
| 312 |
inline EndianReversible native_to_little(EndianReversible x) BOOST_NOEXCEPT |
| 313 |
{ |
| 314 |
# ifdef BOOST_LITTLE_ENDIAN |
| 315 |
return x; |
| 316 |
# else |
| 317 |
return endian_reverse(x); |
| 318 |
# endif |
| 319 |
} |
| 320 |
|
| 321 |
namespace detail |
| 322 |
{ |
| 323 |
// Primary template and specializations to support endian_reverse(). |
| 324 |
// See rationale in endian_reverse() below. |
| 325 |
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, |
| 326 |
class EndianReversible> |
| 327 |
class value_converter ; // primary template |
| 328 |
template <class T> class value_converter <order::big, order::big, T> |
| 329 |
{public: T operator()(T x) BOOST_NOEXCEPT {return x;}}; |
| 330 |
template <class T> class value_converter <order::little, order::little, T> |
| 331 |
{public: T operator()(T x) BOOST_NOEXCEPT {return x;}}; |
| 332 |
template <class T> class value_converter <order::big, order::little, T> |
| 333 |
{public: T operator()(T x) BOOST_NOEXCEPT {return endian_reverse(x);}}; |
| 334 |
template <class T> class value_converter <order::little, order::big, T> |
| 335 |
{public: T operator()(T x) BOOST_NOEXCEPT {return endian_reverse(x);}}; |
| 336 |
} |
| 337 |
|
| 338 |
// generic conditional reverse |
| 339 |
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, |
| 340 |
class EndianReversible> |
| 341 |
inline EndianReversible conditional_reverse(EndianReversible from) BOOST_NOEXCEPT { |
| 342 |
// work around lack of function template partial specialization by instantiating |
| 343 |
// a function object of a class that is partially specialized on the two order |
| 344 |
// template parameters, and then calling its operator(). |
| 345 |
detail::value_converter <From, To, EndianReversible> tmp; |
| 346 |
return tmp(from); |
| 347 |
} |
| 348 |
|
| 349 |
// runtime conditional reverse |
| 350 |
template <class EndianReversible > |
| 351 |
inline EndianReversible conditional_reverse(EndianReversible from, |
| 352 |
BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order) BOOST_NOEXCEPT |
| 353 |
{ |
| 354 |
return from_order == to_order ? from : endian_reverse(from); |
| 355 |
} |
| 356 |
|
| 357 |
//--------------------------------------------------------------------------------------// |
| 358 |
// reverse-in-place implementation // |
| 359 |
//--------------------------------------------------------------------------------------// |
| 360 |
|
| 361 |
// reverse in place |
| 362 |
template <class EndianReversible> |
| 363 |
inline void endian_reverse_inplace(EndianReversible& x) BOOST_NOEXCEPT |
| 364 |
{ |
| 365 |
x = endian_reverse(x); |
| 366 |
} |
| 367 |
|
| 368 |
template <class EndianReversibleInplace> |
| 369 |
# ifdef BOOST_BIG_ENDIAN |
| 370 |
inline void big_to_native_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {} |
| 371 |
# else |
| 372 |
inline void big_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT |
| 373 |
{ endian_reverse_inplace(x); } |
| 374 |
# endif |
| 375 |
template <class EndianReversibleInplace> |
| 376 |
# ifdef BOOST_BIG_ENDIAN |
| 377 |
inline void native_to_big_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {} |
| 378 |
# else |
| 379 |
inline void native_to_big_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT |
| 380 |
{ |
| 381 |
endian_reverse_inplace(x); |
| 382 |
} |
| 383 |
# endif |
| 384 |
|
| 385 |
template <class EndianReversibleInplace> |
| 386 |
# ifdef BOOST_LITTLE_ENDIAN |
| 387 |
inline void little_to_native_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {} |
| 388 |
# else |
| 389 |
inline void little_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT |
| 390 |
{ endian_reverse_inplace(x); } |
| 391 |
# endif |
| 392 |
template <class EndianReversibleInplace> |
| 393 |
# ifdef BOOST_LITTLE_ENDIAN |
| 394 |
inline void native_to_little_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {} |
| 395 |
# else |
| 396 |
inline void native_to_little_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT |
| 397 |
{ |
| 398 |
endian_reverse_inplace(x); |
| 399 |
} |
| 400 |
# endif |
| 401 |
|
| 402 |
namespace detail |
| 403 |
{ |
| 404 |
// Primary template and specializations support generic |
| 405 |
// endian_reverse_inplace(). |
| 406 |
// See rationale in endian_reverse_inplace() below. |
| 407 |
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, |
| 408 |
class EndianReversibleInplace> |
| 409 |
class converter; // primary template |
| 410 |
template <class T> class converter<order::big, order::big, T> |
| 411 |
{public: void operator()(T&) BOOST_NOEXCEPT {/*no effect*/}}; |
| 412 |
template <class T> class converter<order::little, order::little, T> |
| 413 |
{public: void operator()(T&) BOOST_NOEXCEPT {/*no effect*/}}; |
| 414 |
template <class T> class converter<order::big, order::little, T> |
| 415 |
{public: void operator()(T& x) BOOST_NOEXCEPT { endian_reverse_inplace(x); }}; |
| 416 |
template <class T> class converter<order::little, order::big, T> |
| 417 |
{public: void operator()(T& x) BOOST_NOEXCEPT { endian_reverse_inplace(x); }}; |
| 418 |
} // namespace detail |
| 419 |
|
| 420 |
// generic conditional reverse in place |
| 421 |
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, |
| 422 |
class EndianReversibleInplace> |
| 423 |
inline void conditional_reverse_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT |
| 424 |
{ |
| 425 |
// work around lack of function template partial specialization by instantiating |
| 426 |
// a function object of a class that is partially specialized on the two order |
| 427 |
// template parameters, and then calling its operator(). |
| 428 |
detail::converter<From, To, EndianReversibleInplace> tmp; |
| 429 |
tmp(x); // call operator () |
| 430 |
} |
| 431 |
|
| 432 |
// runtime reverse in place |
| 433 |
template <class EndianReversibleInplace> |
| 434 |
inline void conditional_reverse_inplace(EndianReversibleInplace& x, |
| 435 |
BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order) |
| 436 |
BOOST_NOEXCEPT |
| 437 |
{ |
| 438 |
if (from_order != to_order) |
| 439 |
endian_reverse_inplace(x); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
namespace detail |
| 444 |
{ |
| 445 |
template <class T> |
| 446 |
inline void big_reverse_copy(T from, char* to) BOOST_NOEXCEPT |
| 447 |
{ |
| 448 |
# ifdef BOOST_BIG_ENDIAN |
| 449 |
std::memcpy(to, reinterpret_cast<const char*>(&from), sizeof(T)); |
| 450 |
# else |
| 451 |
std::reverse_copy(reinterpret_cast<const char*>(&from), |
| 452 |
reinterpret_cast<const char*>(&from) + sizeof(T), to); |
| 453 |
# endif |
| 454 |
} |
| 455 |
template <class T> |
| 456 |
inline void big_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT |
| 457 |
{ |
| 458 |
# ifdef BOOST_BIG_ENDIAN |
| 459 |
std::memcpy(reinterpret_cast<char*>(&to), from, sizeof(T)); |
| 460 |
# else |
| 461 |
std::reverse_copy(from, from + sizeof(T), reinterpret_cast<char*>(&to)); |
| 462 |
# endif |
| 463 |
} |
| 464 |
template <class T> |
| 465 |
inline void little_reverse_copy(T from, char* to) BOOST_NOEXCEPT |
| 466 |
{ |
| 467 |
# ifdef BOOST_LITTLE_ENDIAN |
| 468 |
std::memcpy(to, reinterpret_cast<const char*>(&from), sizeof(T)); |
| 469 |
# else |
| 470 |
std::reverse_copy(reinterpret_cast<const char*>(&from), |
| 471 |
reinterpret_cast<const char*>(&from) + sizeof(T), to); |
| 472 |
# endif |
| 473 |
} |
| 474 |
template <class T> |
| 475 |
inline void little_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT |
| 476 |
{ |
| 477 |
# ifdef BOOST_LITTLE_ENDIAN |
| 478 |
std::memcpy(reinterpret_cast<char*>(&to), from, sizeof(T)); |
| 479 |
# else |
| 480 |
std::reverse_copy(from, from + sizeof(T), reinterpret_cast<char*>(&to)); |
| 481 |
# endif |
| 482 |
} |
| 483 |
} // namespace detail |
| 484 |
} // namespace endian |
| 485 |
} // namespace boost |
| 486 |
|
| 487 |
#endif // BOOST_ENDIAN_CONVERSION_HPP |