Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r63193 - sandbox/SOC/2010/bits_and_ints/boost/integer
From: muriloufg_at_[hidden]
Date: 2010-06-21 14:01:57


Author: murilov
Date: 2010-06-21 14:01:55 EDT (Mon, 21 Jun 2010)
New Revision: 63193
URL: http://svn.boost.org/trac/boost/changeset/63193

Log:
Population count (pop_count) and count_trailing_zeros functions added
Added:
   sandbox/SOC/2010/bits_and_ints/boost/integer/count_trailing_zeros.hpp (contents, props changed)
   sandbox/SOC/2010/bits_and_ints/boost/integer/pop_count.hpp (contents, props changed)
Text files modified:
   sandbox/SOC/2010/bits_and_ints/boost/integer/bit_utils.hpp | 4 +++-
   1 files changed, 3 insertions(+), 1 deletions(-)

Modified: sandbox/SOC/2010/bits_and_ints/boost/integer/bit_utils.hpp
==============================================================================
--- sandbox/SOC/2010/bits_and_ints/boost/integer/bit_utils.hpp (original)
+++ sandbox/SOC/2010/bits_and_ints/boost/integer/bit_utils.hpp 2010-06-21 14:01:55 EDT (Mon, 21 Jun 2010)
@@ -18,6 +18,8 @@
 namespace boost
 {
         
+/*** Static metafunctions ***/
+
 // Sets the bit `pos' in data
 template <typename T, T data, unsigned char pos>
 struct set_bit
@@ -44,7 +46,7 @@
 struct test_bit
 {
         BOOST_STATIC_CONSTANT(bool, value = ((data >> pos) & T(1)) != T(0));
-}; // test_bit
+}; // test_bit
         
 } // boost
 

Added: sandbox/SOC/2010/bits_and_ints/boost/integer/count_trailing_zeros.hpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2010/bits_and_ints/boost/integer/count_trailing_zeros.hpp 2010-06-21 14:01:55 EDT (Mon, 21 Jun 2010)
@@ -0,0 +1,37 @@
+// Boost integer/count_trailing_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_TRAILING_ZEROS_INCLUDED
+#define BOOST_COUNT_TRAILING_ZEROS_INCLUDED
+
+#include <boost/integer/pop_count.hpp>
+#include <boost/cstdint.hpp>
+
+/*
+ * count_trailing_zeros counts the number of consecutive 0's
+ * from the least significant bit of an integral value.
+ *
+ * For example:
+ *
+ * trailing zeros: vvv vvvv
+ * unsigned value = 0xF080; // 0000 0000 1111 0000 1000 0000
+ * int x = count_trailing_zeros(value); // x = 7
+ *
+ */
+
+namespace boost {
+
+int count_trailing_zeros(uintmax_t value)
+{
+ return pop_count(~value & (value - 1));
+} // count_trailing_zeros
+
+} // boost
+
+#endif
\ No newline at end of file

Added: sandbox/SOC/2010/bits_and_ints/boost/integer/pop_count.hpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2010/bits_and_ints/boost/integer/pop_count.hpp 2010-06-21 14:01:55 EDT (Mon, 21 Jun 2010)
@@ -0,0 +1,54 @@
+// Boost integer/pop_count.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_POP_COUNT_INCLUDED
+#define BOOST_POP_COUNT_INCLUDED
+
+#include <boost/cstdint.hpp>
+
+/*
+ * Population count (pop_count) counts the number of 1-bits in an
+ * integral value.
+ *
+ * For example:
+ *
+ * uint8_t value = 0xAF; // value is 10101111
+ *
+ * int x = pop_count(value); // x = 6
+ *
+ */
+
+namespace boost {
+
+int pop_count(uintmax_t value)
+{
+ static const uintmax_t mask[6] = {
+#ifndef BOOST_NO_INT64_T
+ 0x5555555555555555, 0x3333333333333333, 0x0F0F0F0F0F0F0F0F,
+ 0x00FF00FF00FF00FF, 0x0000FFFF0000FFFF, 0x00000000FFFFFFFF
+#else
+ 0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF, 0x0
+#endif
+ };
+
+ value = (value & mask[0]) + ((value >> 1) & mask[0]);
+ value = (value & mask[1]) + ((value >> 2) & mask[1]);
+ value = (value & mask[2]) + ((value >> 4) & mask[2]);
+ value = (value & mask[3]) + ((value >> 8) & mask[3]);
+ value = (value & mask[4]) + ((value >> 16) & mask[4]);
+#ifndef BOOST_NO_INT64_T
+ value = (value & mask[5]) + ((value >> 32) & mask[5]);
+#endif
+
+ return int(value);
+} // pop_count
+
+} // boost
+
+#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