|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r63996 - in sandbox/SOC/2010/bits_and_ints: boost/integer libs/integer/test
From: muriloufg_at_[hidden]
Date: 2010-07-13 16:00:57
Author: murilov
Date: 2010-07-13 16:00:56 EDT (Tue, 13 Jul 2010)
New Revision: 63996
URL: http://svn.boost.org/trac/boost/changeset/63996
Log:
Added ilog2.hpp wich contains a function wich calculates the integer log2 of an unsigned integral value
Added:
sandbox/SOC/2010/bits_and_ints/boost/integer/ilog2.hpp (contents, props changed)
sandbox/SOC/2010/bits_and_ints/libs/integer/test/ilog2_test.cpp (contents, props changed)
Added: sandbox/SOC/2010/bits_and_ints/boost/integer/ilog2.hpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2010/bits_and_ints/boost/integer/ilog2.hpp 2010-07-13 16:00:56 EDT (Tue, 13 Jul 2010)
@@ -0,0 +1,37 @@
+// 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_ILOG2_INCLUDED
+#define BOOST_ILOG2_INCLUDED
+
+#include <boost/static_assert.hpp>
+#include <boost/type_traits/is_unsigned.hpp>
+#include <boost/integer/count_leading_zeros.hpp>
+
+namespace boost {
+
+/*
+ * This function returns the integer value of
+ * logarithm in base 2 of an unsigned integral `value`
+ *
+ * If `value` is equal to 0, the value returned is -1
+ *
+ * `value` must be unsigned.
+ */
+template <typename T>
+inline T ilog2(T value)
+{
+ BOOST_STATIC_ASSERT((is_unsigned<T>::value));
+
+ return (sizeof(T) * 8) - count_leading_zeros(value) - 1;
+}
+
+}
+
+#endif
Added: sandbox/SOC/2010/bits_and_ints/libs/integer/test/ilog2_test.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2010/bits_and_ints/libs/integer/test/ilog2_test.cpp 2010-07-13 16:00:56 EDT (Tue, 13 Jul 2010)
@@ -0,0 +1,40 @@
+// 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/ilog2.hpp>
+#include <boost/cstdint.hpp>
+#include <iostream>
+
+#define ILOG2_TEST(x, y) \
+BOOST_TEST((::boost::ilog2(x) == y)); \
+std::cout << "deu " << ::boost::ilog2(x) << " era pra ser " << y << std::endl
+
+// Main testing function
+int main(int, char* [])
+{
+ using namespace boost;
+ std::cout << "Doing tests on ilog2 function." << std::endl;
+
+ ILOG2_TEST((unsigned)0, -1);
+ ILOG2_TEST((unsigned)100, 6);
+ ILOG2_TEST((unsigned)265, 8);
+ ILOG2_TEST((unsigned)158741, 17);
+ ILOG2_TEST((unsigned)85256985, 26);
+ ILOG2_TEST((unsigned)1073741824, 30);
+ ILOG2_TEST((unsigned)8, 3);
+ ILOG2_TEST((unsigned)21, 4);
+ ILOG2_TEST((unsigned)23, 4);
+ ILOG2_TEST((unsigned)1, 0);
+
+#ifndef BOOST_HAS_NO_INT64_T
+ ILOG2_TEST(8589934592ULL, 33);
+ ILOG2_TEST(17179869181ULL, 33);
+ ILOG2_TEST(34359738369ULL, 35);
+#endif
+ return boost::report_errors();
+}
\ 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