Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r62924 - sandbox/SOC/2010/bits_and_ints/boost/integer
From: muriloufg_at_[hidden]
Date: 2010-06-13 19:36:06


Author: murilov
Date: 2010-06-13 19:36:03 EDT (Sun, 13 Jun 2010)
New Revision: 62924
URL: http://svn.boost.org/trac/boost/changeset/62924

Log:
Added overload in interleave function for accepting 8-bit (returning 16-bit) and 16-bit (returning 32-bit) integers as parameter.
Text files modified:
   sandbox/SOC/2010/bits_and_ints/boost/integer/interleave.hpp | 61 ++++++++++++++++++++++++++++++---------
   1 files changed, 47 insertions(+), 14 deletions(-)

Modified: sandbox/SOC/2010/bits_and_ints/boost/integer/interleave.hpp
==============================================================================
--- sandbox/SOC/2010/bits_and_ints/boost/integer/interleave.hpp (original)
+++ sandbox/SOC/2010/bits_and_ints/boost/integer/interleave.hpp 2010-06-13 19:36:03 EDT (Sun, 13 Jun 2010)
@@ -12,6 +12,8 @@
 
 #include <boost/utility/enable_if.hpp>
 #include <boost/type_traits/is_integral.hpp>
+#include <boost/cstdint.hpp>
+
 
 /*
  * Bit interleaving functions.
@@ -22,7 +24,7 @@
  * b = 10; // 0000 1010
  * z = interleave(a, b); // z = 102
  *
- * a = 0 0 0 0 0 1 0 1
+ * a = 0 0 0 0 0 1 0 1
  * b = 0 0 0 0 1 0 1 0
  * z = 0000000001100110
  */
@@ -30,24 +32,26 @@
 namespace boost
 {
 
+/*
+ * Interleave two 16-bit integral types and returns the bits interleaved in an
+ * uint32_t.
+ */
 template <typename T>
-inline typename enable_if_c<is_integral<T>::type::value && sizeof(T) == 4, T>::type
+inline typename enable_if_c<is_integral<T>::type::value && sizeof(T) == 2, uint32_t>::type
 interleave(T x, T y)
 {
- // puts the byte 2 in the byte 4
- // and the byte 3 in the byte 5
+ uint32_t a(x), b(y);
+
+ // puts the byte 2 in the byte 4 and the byte 3 in the byte 5
         a = (a | (a << 8)) & 0x00FF00FF;
         
- // puts the bytes in alternate
- // bytes
+ // now the original bytes of x are in alternate bytes
         a = (a | (a << 4)) & 0x0F0F0F0F;
         
- // now the original bits are in
- // the lowest 2 bits of the nibbles
+ // now the original bits of x are in the lowest 2 bits of the nibbles
         a = (a | (a << 2)) & 0x33333333;
         
- // now the original bits are in
- // the even bits
+ // now the original bits are in the even bits
         a = (a | (a << 1)) & 0x55555555;
         
         // do the same with b
@@ -55,15 +59,44 @@
         b = (b | (b << 4)) & 0x0F0F0F0F;
         b = (b | (b << 2)) & 0x33333333;
         b = (b | (b << 1)) & 0x55555555;
- // now the original bits are in
- // the odd bits
+
+ // now the original bits are in the odd bits
         b <<= 1;
 
- // the result is the union of
- // a and b bits
+ // the result is the union of a and b bits
         return (a | b);
 }
 
+/*
+ * Interleave two 8-bit integral types and returns the bits interleaved in an
+ * uint16_t.
+ */
+template <typename T>
+inline typename enable_if_c<is_integral<T>::type::value && sizeof(T) == 1, uint16_t>::type
+interleave(T x, T y)
+{
+ uint16_t a(x), b(y);
+
+ // now the original bytes are in alternate bytes
+ a = (a | (a << 4)) & 0x0F0F;
+
+ // now the original bits are in the lowest 2 bits of the nibbles
+ a = (a | (a << 2)) & 0x3333;
+
+ // now the original bits are in the even bits
+ a = (a | (a << 1)) & 0x5555;
+
+ // do the same with b
+ b = (b | (b << 4)) & 0x0F0F;
+ b = (b | (b << 2)) & 0x3333;
+ b = (b | (b << 1)) & 0x5555;
+
+ // now the original bits are inthe odd bits
+ b <<= 1;
+
+ // the result is the union of a and b bits
+ return (a | b);
 }
 
+}
 #endif
\ No newline at end of file


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