From 9b6c747e2ac7ef7aa8e5c31f55d5708889c49ae7 Mon Sep 17 00:00:00 2001 From: bemandawes Date: Sun, 22 May 2011 19:27:57 +0000 Subject: [PATCH] Rename endian_flip.hpp to conversion.hpp git-svn-id: http://svn.boost.org/svn/boost/sandbox/endian@72092 b8fc166d-592f-0410-95f2-cb63ce0dd405 --- boost/integer/conversion.hpp | 202 +++++++++++++++++++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 boost/integer/conversion.hpp diff --git a/boost/integer/conversion.hpp b/boost/integer/conversion.hpp new file mode 100644 index 0000000..5336d1a --- /dev/null +++ b/boost/integer/conversion.hpp @@ -0,0 +1,202 @@ +// boost/endian/conversion.hpp -------------------------------------------------------// + +// Copyright Beman Dawes 2010, 2011 + +// Distributed under the Boost Software License, Version 1.0. +// http://www.boost.org/LICENSE_1_0.txt + +#ifndef BOOST_ENDIAN_CONVERSION_HPP +#define BOOST_ENDIAN_CONVERSION_HPP + +#include +#include + +//------------------------------------- synopsis ---------------------------------------// + +namespace boost +{ +namespace endian +{ + // unconditional modifying (i.e. in-place) endianness reversal + + inline void flip(int16_t& x); + inline void flip(int32_t& x); + inline void flip(int64_t& x); + inline void flip(uint16_t& x); + inline void flip(uint32_t& x); + inline void flip(uint64_t& x); + + // unconditional non-modifying endianness reversing copy + + inline void flip(int16_t source, int16_t& target); + inline void flip(int32_t source, int32_t& target); + inline void flip(int64_t source, int64_t& target); + inline void flip(uint16_t source, uint16_t& target); + inline void flip(uint32_t source, uint32_t& target); + inline void flip(uint64_t source, uint64_t& target); + + // conditional modifying (i.e. in-place) endianness reversal; + // no effect if native endianness and specified endianness are the same + + template inline void to_big(T& x); // if different, convert native to big + template inline void to_little(T& x); // if different, convert native to little + template inline void from_big(T& x); // if different, convert big to native + template inline void from_little(T& x); // if different, convert little to native + + // non-modifying copy, conditionally reversing endianness; + // copy the first argument to the second argument, converting to or from the + // specified endianness if different than native endianness + + template inline void to_big(T native, T& big); + template inline void to_little(T native, T& little); + template inline void from_big(T big, T& native); + template inline void from_little(T little, T& native); + +//----------------------------------- implementation -----------------------------------// + + inline void flip(int16_t& x) + { + char* rep = reinterpret_cast(&x); + char tmp; + tmp = *rep; + *rep = *(rep+1); + *(rep+1) = tmp; + } + + inline void flip(int32_t& x) + { + char* rep = reinterpret_cast(&x); + char tmp; + tmp = *rep; + *rep = *(rep+3); + *(rep+3) = tmp; + tmp = *(rep+1); + *(rep+1) = *(rep+2); + *(rep+2) = tmp; + } + + inline void flip(int64_t& x) + { + char* rep = reinterpret_cast(&x); + char tmp; + tmp = *rep; + *rep = *(rep+7); + *(rep+7) = tmp; + tmp = *(rep+1); + *(rep+1) = *(rep+6); + *(rep+6) = tmp; + tmp = *(rep+2); + *(rep+2) = *(rep+5); + *(rep+5) = tmp; + tmp = *(rep+3); + *(rep+3) = *(rep+4); + *(rep+4) = tmp; + } + + inline void flip(uint16_t& x) + { + char* rep = reinterpret_cast(&x); + char tmp; + tmp = *rep; + *rep = *(rep+1); + *(rep+1) = tmp; + } + + inline void flip(uint32_t& x) + { + char* rep = reinterpret_cast(&x); + char tmp; + tmp = *rep; + *rep = *(rep+3); + *(rep+3) = tmp; + tmp = *(rep+1); + *(rep+1) = *(rep+2); + *(rep+2) = tmp; + } + + inline void flip(uint64_t& x) + { + char* rep = reinterpret_cast(&x); + char tmp; + tmp = *rep; + *rep = *(rep+7); + *(rep+7) = tmp; + tmp = *(rep+1); + *(rep+1) = *(rep+6); + *(rep+6) = tmp; + tmp = *(rep+2); + *(rep+2) = *(rep+5); + *(rep+5) = tmp; + tmp = *(rep+3); + *(rep+3) = *(rep+4); + *(rep+4) = tmp; + } + + inline void flip(int16_t source, int16_t& target) + { + const char* s (reinterpret_cast(&source)); + char * t (reinterpret_cast(&target) + sizeof(target) - 1); + *t = *s; + *++t = *++s; + } + + inline void flip(int32_t source, int32_t& target) + { + const char* s (reinterpret_cast(&source)); + char * t (reinterpret_cast(&target) + sizeof(target) - 1); + *t = *s; + *++t = *++s; + *++t = *++s; + *++t = *++s; + } + + inline void flip(int64_t source, int64_t& target) + { + const char* s (reinterpret_cast(&source)); + char * t (reinterpret_cast(&target) + sizeof(target) - 1); + *t = *s; + *++t = *++s; + *++t = *++s; + *++t = *++s; + *++t = *++s; + *++t = *++s; + *++t = *++s; + *++t = *++s; + } + + inline void flip(uint16_t source, uint16_t& target) + { + const char* s (reinterpret_cast(&source)); + char * t (reinterpret_cast(&target) + sizeof(target) - 1); + *t = *s; + *++t = *++s; + } + + inline void flip(uint32_t source, uint32_t& target) + { + const char* s (reinterpret_cast(&source)); + char * t (reinterpret_cast(&target) + sizeof(target) - 1); + *t = *s; + *++t = *++s; + *++t = *++s; + *++t = *++s; + } + + inline void flip(uint64_t source, uint64_t& target) + { + const char* s (reinterpret_cast(&source)); + char * t (reinterpret_cast(&target) + sizeof(target) - 1); + *t = *s; + *++t = *++s; + *++t = *++s; + *++t = *++s; + *++t = *++s; + *++t = *++s; + *++t = *++s; + *++t = *++s; + } + +} // namespace endian +} // namespace boost + +#endif // BOOST_ENDIAN_CONVERSION_HPP