Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r63992 - in sandbox/SOC/2010/bits_and_ints: boost/integer libs/integer/test
From: muriloufg_at_[hidden]
Date: 2010-07-13 15:10:45


Author: murilov
Date: 2010-07-13 15:10:44 EDT (Tue, 13 Jul 2010)
New Revision: 63992
URL: http://svn.boost.org/trac/boost/changeset/63992

Log:
Added functions to count leading zeros in an integral value
Added some tests
Added:
   sandbox/SOC/2010/bits_and_ints/boost/integer/count_leading_zeros.hpp (contents, props changed)
   sandbox/SOC/2010/bits_and_ints/libs/integer/test/count_leading_zeros_test.cpp (contents, props changed)
Text files modified:
   sandbox/SOC/2010/bits_and_ints/libs/integer/test/Jamfile.v2 | 1 +
   sandbox/SOC/2010/bits_and_ints/libs/integer/test/isign_test.cpp | 2 +-
   2 files changed, 2 insertions(+), 1 deletions(-)

Added: sandbox/SOC/2010/bits_and_ints/boost/integer/count_leading_zeros.hpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2010/bits_and_ints/boost/integer/count_leading_zeros.hpp 2010-07-13 15:10:44 EDT (Tue, 13 Jul 2010)
@@ -0,0 +1,83 @@
+// Boost integer/count_leading_zeros.hpp header file ------------------------------//
+
+// (C) Copyright Murilo Adriano Vasconcelos 2010.
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt
+
+// See http://www.boost.org for updates, documentation, and revision history.
+
+#ifndef BOOST_COUNT_LEADING_ZEROS_INCLUDED
+#define BOOST_COUNT_LEADING_ZEROS_INCLUDED
+
+#include <boost/cstdint.hpp>
+#include <boost/utility/enable_if.hpp>
+#include <boost/integer/pop_count.hpp>
+
+/*
+ * count_leading_zeros counts the number of consecutive 0's
+ * from the most significant bit of an integral value.
+ *
+ * For example:
+ *
+ * leading zeros: vvvv vvv
+ * unsigned value = 0xF080; // 0000 0001 1111 0000 1000 0000
+ * int x = count_leading_zeros(value); // x = 7
+ *
+ */
+
+namespace boost {
+
+template <typename T>
+typename enable_if_c<sizeof(T) == 1, int>::type
+count_leading_zeros(T value)
+{
+ value = value | (value >> 1);
+ value = value | (value >> 2);
+ value = value | (value >> 4);
+
+ return pop_count(T(~value));
+}
+
+template <typename T>
+typename enable_if_c<sizeof(T) == 2, int>::type
+count_leading_zeros(T value)
+{
+ value = value | (value >> 1);
+ value = value | (value >> 2);
+ value = value | (value >> 4);
+ value = value | (value >> 8);
+
+ return pop_count(T(~value));
+}
+
+template <typename T>
+typename enable_if_c<sizeof(T) == 4, int>::type
+count_leading_zeros(T value)
+{
+ value = value | (value >> 1);
+ value = value | (value >> 2);
+ value = value | (value >> 4);
+ value = value | (value >> 8);
+ value = value | (value >> 16);
+
+ return pop_count(T(~value));
+}
+
+template <typename T>
+typename enable_if_c<sizeof(T) == 8, int>::type
+count_leading_zeros(T value)
+{
+ value = value | (value >> 1);
+ value = value | (value >> 2);
+ value = value | (value >> 4);
+ value = value | (value >> 8);
+ value = value | (value >> 16);
+ value = value | (value >> 16);
+
+ return pop_count(T(~value));
+}
+
+} // boost
+
+#endif

Modified: sandbox/SOC/2010/bits_and_ints/libs/integer/test/Jamfile.v2
==============================================================================
--- sandbox/SOC/2010/bits_and_ints/libs/integer/test/Jamfile.v2 (original)
+++ sandbox/SOC/2010/bits_and_ints/libs/integer/test/Jamfile.v2 2010-07-13 15:10:44 EDT (Tue, 13 Jul 2010)
@@ -26,6 +26,7 @@
                 [ run clear_least_bit_set_test.cpp ]
                 [ run safe_avg_test.cpp ]
                 [ run isign_test.cpp ]
+ [ run count_leading_zeros_test.cpp ]
                 [ compile cstdint_include_test.cpp ]
                 [ compile integer_traits_include_test.cpp ]
                 [ compile integer_include_test.cpp ]

Added: sandbox/SOC/2010/bits_and_ints/libs/integer/test/count_leading_zeros_test.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2010/bits_and_ints/libs/integer/test/count_leading_zeros_test.cpp 2010-07-13 15:10:44 EDT (Tue, 13 Jul 2010)
@@ -0,0 +1,54 @@
+// Boost count_leading_zeros_test.hpp test program --------------------------------------//
+
+// (C) Copyright Murilo Adriano Vasconcelos 2010.
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt
+
+#include <boost/detail/lightweight_test.hpp>
+#include <boost/integer/count_leading_zeros.hpp>
+#include <boost/mpl/integral_c.hpp>
+#include <boost/cstdint.hpp>
+#include <iostream>
+
+#define CLZ_TEST(x, y) \
+BOOST_TEST((::boost::count_leading_zeros(x) == y))
+
+// Main testing function
+int main(int, char* [])
+{
+ using namespace boost;
+ std::cout << "Doing tests on safe_avg functions." << std::endl;
+
+ CLZ_TEST(uint8_t(0xFF), 0);
+ CLZ_TEST(uint8_t(0x7F), 1);
+ CLZ_TEST(uint8_t(0x3F), 2);
+ CLZ_TEST(uint8_t(0x1F), 3);
+ CLZ_TEST(uint8_t(0x0F), 4);
+ CLZ_TEST(uint8_t(0x07), 5);
+ CLZ_TEST(uint8_t(0x03), 6);
+ CLZ_TEST(uint8_t(0x01), 7);
+ CLZ_TEST(uint8_t(0x00), 8);
+
+ CLZ_TEST(uint16_t(0xF000), 0);
+ CLZ_TEST(uint16_t(0xAA00), 0);
+ CLZ_TEST(uint16_t(0x0000), 16);
+ CLZ_TEST(uint16_t(0x0002), 14);
+
+ CLZ_TEST(uint32_t(0x0), 32);
+ CLZ_TEST(uint32_t(0xFFFFFFFF), 0);
+ CLZ_TEST(uint32_t(0x0001FFFF), 15);
+ CLZ_TEST(uint32_t(0x00FFFFFF), 8);
+ CLZ_TEST(uint32_t(0x007FFFFF), 9);
+
+#ifndef BOOST_HAS_NO_INT64_T
+
+ CLZ_TEST(0x0LL, 64);
+ CLZ_TEST(0xFLL, 60);
+ CLZ_TEST(0xFFFFFFFFLL, 32);
+ CLZ_TEST(0x7FFFFFFFLL, 33);
+ CLZ_TEST(0xFFFFFFFFFFFFFFFFLL, 0);
+
+#endif
+ return boost::report_errors();
+}
\ No newline at end of file

Modified: sandbox/SOC/2010/bits_and_ints/libs/integer/test/isign_test.cpp
==============================================================================
--- sandbox/SOC/2010/bits_and_ints/libs/integer/test/isign_test.cpp (original)
+++ sandbox/SOC/2010/bits_and_ints/libs/integer/test/isign_test.cpp 2010-07-13 15:10:44 EDT (Tue, 13 Jul 2010)
@@ -1,4 +1,4 @@
-// Boost safe_avg_test.hpp test program --------------------------------------//
+// Boost isign_test.hpp test program --------------------------------------//
 
 // (C) Copyright Murilo Adriano Vasconcelos 2010.
 // Distributed under the Boost Software License, Version 1.0. (See


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