diff --git a/include/boost/endian/detail/endian_store.hpp b/include/boost/endian/detail/endian_store.hpp index d8fa511..06b4d90 100644 --- a/include/boost/endian/detail/endian_store.hpp +++ b/include/boost/endian/detail/endian_store.hpp @@ -74,6 +74,92 @@ template 1 + +template struct endian_store_impl +{ + inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT + { + BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); + + unsigned char tmp[ 2 ]; + boost::endian::endian_store( tmp, v ); + + p[0] = tmp[0]; + } +}; + +template struct endian_store_impl +{ + inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT + { + BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); + + unsigned char tmp[ 2 ]; + boost::endian::endian_store( tmp, v ); + + p[0] = tmp[1]; + } +}; + +// truncating store 4 -> 1 + +template struct endian_store_impl +{ + inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT + { + BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); + + unsigned char tmp[ 4 ]; + boost::endian::endian_store( tmp, v ); + + p[0] = tmp[0]; + } +}; + +template struct endian_store_impl +{ + inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT + { + BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); + + unsigned char tmp[ 4 ]; + boost::endian::endian_store( tmp, v ); + + p[0] = tmp[3]; + } +}; + +// truncating store 4 -> 2 + +template struct endian_store_impl +{ + inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT + { + BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); + + unsigned char tmp[ 4 ]; + boost::endian::endian_store( tmp, v ); + + p[0] = tmp[0]; + p[1] = tmp[1]; + } +}; + +template struct endian_store_impl +{ + inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT + { + BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); + + unsigned char tmp[ 4 ]; + boost::endian::endian_store( tmp, v ); + + p[0] = tmp[2]; + p[1] = tmp[3]; + } +}; + // truncating store 4 -> 3 template struct endian_store_impl @@ -106,6 +192,130 @@ template struct endian_store_impl 1 + +template struct endian_store_impl +{ + inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT + { + BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); + + unsigned char tmp[ 8 ]; + boost::endian::endian_store( tmp, v ); + + p[0] = tmp[0]; + } +}; + +template struct endian_store_impl +{ + inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT + { + BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); + + unsigned char tmp[ 8 ]; + boost::endian::endian_store( tmp, v ); + + p[0] = tmp[7]; + } +}; + +// truncating store 8 -> 2 + +template struct endian_store_impl +{ + inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT + { + BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); + + unsigned char tmp[ 8 ]; + boost::endian::endian_store( tmp, v ); + + p[0] = tmp[0]; + p[1] = tmp[1]; + } +}; + +template struct endian_store_impl +{ + inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT + { + BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); + + unsigned char tmp[ 8 ]; + boost::endian::endian_store( tmp, v ); + + p[0] = tmp[6]; + p[1] = tmp[7]; + } +}; + +// truncating store 8 -> 3 + +template struct endian_store_impl +{ + inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT + { + BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); + + unsigned char tmp[ 8 ]; + boost::endian::endian_store( tmp, v ); + + p[0] = tmp[0]; + p[1] = tmp[1]; + p[2] = tmp[2]; + } +}; + +template struct endian_store_impl +{ + inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT + { + BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); + + unsigned char tmp[ 8 ]; + boost::endian::endian_store( tmp, v ); + + p[0] = tmp[5]; + p[1] = tmp[6]; + p[2] = tmp[7]; + } +}; + +// truncating store 8 -> 4 + +template struct endian_store_impl +{ + inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT + { + BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); + + unsigned char tmp[ 8 ]; + boost::endian::endian_store( tmp, v ); + + p[0] = tmp[0]; + p[1] = tmp[1]; + p[2] = tmp[2]; + p[3] = tmp[3]; + } +}; + +template struct endian_store_impl +{ + inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT + { + BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); + + unsigned char tmp[ 8 ]; + boost::endian::endian_store( tmp, v ); + + p[0] = tmp[4]; + p[1] = tmp[5]; + p[2] = tmp[6]; + p[3] = tmp[7]; + } +}; + // truncating store 8 -> 5 template struct endian_store_impl diff --git a/test/endian_store_test.cpp b/test/endian_store_test.cpp index e3ce1d8..77b8a9d 100644 --- a/test/endian_store_test.cpp +++ b/test/endian_store_test.cpp @@ -59,343 +59,252 @@ public: } }; +template void test_1() +{ + { + unsigned char v[] = { 0xAA, 0xAA }; + + boost::endian::endian_store( v, 0x01 ); + + unsigned char w[] = { 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA }; + + boost::endian::endian_store( v, 0x01 ); + + unsigned char w[] = { 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } +} + +template void test_2() +{ + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( v, 0x0102 ); + + unsigned char w[] = { 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( v, 0x0102 ); + + unsigned char w[] = { 0x01, 0x02, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } +} + +template void test_3() +{ + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( v, 0x010203 ); + + unsigned char w[] = { 0x03, 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( v, 0x010203 ); + + unsigned char w[] = { 0x01, 0x02, 0x03, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } +} + +template void test_4() +{ + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( v, 0x01020304 ); + + unsigned char w[] = { 0x04, 0x03, 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( v, 0x01020304 ); + + unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } +} + +template void test_5() +{ + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( v, 0x0102030405 ); + + unsigned char w[] = { 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( v, 0x0102030405 ); + + unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } +} + +template void test_6() +{ + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( v, 0x010203040506 ); + + unsigned char w[] = { 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( v, 0x010203040506 ); + + unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } +} + +template void test_7() +{ + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( v, 0x01020304050607 ); + + unsigned char w[] = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( v, 0x01020304050607 ); + + unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } +} + +template void test_8() +{ + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( v, 0x0102030405060708 ); + + unsigned char w[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( v, 0x0102030405060708 ); + + unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } +} + int main() { // 1 - { - unsigned char v[] = { 0xAA, 0xAA }; + test_1(); + test_1(); - boost::endian::endian_store( v, 0x01 ); + test_1(); + test_1(); - unsigned char w[] = { 0x01, 0xAA }; + test_1(); + test_1(); - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x01 ); - - unsigned char w[] = { 0x01, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x01 ); - - unsigned char w[] = { 0x01, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x01 ); - - unsigned char w[] = { 0x01, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } + test_1(); + test_1(); // 2 - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA }; + test_2(); + test_2(); - boost::endian::endian_store( v, 0x0102 ); + test_2(); + test_2(); - unsigned char w[] = { 0x02, 0x01, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x0102 ); - - unsigned char w[] = { 0x02, 0x01, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x0102 ); - - unsigned char w[] = { 0x01, 0x02, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x0102 ); - - unsigned char w[] = { 0x01, 0x02, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } + test_2(); + test_2(); // 3 - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA }; + test_3(); + test_3(); - boost::endian::endian_store( v, 0x010203 ); - - unsigned char w[] = { 0x03, 0x02, 0x01, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x010203 ); - - unsigned char w[] = { 0x03, 0x02, 0x01, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x010203 ); - - unsigned char w[] = { 0x01, 0x02, 0x03, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x010203 ); - - unsigned char w[] = { 0x01, 0x02, 0x03, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } + test_3(); + test_3(); // 4 - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + test_4(); + test_4(); - boost::endian::endian_store( v, 0x01020304 ); - - unsigned char w[] = { 0x04, 0x03, 0x02, 0x01, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x01020304 ); - - unsigned char w[] = { 0x04, 0x03, 0x02, 0x01, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x01020304 ); - - unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x01020304 ); - - unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } + test_4(); + test_4(); // 5 - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x0102030405 ); - - unsigned char w[] = { 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x0102030405 ); - - unsigned char w[] = { 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x0102030405 ); - - unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x0102030405 ); - - unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } + test_5(); + test_5(); // 6 - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x010203040506 ); - - unsigned char w[] = { 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x010203040506 ); - - unsigned char w[] = { 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x010203040506 ); - - unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x010203040506 ); - - unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } + test_6(); + test_6(); // 7 - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x01020304050607 ); - - unsigned char w[] = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x01020304050607 ); - - unsigned char w[] = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x01020304050607 ); - - unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x01020304050607 ); - - unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } + test_7(); + test_7(); // 8 - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x0102030405060708 ); - - unsigned char w[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x0102030405060708 ); - - unsigned char w[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x0102030405060708 ); - - unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } - - { - unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; - - boost::endian::endian_store( v, 0x0102030405060708 ); - - unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0xAA }; - - BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); - } + test_8(); + test_8(); return boost::report_errors(); }