|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r62429 - in sandbox/SOC/2010/bits_and_ints: boost/integer libs/integer/test
From: muriloufg_at_[hidden]
Date: 2010-06-04 12:39:18
Author: murilov
Date: 2010-06-04 12:39:17 EDT (Fri, 04 Jun 2010)
New Revision: 62429
URL: http://svn.boost.org/trac/boost/changeset/62429
Log:
Added 8-bit and 16-bit overload to bit_reversal function.
Text files modified:
sandbox/SOC/2010/bits_and_ints/boost/integer/bit_reversal.hpp | 32 ++++++++++++++++++++++++++++++++
sandbox/SOC/2010/bits_and_ints/libs/integer/test/bit_reversal_test.cpp | 26 ++++++++++++++++++++++++++
sandbox/SOC/2010/bits_and_ints/libs/integer/test/sign_extend_test.cpp | 4 ++--
3 files changed, 60 insertions(+), 2 deletions(-)
Modified: sandbox/SOC/2010/bits_and_ints/boost/integer/bit_reversal.hpp
==============================================================================
--- sandbox/SOC/2010/bits_and_ints/boost/integer/bit_reversal.hpp (original)
+++ sandbox/SOC/2010/bits_and_ints/boost/integer/bit_reversal.hpp 2010-06-04 12:39:17 EDT (Fri, 04 Jun 2010)
@@ -66,6 +66,38 @@
return data;
}
+
+// 16-bit version
+template <typename T>
+inline typename enable_if_c<is_integral<T>::type::value && sizeof(T) == 2, T>::type
+bit_reversal(T data)
+{
+ // swap odd and even bits
+ data = ((data >> 1) & 0x5555) | ((data & 0x5555) << 1);
+ // swap consecutive pairs
+ data = ((data >> 2) & 0x3333) | ((data & 0x3333) << 2);
+ // swap nibbles
+ data = ((data >> 4) & 0x0F0F) | ((data & 0x0F0F) << 4);
+ // swap bytes
+ data = (data >> 8) | (data << 8);
+
+ return data;
+}
+
+// 8-bit version
+template <typename T>
+inline typename enable_if_c<is_integral<T>::type::value && sizeof(T) == 1, T>::type
+bit_reversal(T data)
+{
+ // swap odd and even bits
+ data = ((data >> 1) & 0x55) | ((data & 0x55) << 1);
+ // swap consecutive pairs
+ data = ((data >> 2) & 0x33) | ((data & 0x33) << 2);
+ // swap nibbles
+ data = (data >> 4) | (data << 4);
+
+ return data;
+}
} // boost
Modified: sandbox/SOC/2010/bits_and_ints/libs/integer/test/bit_reversal_test.cpp
==============================================================================
--- sandbox/SOC/2010/bits_and_ints/libs/integer/test/bit_reversal_test.cpp (original)
+++ sandbox/SOC/2010/bits_and_ints/libs/integer/test/bit_reversal_test.cpp 2010-06-04 12:39:17 EDT (Fri, 04 Jun 2010)
@@ -26,6 +26,32 @@
{
std::cout << "Doing tests on bit_reversal." << std::endl;
+
+ BIT_REVERSAL_TEST(uint8_t(0), uint8_t(0));
+ BIT_REVERSAL_TEST(uint8_t(1), uint8_t(1) << 7);
+ BIT_REVERSAL_TEST(uint8_t(0xF0), uint8_t(0x0F));
+ BIT_REVERSAL_TEST(uint8_t(0x50), uint8_t(0x0A));
+
+ for (uint8_t i = 2, acc = 2; i < 8; ++i) {
+ BIT_REVERSAL_TEST(acc, (uint8_t(1) << (8 - i)));
+ acc *= 2;
+ }
+
+ BIT_REVERSAL_TEST(uint16_t(0), uint16_t(0));
+ BIT_REVERSAL_TEST(uint16_t(1), uint16_t(1) << 15);
+ BIT_REVERSAL_TEST(uint16_t(0xF000), uint16_t(0x000F));
+ BIT_REVERSAL_TEST(uint16_t(0x5000), uint16_t(0x000A));
+ BIT_REVERSAL_TEST(uint16_t(0xA000), uint16_t(0x0005));
+ BIT_REVERSAL_TEST(uint16_t(0x1000), uint16_t(0x0008));
+ BIT_REVERSAL_TEST(uint16_t(0xFF00), uint16_t(0x00FF));
+ BIT_REVERSAL_TEST(uint16_t(0x3030), uint16_t(0x0C0C));
+
+ for (uint16_t i = 2, acc = 2; i < 16; ++i) {
+ BIT_REVERSAL_TEST(acc, (uint16_t(1) << (16 - i)));
+ acc *= 2;
+ }
+
+
BIT_REVERSAL_TEST(unsigned(0), unsigned(0));
BIT_REVERSAL_TEST(unsigned(1), unsigned(1 << 31));
Modified: sandbox/SOC/2010/bits_and_ints/libs/integer/test/sign_extend_test.cpp
==============================================================================
--- sandbox/SOC/2010/bits_and_ints/libs/integer/test/sign_extend_test.cpp (original)
+++ sandbox/SOC/2010/bits_and_ints/libs/integer/test/sign_extend_test.cpp 2010-06-04 12:39:17 EDT (Fri, 04 Jun 2010)
@@ -8,10 +8,10 @@
#include <boost/detail/lightweight_test.hpp>
-#include <boost/cstdlib.hpp> // for boost::exit_success
+#include <boost/cstdlib.hpp> // for boost::exit_success
#include <boost/integer/bits_and_ints.hpp> // for boost::sign_extend and boost::static_sign_extend
-#include <iostream> // for std::cout (std::endl indirectly)
+ #include <iostream> // for std::cout (std::endl indirectly)
// Macros to compact code
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