Ha! Replace invert() with reorder(). That finally feels right.

git-svn-id: http://svn.boost.org/svn/boost/sandbox/endian@74215 b8fc166d-592f-0410-95f2-cb63ce0dd405
This commit is contained in:
bemandawes
2011-09-03 20:42:28 +00:00
parent a114dd72b5
commit f243089ef4
4 changed files with 123 additions and 125 deletions

View File

@@ -17,25 +17,25 @@ namespace boost
{
namespace endian
{
// unconditional modifying (i.e. in-place) endianness reversal
// unconditional modifying (i.e. in-place) reverse byte order
inline void invert(int16_t& x);
inline void invert(int32_t& x);
inline void invert(int64_t& x);
inline void invert(uint16_t& x);
inline void invert(uint32_t& x);
inline void invert(uint64_t& x);
inline void reorder(int16_t& x);
inline void reorder(int32_t& x);
inline void reorder(int64_t& x);
inline void reorder(uint16_t& x);
inline void reorder(uint32_t& x);
inline void reorder(uint64_t& x);
// unconditional non-modifying endianness reversing copy
// unconditional non-modifying reverse byte order copy
inline void invert(int16_t source, int16_t& target);
inline void invert(int32_t source, int32_t& target);
inline void invert(int64_t source, int64_t& target);
inline void invert(uint16_t source, uint16_t& target);
inline void invert(uint32_t source, uint32_t& target);
inline void invert(uint64_t source, uint64_t& target);
inline void reorder(int16_t source, int16_t& target);
inline void reorder(int32_t source, int32_t& target);
inline void reorder(int64_t source, int64_t& target);
inline void reorder(uint16_t source, uint16_t& target);
inline void reorder(uint32_t source, uint32_t& target);
inline void reorder(uint64_t source, uint64_t& target);
// conditional modifying (i.e. in-place) endianness reversal;
// conditional modifying (i.e. in-place) reverse byte order;
// no effect if native endianness and indicated endianness are the same
template <class T> inline void native_to_big(T& x);
@@ -43,7 +43,7 @@ namespace endian
template <class T> inline void big_to_native(T& x);
template <class T> inline void little_to_native(T& x);
// non-modifying copy conditionally reversing endianness;
// non-modifying conditional reverse byte order copy;
// copy the source argument to the target argument, converting to or from the
// indicated endianness if different than native endianness
@@ -54,7 +54,7 @@ namespace endian
//----------------------------------- implementation -----------------------------------//
inline void invert(int16_t& x)
inline void reorder(int16_t& x)
{
char* rep = reinterpret_cast<char*>(&x);
char tmp;
@@ -63,7 +63,7 @@ namespace endian
*(rep+1) = tmp;
}
inline void invert(int32_t& x)
inline void reorder(int32_t& x)
{
char* rep = reinterpret_cast<char*>(&x);
char tmp;
@@ -75,7 +75,7 @@ namespace endian
*(rep+2) = tmp;
}
inline void invert(int64_t& x)
inline void reorder(int64_t& x)
{
char* rep = reinterpret_cast<char*>(&x);
char tmp;
@@ -93,7 +93,7 @@ namespace endian
*(rep+4) = tmp;
}
inline void invert(uint16_t& x)
inline void reorder(uint16_t& x)
{
char* rep = reinterpret_cast<char*>(&x);
char tmp;
@@ -102,7 +102,7 @@ namespace endian
*(rep+1) = tmp;
}
inline void invert(uint32_t& x)
inline void reorder(uint32_t& x)
{
char* rep = reinterpret_cast<char*>(&x);
char tmp;
@@ -114,7 +114,7 @@ namespace endian
*(rep+2) = tmp;
}
inline void invert(uint64_t& x)
inline void reorder(uint64_t& x)
{
char* rep = reinterpret_cast<char*>(&x);
char tmp;
@@ -132,7 +132,7 @@ namespace endian
*(rep+4) = tmp;
}
inline void invert(int16_t source, int16_t& target)
inline void reorder(int16_t source, int16_t& target)
{
const char* s (reinterpret_cast<const char*>(&source));
char * t (reinterpret_cast<char*>(&target) + sizeof(target) - 1);
@@ -140,7 +140,7 @@ namespace endian
*--t = *++s;
}
inline void invert(int32_t source, int32_t& target)
inline void reorder(int32_t source, int32_t& target)
{
const char* s (reinterpret_cast<const char*>(&source));
char * t (reinterpret_cast<char*>(&target) + sizeof(target) - 1);
@@ -150,7 +150,7 @@ namespace endian
*--t = *++s;
}
inline void invert(int64_t source, int64_t& target)
inline void reorder(int64_t source, int64_t& target)
{
const char* s (reinterpret_cast<const char*>(&source));
char * t (reinterpret_cast<char*>(&target) + sizeof(target) - 1);
@@ -164,7 +164,7 @@ namespace endian
*--t = *++s;
}
inline void invert(uint16_t source, uint16_t& target)
inline void reorder(uint16_t source, uint16_t& target)
{
const char* s (reinterpret_cast<const char*>(&source));
char * t (reinterpret_cast<char*>(&target) + sizeof(target) - 1);
@@ -172,7 +172,7 @@ namespace endian
*--t = *++s;
}
inline void invert(uint32_t source, uint32_t& target)
inline void reorder(uint32_t source, uint32_t& target)
{
const char* s (reinterpret_cast<const char*>(&source));
char * t (reinterpret_cast<char*>(&target) + sizeof(target) - 1);
@@ -182,7 +182,7 @@ namespace endian
*--t = *++s;
}
inline void invert(uint64_t source, uint64_t& target)
inline void reorder(uint64_t source, uint64_t& target)
{
const char* s (reinterpret_cast<const char*>(&source));
char * t (reinterpret_cast<char*>(&target) + sizeof(target) - 1);
@@ -197,23 +197,23 @@ namespace endian
}
#ifdef BOOST_LITTLE_ENDIAN
template <class T> inline void native_to_big(T& x) { invert(x); }
template <class T> inline void native_to_big(T& x) { reorder(x); }
template <class T> inline void native_to_little(T&) {}
template <class T> inline void big_to_native(T& x) { invert(x); }
template <class T> inline void big_to_native(T& x) { reorder(x); }
template <class T> inline void little_to_native(T&) {}
template <class T> inline void native_to_big(T source, T& target) { invert(source, target); }
template <class T> inline void native_to_big(T source, T& target) { reorder(source, target); }
template <class T> inline void native_to_little(T source, T& target) { target = source; }
template <class T> inline void big_to_native(T source, T& target) { invert(source, target); }
template <class T> inline void big_to_native(T source, T& target) { reorder(source, target); }
template <class T> inline void little_to_native(T source, T& target) { target = source; }
#else
template <class T> inline void native_to_big(T&) {}
template <class T> inline void native_to_little(T& x) { invert(x); }
template <class T> inline void native_to_little(T& x) { reorder(x); }
template <class T> inline void big_to_native(T&) {}
template <class T> inline void little_to_native(T& x) { invert(x); }
template <class T> inline void little_to_native(T& x) { reorder(x); }
template <class T> inline void native_to_big(T native, T& big) { target = source; }
template <class T> inline void native_to_little(T native, T& little) { invert(source, target); }
template <class T> inline void native_to_little(T native, T& little) { reorder(source, target); }
template <class T> inline void big_to_native(T big, T& native) { target = source; }
template <class T> inline void little_to_native(T little, T& native) { invert(source, target); }
template <class T> inline void little_to_native(T little, T& native) { reorder(source, target); }
#endif
} // namespace endian

View File

@@ -46,34 +46,32 @@ ordering.</p>
{
namespace endian
{
// invert: <span class="f">verb: p</span>ut upside down or in the opposite position, order, or arrangement.
// unconditional modifying (i.e. in-place) reverse byte order
// unconditional modifying (i.e. in-place) endianness reversal
inline void reorder(int16_t&amp; x);
inline void reorder(int32_t&amp; x);
inline void reorder(int64_t&amp; x);
inline void reorder(uint16_t&amp; x);
inline void reorder(uint32_t&amp; x);
inline void reorder(uint64_t&amp; x);
inline void invert(int16_t&amp; x);
inline void invert(int32_t&amp; x);
inline void invert(int64_t&amp; x);
inline void invert(uint16_t&amp; x);
inline void invert(uint32_t&amp; x);
inline void invert(uint64_t&amp; x);
// unconditional non-modifying reverse byte order copy
// unconditional non-modifying endianness reversing copy
inline void reorder(int16_t source, int16_t&amp; target);
inline void reorder(int32_t source, int32_t&amp; target);
inline void reorder(int64_t source, int64_t&amp; target);
inline void reorder(uint16_t source, uint16_t&amp; target);
inline void reorder(uint32_t source, uint32_t&amp; target);
inline void reorder(uint64_t source, uint64_t&amp; target);
inline void invert(int16_t source, int16_t&amp; target);
inline void invert(int32_t source, int32_t&amp; target);
inline void invert(int64_t source, int64_t&amp; target);
inline void invert(uint16_t source, uint16_t&amp; target);
inline void invert(uint32_t source, uint32_t&amp; target);
inline void invert(uint64_t source, uint64_t&amp; target);
// conditional modifying (i.e. in-place) endianness reversal
// conditional modifying (i.e. in-place) reverse byte order
template &lt;class T&gt; void native_to_big(T&amp; x);
template &lt;class T&gt; void native_to_little(T&amp; x);
template &lt;class T&gt; void big_to_native(T&amp; x);
template &lt;class T&gt; void little_to_native(T&amp; x);
// non-modifying copy conditionally reversing endianness;
// non-modifying conditional reverse byte order copy
template &lt;class T&gt; void native_to_big(T source, T&amp; target);
template &lt;class T&gt; void native_to_little(T source, T&amp; target);
@@ -83,21 +81,21 @@ namespace endian
} // namespace endian
} // namespace boost</pre>
<h3 dir="ltr">Members</h3>
<pre dir="ltr">inline void invert(int16_t&amp; x);
inline void invert(int32_t&amp; x);
inline void invert(int64_t&amp; x);
inline void invert(uint16_t&amp; x);
inline void invert(uint32_t&amp; x);
inline void invert(uint64_t&amp; x);</pre>
<pre dir="ltr">inline void reorder(int16_t&amp; x);
inline void reorder(int32_t&amp; x);
inline void reorder(int64_t&amp; x);
inline void reorder(uint16_t&amp; x);
inline void reorder(uint32_t&amp; x);
inline void reorder(uint64_t&amp; x);</pre>
<blockquote>
<p dir="ltr"><i>Effects:</i> Reverses the byte order of <i><code>x</code></i>.</p>
</blockquote>
<pre dir="ltr">inline void invert(int16_t source, int16_t&amp; target);
inline void invert(int32_t source, int32_t&amp; target);
inline void invert(int64_t source, int64_t&amp; target);
inline void invert(uint16_t source, uint16_t&amp; target);
inline void invert(uint32_t source, uint32_t&amp; target);
inline void invert(uint64_t source, uint64_t&amp; target);</pre>
<pre dir="ltr">inline void reorder(int16_t source, int16_t&amp; target);
inline void reorder(int32_t source, int32_t&amp; target);
inline void reorder(int64_t source, int64_t&amp; target);
inline void reorder(uint16_t source, uint16_t&amp; target);
inline void reorder(uint32_t source, uint32_t&amp; target);
inline void reorder(uint64_t source, uint64_t&amp; target);</pre>
<blockquote>
<p dir="ltr"><i>Effects:</i> Copies <code>source</code> to <code>target</code>,
reversing the byte order.</p>
@@ -108,7 +106,7 @@ template &lt;class T&gt; void big_to_native(T&amp; x);
template &lt;class T&gt; void little_to_native(T&amp; x);</pre>
<blockquote>
<p dir="ltr"><i>Effects:</i> If the native byte ordering and indicated byte
ordering are different, <code>invert(x)</code>, otherwise no effect.</p>
ordering are different, <code>reorder(x)</code>, otherwise no effect.</p>
</blockquote>
<pre dir="ltr">template &lt;class T&gt; void native_to_big(T source, T&amp; target);
template &lt;class T&gt; void native_to_little(T source, T&amp; target);
@@ -116,7 +114,7 @@ template &lt;class T&gt; void big_to_native(T source, T&amp; target);
template &lt;class T&gt; void little_to_native(T source, T&amp; target);</pre>
<blockquote>
<p dir="ltr"><i>Effects:</i> If the native byte ordering and indicated byte
ordering are different, <code>invert(source, target)</code>, otherwise <code>
ordering are different, <code>reorder(source, target)</code>, otherwise <code>
target = source</code>.</p>
</blockquote>
<h2><a name="Acknowledgements">Acknowledgements</a></h2>

View File

@@ -16,8 +16,8 @@
#include <iostream>
#include <cstdio>
#include <boost/endian/types.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <boost/detail/lightweight_main.hpp>
#include <boost/static_assert.hpp>
using namespace boost::endian;
@@ -41,14 +41,14 @@ namespace
int cpp_main(int, char * [])
{
BOOST_TEST_EQ( sizeof( header ), 16U ); // requirement for interoperability
BOOST_STATIC_ASSERT( sizeof( header ) == 16U ); // requirement for interoperability
header h;
h.file_code = 0x04030201;
h.file_code = 0x01020304;
h.file_length = sizeof( header );
h.version = -1;
h.shape_type = 0x04030201;
h.shape_type = 0x01020304;
// Low-level I/O such as POSIX read/write or <cstdio> fread/fwrite is sometimes
// used for binary file operations when ultimate efficiency is important.
@@ -74,5 +74,5 @@ int cpp_main(int, char * [])
std::cout << "created file " << filename << '\n';
return ::boost::report_errors();
return 0;
}

View File

@@ -19,127 +19,127 @@ namespace be = boost::endian;
namespace
{
void test_in_place_invert()
void test_in_place_reorder()
{
std::cout << "test_in_place_invert...\n";
std::cout << "test_in_place_reorder...\n";
boost::int64_t i64 = 0x0102030405060708LL;
be::invert(i64);
be::reorder(i64);
BOOST_TEST_EQ(i64, 0x0807060504030201LL);
be::invert(i64);
be::reorder(i64);
BOOST_TEST_EQ(i64, 0x0102030405060708LL);
i64 = 0xfefdfcfbfaf9f8f7LL;
be::invert(i64);
be::reorder(i64);
BOOST_TEST_EQ(i64, static_cast<boost::int64_t>(0xf7f8f9fafbfcfdfeULL));
be::invert(i64);
be::reorder(i64);
BOOST_TEST_EQ(i64, static_cast<boost::int64_t>(0xfefdfcfbfaf9f8f7ULL));
boost::int32_t i32 = 0x01020304;
be::invert(i32);
be::reorder(i32);
BOOST_TEST_EQ(i32, 0x04030201);
be::invert(i32);
be::reorder(i32);
BOOST_TEST_EQ(i32, 0x01020304);
i32 = 0xfefdfcfb;
be::invert(i32);
be::reorder(i32);
BOOST_TEST_EQ(i32, static_cast<boost::int32_t>(0xfbfcfdfe));
be::invert(i32);
be::reorder(i32);
BOOST_TEST_EQ(i32, static_cast<boost::int32_t>(0xfefdfcfb));
boost::int16_t i16 = 0x0102;
be::invert(i16);
be::reorder(i16);
BOOST_TEST_EQ(i16, 0x0201);
be::invert(i16);
be::reorder(i16);
BOOST_TEST_EQ(i16, 0x0102);
i16 = static_cast<boost::int16_t>(static_cast<boost::uint16_t>(0xfefd));
be::invert(i16);
be::reorder(i16);
BOOST_TEST_EQ(i16, static_cast<boost::int16_t>(static_cast<boost::uint16_t>(0xfdfe)));
be::invert(i16);
be::reorder(i16);
BOOST_TEST_EQ(i16, static_cast<boost::int16_t>(static_cast<boost::uint16_t>(0xfefd)));
boost::uint64_t ui64 = 0x0102030405060708ULL;
be::invert(ui64);
be::reorder(ui64);
BOOST_TEST_EQ(ui64, 0x0807060504030201ULL);
be::invert(ui64);
be::reorder(ui64);
BOOST_TEST_EQ(ui64, 0x0102030405060708ULL);
boost::uint32_t ui32 = 0x01020304;
be::invert(ui32);
be::reorder(ui32);
BOOST_TEST_EQ(ui32, static_cast<boost::uint32_t>(0x04030201));
be::invert(ui32);
be::reorder(ui32);
BOOST_TEST_EQ(ui32, static_cast<boost::uint32_t>(0x01020304));
boost::uint16_t ui16 = 0x0102;
be::invert(ui16);
be::reorder(ui16);
BOOST_TEST_EQ(ui16, 0x0201);
be::invert(ui16);
be::reorder(ui16);
BOOST_TEST_EQ(ui16, static_cast<boost::uint16_t>(0x0102));
std::cout << " test_in_place_invert complete\n";
std::cout << " test_in_place_reorder complete\n";
}
void test_copying_invert()
void test_copying_reorder()
{
std::cout << "test_copying_invert...\n";
std::cout << "test_copying_reorder...\n";
boost::int64_t i64 = 0x0102030405060708LL, j64, k64;
be::invert(i64, j64);
be::reorder(i64, j64);
BOOST_TEST_EQ(j64, 0x0807060504030201LL);
BOOST_TEST_EQ(i64, 0x0102030405060708LL);
be::invert(j64, k64);
be::reorder(j64, k64);
BOOST_TEST_EQ(k64, 0x0102030405060708LL);
i64 = 0xfefdfcfbfaf9f8f7LL;
be::invert(i64, j64);
be::reorder(i64, j64);
BOOST_TEST_EQ(j64, static_cast<boost::int64_t>(0xf7f8f9fafbfcfdfeLL));
be::invert(j64, k64);
be::reorder(j64, k64);
BOOST_TEST_EQ(k64, static_cast<boost::int64_t>(0xfefdfcfbfaf9f8f7LL));
boost::int32_t i32 = 0x01020304, j32, k32;
be::invert(i32, j32);
be::reorder(i32, j32);
BOOST_TEST_EQ(j32, 0x04030201);
be::invert(j32, k32);
be::reorder(j32, k32);
BOOST_TEST_EQ(k32, 0x01020304);
i32 = 0xfefdfcfb;
be::invert(i32, j32);
be::reorder(i32, j32);
BOOST_TEST_EQ(j32, static_cast<boost::int32_t>(0xfbfcfdfe));
be::invert(j32, k32);
be::reorder(j32, k32);
BOOST_TEST_EQ(k32, static_cast<boost::int32_t>(0xfefdfcfb));
boost::int16_t i16 = 0x0102, j16, k16;
be::invert(i16, j16);
be::reorder(i16, j16);
BOOST_TEST_EQ(j16, 0x0201);
be::invert(j16, k16);
be::reorder(j16, k16);
BOOST_TEST_EQ(k16, 0x0102);
i16 = static_cast<boost::int16_t>(static_cast<boost::uint16_t>(0xfefd));
be::invert(i16, j16);
be::reorder(i16, j16);
BOOST_TEST_EQ(j16, static_cast<boost::int16_t>(static_cast<boost::uint16_t>(0xfdfe)));
be::invert(j16, k16);
be::reorder(j16, k16);
BOOST_TEST_EQ(k16, static_cast<boost::int16_t>(static_cast<boost::uint16_t>(0xfefd)));
boost::uint64_t ui64 = 0x0102030405060708ULL, uj64, uk64;
be::invert(ui64, uj64);
be::reorder(ui64, uj64);
BOOST_TEST_EQ(uj64, 0x0807060504030201ULL);
be::invert(uj64, uk64);
be::reorder(uj64, uk64);
BOOST_TEST_EQ(uk64, 0x0102030405060708ULL);
boost::uint32_t ui32 = 0x01020304, uj32, uk32;
be::invert(ui32, uj32);
be::reorder(ui32, uj32);
BOOST_TEST_EQ(uj32, static_cast<boost::uint32_t>(0x04030201));
be::invert(uj32, uk32);
be::reorder(uj32, uk32);
BOOST_TEST_EQ(uk32, static_cast<boost::uint32_t>(0x01020304));
boost::uint16_t ui16 = 0x0102, uj16, uk16;
be::invert(ui16, uj16);
be::reorder(ui16, uj16);
BOOST_TEST_EQ(uj16, 0x0201);
be::invert(uj16, uk16);
be::reorder(uj16, uk16);
BOOST_TEST_EQ(uk16, 0x0102);
std::cout << " test_copying_invert complete\n";
std::cout << " test_copying_reorder complete\n";
}
const boost::int64_t ni64 = 0x0102030405060708LL;
@@ -196,9 +196,9 @@ namespace
const boost::uint16_t lui16 = 0x0102;
# endif
void test_in_place_conditional_invert()
void test_in_place_conditional_reorder()
{
std::cout << "test_in_place_conditional_invert...\n";
std::cout << "test_in_place_conditional_reorder...\n";
boost::int64_t i64;
@@ -308,12 +308,12 @@ namespace
be::little_to_native(ui16);
BOOST_TEST_EQ(ui16, nui16);
std::cout << " test_in_place_conditional_invert complete\n";
std::cout << " test_in_place_conditional_reorder complete\n";
}
void test_copying_conditional_invert()
void test_copying_conditional_reorder()
{
std::cout << "test_copying_conditional_invert...\n";
std::cout << "test_copying_conditional_reorder...\n";
boost::int64_t i64, ti64;
@@ -423,7 +423,7 @@ namespace
be::little_to_native(ui16, tui16);
BOOST_TEST_EQ(tui16, nui16);
std::cout << " test_copying_conditional_invert complete\n";
std::cout << " test_copying_conditional_reorder complete\n";
}
} // unnamed namespace
@@ -431,10 +431,10 @@ namespace
int cpp_main(int, char * [])
{
std::cerr << std::hex;
test_in_place_invert();
test_copying_invert();
test_in_place_conditional_invert();
test_copying_conditional_invert();
test_in_place_reorder();
test_copying_reorder();
test_in_place_conditional_reorder();
test_copying_conditional_reorder();
return ::boost::report_errors();
}