Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r61692 - in sandbox/hash: boost/hash boost/hash/detail libs/hash/test
From: me22.ca+boost_at_[hidden]
Date: 2010-04-29 21:59:39


Author: smcmurray
Date: 2010-04-29 21:59:39 EDT (Thu, 29 Apr 2010)
New Revision: 61692
URL: http://svn.boost.org/trac/boost/changeset/61692

Log:
hash: remove literals from exploders and imploders by dealing in parametrized endian types instead of specifically octet-oriented ones
Text files modified:
   sandbox/hash/boost/hash/detail/exploder.hpp | 97 +++++++++++++++++++++------------------
   sandbox/hash/boost/hash/detail/imploder.hpp | 97 +++++++++++++++++++++------------------
   sandbox/hash/boost/hash/stream_endian.hpp | 10 ++--
   sandbox/hash/libs/hash/test/pack.cpp | 8 +-
   4 files changed, 113 insertions(+), 99 deletions(-)

Modified: sandbox/hash/boost/hash/detail/exploder.hpp
==============================================================================
--- sandbox/hash/boost/hash/detail/exploder.hpp (original)
+++ sandbox/hash/boost/hash/detail/exploder.hpp 2010-04-29 21:59:39 EDT (Thu, 29 Apr 2010)
@@ -20,15 +20,22 @@
 namespace hash {
 namespace detail {
 
+// By definition, for all exploders, InputBits > OutputBits,
+// so we're taking one value and splitting it into many smaller values
+
 template <typename Endianness,
           int InputBits, int OutputBits,
           int k = 0>
 struct exploder;
 
-template <int InputBits, int OutputBits,
+template <int UnitBits, int InputBits, int OutputBits,
           int k>
-struct exploder<stream_endian::big_octet_big_bit,
+struct exploder<stream_endian::big_unit_big_bit<UnitBits>,
                 InputBits, OutputBits, k> {
+ // If this fires, you probably want stream_endian::big_bit instead
+ // of big_byte_big_bit or big_octet_big_bit
+ BOOST_STATIC_ASSERT(!(InputBits % UnitBits && UnitBits % InputBits ) &&
+ !(OutputBits % UnitBits && UnitBits % OutputBits));
     template <typename OutputValue, typename InputValue>
     static void step(OutputValue &z, InputValue x) {
         InputValue y = unbounded_shr<InputBits - (OutputBits+k)>(x);
@@ -37,90 +44,90 @@
     template <typename OutputType, typename InputValue>
     static void explode1_array(OutputType &out, unsigned &i, InputValue x) {
         step(out[i++], x);
- exploder<stream_endian::big_octet_big_bit,
+ exploder<stream_endian::big_unit_big_bit<UnitBits>,
                 InputBits, OutputBits, k+OutputBits>
          ::explode1_array(out, i, x);
     }
 };
-template <int InputBits, int OutputBits>
-struct exploder<stream_endian::big_octet_big_bit,
+template <int UnitBits, int InputBits, int OutputBits>
+struct exploder<stream_endian::big_unit_big_bit<UnitBits>,
                 InputBits, OutputBits, InputBits> {
     template <typename OutputType, typename IntputValue>
     static void explode1_array(OutputType &, unsigned &, IntputValue) {}
 };
 
-template <int InputBits, int OutputBits,
+template <int UnitBits, int InputBits, int OutputBits,
           int k>
-struct exploder<stream_endian::little_octet_big_bit,
+struct exploder<stream_endian::little_unit_big_bit<UnitBits>,
                 InputBits, OutputBits, k> {
- // Mixed-endian pack explode can only handle bitwidths that are
- // multiples or fractions of octets
- BOOST_STATIC_ASSERT((InputBits % 8 == 0 || 8 % InputBits == 0) &&
- (OutputBits % 8 == 0 || 8 % OutputBits == 0));
+ BOOST_STATIC_ASSERT(!((InputBits % UnitBits) && (UnitBits % InputBits )) &&
+ !((OutputBits % UnitBits) && (UnitBits % OutputBits)));
     template <typename OutputValue, typename InputValue>
     static void step(OutputValue &z, InputValue x) {
- int const kb = (k % 8);
- int const kB = k - kb;
+ int const kb = (k % UnitBits);
+ int const ku = k - kb;
         int const shift =
- OutputBits >= 8 ? k :
- InputBits >= 8 ? kB + (8-(OutputBits+kb)) :
- InputBits - (OutputBits+kb);
+ OutputBits >= UnitBits ? k :
+ InputBits >= UnitBits ? ku + (UnitBits-(OutputBits+kb)) :
+ InputBits - (OutputBits+kb);
         InputValue y = unbounded_shr<shift>(x);
         z = OutputValue(low_bits<OutputBits>(y));
     }
     template <typename OutputType, typename InputValue>
     static void explode1_array(OutputType &out, unsigned &i, InputValue x) {
         step(out[i++], x);
- exploder<stream_endian::little_octet_big_bit,
+ exploder<stream_endian::little_unit_big_bit<UnitBits>,
                 InputBits, OutputBits, k+OutputBits>
          ::explode1_array(out, i, x);
     }
 };
-template <int InputBits, int OutputBits>
-struct exploder<stream_endian::little_octet_big_bit,
+template <int UnitBits, int InputBits, int OutputBits>
+struct exploder<stream_endian::little_unit_big_bit<UnitBits>,
                 InputBits, OutputBits, InputBits> {
     template <typename OutputType, typename IntputValue>
     static void explode1_array(OutputType &, unsigned &, IntputValue) {}
 };
 
-template <int InputBits, int OutputBits,
+template <int UnitBits, int InputBits, int OutputBits,
           int k>
-struct exploder<stream_endian::big_octet_little_bit,
+struct exploder<stream_endian::big_unit_little_bit<UnitBits>,
                 InputBits, OutputBits, k> {
- // Mixed-endian pack explode can only handle bitwidths that are
- // multiples or fractions of octets
- BOOST_STATIC_ASSERT((InputBits % 8 == 0 || 8 % InputBits == 0) &&
- (OutputBits % 8 == 0 || 8 % OutputBits == 0));
+ BOOST_STATIC_ASSERT(!((InputBits % UnitBits) && (UnitBits % InputBits )) &&
+ !((OutputBits % UnitBits) && (UnitBits % OutputBits)));
     template <typename OutputValue, typename InputValue>
     static void step(OutputValue &z, InputValue x) {
- int const kb = (k % 8);
- int const kB = k - kb;
+ int const kb = (k % UnitBits);
+ int const ku = k - kb;
         int const shift =
- OutputBits >= 8 ? InputBits - (OutputBits+k) :
- InputBits >= 8 ? InputBits - (8+kB) + kb :
- kb;
+ OutputBits >= UnitBits ? InputBits - (OutputBits+k) :
+ InputBits >= UnitBits ? InputBits - (UnitBits+ku) + kb :
+ kb;
         InputValue y = unbounded_shr<shift>(x);
         z = OutputValue(low_bits<OutputBits>(y));
     }
     template <typename OutputType, typename InputValue>
     static void explode1_array(OutputType &out, unsigned &i, InputValue x) {
         step(out[i++], x);
- exploder<stream_endian::big_octet_little_bit,
+ exploder<stream_endian::big_unit_little_bit<UnitBits>,
                 InputBits, OutputBits, k+OutputBits>
          ::explode1_array(out, i, x);
     }
 };
-template <int InputBits, int OutputBits>
-struct exploder<stream_endian::big_octet_little_bit,
+template <int UnitBits, int InputBits, int OutputBits>
+struct exploder<stream_endian::big_unit_little_bit<UnitBits>,
                 InputBits, OutputBits, InputBits> {
     template <typename OutputType, typename IntputValue>
     static void explode1_array(OutputType &, unsigned &, IntputValue) {}
 };
 
-template <int InputBits, int OutputBits,
+template <int UnitBits, int InputBits, int OutputBits,
           int k>
-struct exploder<stream_endian::little_octet_little_bit,
+struct exploder<stream_endian::little_unit_little_bit<UnitBits>,
                 InputBits, OutputBits, k> {
+ // If this fires, you probably want stream_endian::little_bit instead
+ // of little_byte_little_bit or little_octet_little_bit
+ BOOST_STATIC_ASSERT(!((InputBits % UnitBits) && (UnitBits % InputBits )) &&
+ !((OutputBits % UnitBits) && (UnitBits % OutputBits)));
     template <typename OutputValue, typename InputValue>
     static void step(OutputValue &z, InputValue x) {
         InputValue y = unbounded_shr<k>(x);
@@ -129,24 +136,24 @@
     template <typename OutputType, typename InputValue>
     static void explode1_array(OutputType &out, unsigned &i, InputValue x) {
         step(out[i++], x);
- exploder<stream_endian::little_octet_little_bit,
+ exploder<stream_endian::little_unit_little_bit<UnitBits>,
                 InputBits, OutputBits, k+OutputBits>
          ::explode1_array(out, i, x);
     }
 };
-template <int InputBits, int OutputBits>
-struct exploder<stream_endian::little_octet_little_bit,
+template <int UnitBits, int InputBits, int OutputBits>
+struct exploder<stream_endian::little_unit_little_bit<UnitBits>,
                 InputBits, OutputBits, InputBits> {
     template <typename OutputType, typename IntputValue>
     static void explode1_array(OutputType &, unsigned &, IntputValue) {}
 };
 
-template <int InputBits, int OutputBits,
+template <int UnitBits, int InputBits, int OutputBits,
           int k>
-struct exploder<stream_endian::host_byte,
+struct exploder<stream_endian::host_unit<UnitBits>,
                 InputBits, OutputBits, k> {
- BOOST_STATIC_ASSERT(InputBits % CHAR_BIT == 0);
- BOOST_STATIC_ASSERT(OutputBits % CHAR_BIT == 0);
+ BOOST_STATIC_ASSERT(!(InputBits % UnitBits) &&
+ !(OutputBits % UnitBits));
     template <typename OutputValue, typename InputValue>
     static void step(OutputValue &z, InputValue x) {
         std::memcpy(&z, (char*)&x + k/CHAR_BIT, OutputBits/CHAR_BIT);
@@ -154,13 +161,13 @@
     template <typename OutputType, typename InputValue>
     static void explode1_array(OutputType &out, unsigned &i, InputValue x) {
         step(out[i++], x);
- exploder<stream_endian::host_byte,
+ exploder<stream_endian::host_unit<UnitBits>,
                 InputBits, OutputBits, k+OutputBits>
          ::explode1_array(out, i, x);
     }
 };
-template <int InputBits, int OutputBits>
-struct exploder<stream_endian::host_byte,
+template <int UnitBits, int InputBits, int OutputBits>
+struct exploder<stream_endian::host_unit<UnitBits>,
                 InputBits, OutputBits, InputBits> {
     template <typename OutputType, typename IntputValue>
     static void explode1_array(OutputType &, unsigned &, IntputValue) {}

Modified: sandbox/hash/boost/hash/detail/imploder.hpp
==============================================================================
--- sandbox/hash/boost/hash/detail/imploder.hpp (original)
+++ sandbox/hash/boost/hash/detail/imploder.hpp 2010-04-29 21:59:39 EDT (Thu, 29 Apr 2010)
@@ -20,15 +20,22 @@
 namespace hash {
 namespace detail {
 
+// By definition, for all imploders, InputBits < OutputBits,
+// so we're taking many smaller values and combining them into one value
+
 template <typename Endianness,
           int InputBits, int OutputBits,
           int k = 0>
 struct imploder;
 
-template <int InputBits, int OutputBits,
+template <int UnitBits, int InputBits, int OutputBits,
           int k>
-struct imploder<stream_endian::big_octet_big_bit,
+struct imploder<stream_endian::big_unit_big_bit<UnitBits>,
                 InputBits, OutputBits, k> {
+ // If this fires, you probably want stream_endian::big_bit instead
+ // of big_byte_big_bit or big_octet_big_bit
+ BOOST_STATIC_ASSERT(!(InputBits % UnitBits && UnitBits % InputBits ) &&
+ !(OutputBits % UnitBits && UnitBits % OutputBits));
     template <typename InputValue, typename OutputValue>
     static void step(InputValue z, OutputValue &x) {
         OutputValue y = low_bits<OutputBits>(OutputValue(z));
@@ -37,90 +44,90 @@
     template <typename InputType, typename OutputValue>
     static void implode1_array(InputType const &in, unsigned &i, OutputValue &x) {
         step(in[i++], x);
- imploder<stream_endian::big_octet_big_bit,
+ imploder<stream_endian::big_unit_big_bit<UnitBits>,
                 InputBits, OutputBits, k+InputBits>
          ::implode1_array(in, i, x);
     }
 };
-template <int InputBits, int OutputBits>
-struct imploder<stream_endian::big_octet_big_bit,
+template <int UnitBits, int InputBits, int OutputBits>
+struct imploder<stream_endian::big_unit_big_bit<UnitBits>,
                 InputBits, OutputBits, OutputBits> {
     template <typename InputType, typename OutputValue>
     static void implode1_array(InputType const &, unsigned &, OutputValue &) {}
 };
 
-template <int InputBits, int OutputBits,
+template <int UnitBits, int InputBits, int OutputBits,
           int k>
-struct imploder<stream_endian::little_octet_big_bit,
+struct imploder<stream_endian::little_unit_big_bit<UnitBits>,
                 InputBits, OutputBits, k> {
- // Mixed-endian pack explode can only handle bitwidths that are
- // multiples or fractions of octets
- BOOST_STATIC_ASSERT((InputBits % 8 == 0 || 8 % InputBits == 0) &&
- (OutputBits % 8 == 0 || 8 % OutputBits == 0));
+ BOOST_STATIC_ASSERT(!((InputBits % UnitBits) && (UnitBits % InputBits )) &&
+ !((OutputBits % UnitBits) && (UnitBits % OutputBits)));
     template <typename InputValue, typename OutputValue>
     static void step(InputValue z, OutputValue &x) {
         OutputValue y = low_bits<OutputBits>(OutputValue(z));
- int const kb = (k % 8);
- int const kB = k - kb;
+ int const kb = (k % UnitBits);
+ int const ku = k - kb;
         int const shift =
- InputBits >= 8 ? k :
- OutputBits >= 8 ? kB + (8-(InputBits+kb)) :
- OutputBits - (InputBits+kb);
+ InputBits >= UnitBits ? k :
+ OutputBits >= UnitBits ? ku + (UnitBits-(InputBits+kb)) :
+ OutputBits - (InputBits+kb);
         x |= unbounded_shl<shift>(y);
     }
     template <typename InputType, typename OutputValue>
     static void implode1_array(InputType const &in, unsigned &i, OutputValue &x) {
         step(in[i++], x);
- imploder<stream_endian::little_octet_big_bit,
+ imploder<stream_endian::little_unit_big_bit<UnitBits>,
                 InputBits, OutputBits, k+InputBits>
          ::implode1_array(in, i, x);
     }
 };
-template <int InputBits, int OutputBits>
-struct imploder<stream_endian::little_octet_big_bit,
+template <int UnitBits, int InputBits, int OutputBits>
+struct imploder<stream_endian::little_unit_big_bit<UnitBits>,
                 InputBits, OutputBits, OutputBits> {
     template <typename InputType, typename OutputValue>
     static void implode1_array(InputType const &, unsigned &, OutputValue &) {}
 };
 
-template <int InputBits, int OutputBits,
+template <int UnitBits, int InputBits, int OutputBits,
           int k>
-struct imploder<stream_endian::big_octet_little_bit,
+struct imploder<stream_endian::big_unit_little_bit<UnitBits>,
                 InputBits, OutputBits, k> {
- // Mixed-endian pack explode can only handle bitwidths that are
- // multiples or fractions of octets
- BOOST_STATIC_ASSERT((InputBits % 8 == 0 || 8 % InputBits == 0) &&
- (OutputBits % 8 == 0 || 8 % OutputBits == 0));
+ BOOST_STATIC_ASSERT(!((InputBits % UnitBits) && (UnitBits % InputBits )) &&
+ !((OutputBits % UnitBits) && (UnitBits % OutputBits)));
     template <typename InputValue, typename OutputValue>
     static void step(InputValue z, OutputValue &x) {
         OutputValue y = low_bits<OutputBits>(OutputValue(z));
- int const kb = (k % 8);
- int const kB = k - kb;
+ int const kb = (k % UnitBits);
+ int const ku = k - kb;
         int const shift =
- InputBits >= 8 ? OutputBits - (InputBits+k) :
- OutputBits >= 8 ? OutputBits - (8+kB) + kb :
- kb;
+ InputBits >= UnitBits ? OutputBits - (InputBits+k) :
+ OutputBits >= UnitBits ? OutputBits - (UnitBits+ku) + kb :
+ kb;
         x |= unbounded_shl<shift>(y);
     }
     template <typename InputType, typename OutputValue>
     static void implode1_array(InputType const &in, unsigned &i, OutputValue &x) {
         step(in[i++], x);
- imploder<stream_endian::big_octet_little_bit,
+ imploder<stream_endian::big_unit_little_bit<UnitBits>,
                 InputBits, OutputBits, k+InputBits>
          ::implode1_array(in, i, x);
     }
 };
-template <int InputBits, int OutputBits>
-struct imploder<stream_endian::big_octet_little_bit,
+template <int UnitBits, int InputBits, int OutputBits>
+struct imploder<stream_endian::big_unit_little_bit<UnitBits>,
                 InputBits, OutputBits, OutputBits> {
     template <typename InputType, typename OutputValue>
     static void implode1_array(InputType const &, unsigned &, OutputValue &) {}
 };
 
-template <int InputBits, int OutputBits,
+template <int UnitBits, int InputBits, int OutputBits,
           int k>
-struct imploder<stream_endian::little_octet_little_bit,
+struct imploder<stream_endian::little_unit_little_bit<UnitBits>,
                 InputBits, OutputBits, k> {
+ // If this fires, you probably want stream_endian::little_bit instead
+ // of little_byte_little_bit or little_octet_little_bit
+ BOOST_STATIC_ASSERT(!((InputBits % UnitBits) && (UnitBits % InputBits )) &&
+ !((OutputBits % UnitBits) && (UnitBits % OutputBits)));
     template <typename InputValue, typename OutputValue>
     static void step(InputValue z, OutputValue &x) {
         OutputValue y = low_bits<OutputBits>(OutputValue(z));
@@ -129,24 +136,24 @@
     template <typename InputType, typename OutputValue>
     static void implode1_array(InputType const &in, unsigned &i, OutputValue &x) {
         step(in[i++], x);
- imploder<stream_endian::little_octet_little_bit,
+ imploder<stream_endian::little_unit_little_bit<UnitBits>,
                 InputBits, OutputBits, k+InputBits>
          ::implode1_array(in, i, x);
     }
 };
-template <int InputBits, int OutputBits>
-struct imploder<stream_endian::little_octet_little_bit,
+template <int UnitBits, int InputBits, int OutputBits>
+struct imploder<stream_endian::little_unit_little_bit<UnitBits>,
                 InputBits, OutputBits, OutputBits> {
     template <typename InputType, typename OutputValue>
     static void implode1_array(InputType const &, unsigned &, OutputValue &) {}
 };
 
-template <int InputBits, int OutputBits,
+template <int UnitBits, int InputBits, int OutputBits,
           int k>
-struct imploder<stream_endian::host_byte,
+struct imploder<stream_endian::host_unit<UnitBits>,
                 InputBits, OutputBits, k> {
- BOOST_STATIC_ASSERT(InputBits % CHAR_BIT == 0);
- BOOST_STATIC_ASSERT(OutputBits % CHAR_BIT == 0);
+ BOOST_STATIC_ASSERT(!(InputBits % UnitBits) &&
+ !(OutputBits % UnitBits));
     template <typename InputValue, typename OutputValue>
     static void step(InputValue z, OutputValue &x) {
         std::memcpy((char*)&x + k/CHAR_BIT, &z, InputBits/CHAR_BIT);
@@ -154,13 +161,13 @@
     template <typename InputType, typename OutputValue>
     static void implode1_array(InputType const &in, unsigned &i, OutputValue &x) {
         step(in[i++], x);
- imploder<stream_endian::host_byte,
+ imploder<stream_endian::host_unit<UnitBits>,
                 InputBits, OutputBits, k+InputBits>
          ::implode1_array(in, i, x);
     }
 };
-template <int InputBits, int OutputBits>
-struct imploder<stream_endian::host_byte,
+template <int UnitBits, int InputBits, int OutputBits>
+struct imploder<stream_endian::host_unit<UnitBits>,
                 InputBits, OutputBits, OutputBits> {
     template <typename InputType, typename OutputValue>
     static void implode1_array(InputType const &, unsigned &, OutputValue &) {}

Modified: sandbox/hash/boost/hash/stream_endian.hpp
==============================================================================
--- sandbox/hash/boost/hash/stream_endian.hpp (original)
+++ sandbox/hash/boost/hash/stream_endian.hpp 2010-04-29 21:59:39 EDT (Thu, 29 Apr 2010)
@@ -20,15 +20,15 @@
 
     // General versions; There should be no need to use these directly
 
- template <unsigned UnitBits>
+ template <int UnitBits>
     struct big_unit_big_bit {};
- template <unsigned UnitBits>
+ template <int UnitBits>
     struct little_unit_little_bit {};
- template <unsigned UnitBits>
+ template <int UnitBits>
     struct big_unit_little_bit {};
- template <unsigned UnitBits>
+ template <int UnitBits>
     struct little_unit_big_bit {};
- template <unsigned UnitBits>
+ template <int UnitBits>
     struct host_unit { BOOST_STATIC_ASSERT(UnitBits % CHAR_BIT == 0); };
 
     // Typical, useful instantiations

Modified: sandbox/hash/libs/hash/test/pack.cpp
==============================================================================
--- sandbox/hash/libs/hash/test/pack.cpp (original)
+++ sandbox/hash/libs/hash/test/pack.cpp 2010-04-29 21:59:39 EDT (Thu, 29 Apr 2010)
@@ -88,7 +88,7 @@
     {
     array<uint16_t, 1> in = {{(31 << 10) | (17 << 5) | (4 << 0)}};
     array<uint8_t, 3> out;
- pack<big_octet_big_bit, 15, 5>(in, out);
+ pack<big_bit, 15, 5>(in, out);
     for (unsigned i = 0; i < out.size(); ++i) printf("%.2x ", (int)out[i]);
     printf("\n");
     array<uint8_t, 3> eout = {{31, 17, 4}};
@@ -329,7 +329,7 @@
     {
     array<uint16_t, 1> in = {{(31 << 10) | (17 << 5) | (4 << 0)}};
     array<uint8_t, 3> out;
- pack<little_octet_little_bit, 15, 5>(in, out);
+ pack<little_bit, 15, 5>(in, out);
     for (unsigned i = 0; i < out.size(); ++i) printf("%.2x ", (int)out[i]);
     printf("\n");
     array<uint8_t, 3> eout = {{4, 17, 31}};
@@ -416,7 +416,7 @@
     {
     array<uint8_t, 3> in = {{31, 17, 4}};
     array<uint16_t, 1> out;
- pack<big_octet_big_bit, 5, 15>(in, out);
+ pack<big_bit, 5, 15>(in, out);
     for (unsigned i = 0; i < out.size(); ++i) printf("%.4x ", (int)out[i]);
     printf("\n");
     array<uint16_t, 1> eout = {{(31 << 10) | (17 << 5) | (4 << 0)}};
@@ -657,7 +657,7 @@
     {
     array<uint8_t, 3> in = {{4, 17, 31}};
     array<uint16_t, 1> out;
- pack<little_octet_little_bit, 5, 15>(in, out);
+ pack<little_bit, 5, 15>(in, out);
     for (unsigned i = 0; i < out.size(); ++i) printf("%.4x ", (int)out[i]);
     printf("\n");
     array<uint16_t, 1> eout = {{(31 << 10) | (17 << 5) | (4 << 0)}};


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk