|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r64197 - sandbox/SOC/2010/bits_and_ints/boost/integer
From: muriloufg_at_[hidden]
Date: 2010-07-20 09:53:51
Author: murilov
Date: 2010-07-20 09:53:50 EDT (Tue, 20 Jul 2010)
New Revision: 64197
URL: http://svn.boost.org/trac/boost/changeset/64197
Log:
Added count_leading_zeros() verification:
If value is 0, now it returns the size in bits of value type
Added policies on ilog2() function:
- Now if value is 0, an indeterminate_result_error is raised and the user can handle this error according the policy utilized.
Text files modified:
sandbox/SOC/2010/bits_and_ints/boost/integer/count_leading_zeros.hpp | 54 ++++++++++++++++-----------------------
sandbox/SOC/2010/bits_and_ints/boost/integer/ilog2.hpp | 28 +++++++++++++++++--
2 files changed, 46 insertions(+), 36 deletions(-)
Modified: sandbox/SOC/2010/bits_and_ints/boost/integer/count_leading_zeros.hpp
==============================================================================
--- sandbox/SOC/2010/bits_and_ints/boost/integer/count_leading_zeros.hpp (original)
+++ sandbox/SOC/2010/bits_and_ints/boost/integer/count_leading_zeros.hpp 2010-07-20 09:53:50 EDT (Tue, 20 Jul 2010)
@@ -30,53 +30,43 @@
#ifdef __GNUC__
template <typename T>
-inline int unchecked_count_leading_zeros(T value)
-{
- return __builtin_clz(value) - (32 - (sizeof(T) << 3));
-}
-
-template <typename T>
inline int count_leading_zeros(T value)
{
- return (value != 0) ?
- __builtin_clz(value) - (32 - (sizeof(T) << 3)) : -1;
+ if (value == 0) {
+ return sizeof(T) << 3;
+ }
+
+ return __builtin_clz(value) - (32 - (sizeof(T) << 3));
}
-inline int unchecked_count_leading_zeros(unsigned int value)
-{
- return (value != 0) ?
- __builtin_clz(value) : -1;
-}
-
inline int count_leading_zeros(unsigned int value)
{
- return __builtin_clz(value);
-}
+ if (value == 0) {
+ return sizeof(unsigned int) << 3;
+ }
-inline int unchecked_count_leading_zeros(unsigned long int value)
-{
- return __builtin_clzl(value);
+ return __builtin_clz(value);
}
-
+
inline int count_leading_zeros(unsigned long int value)
{
- return (value != 0) ?
- __builtin_clzl(value) : -1;
+ if (value == 0) {
+ return sizeof(unsigned long int) << 3;
+ }
+
+ return __builtin_clzl(value);
}
#ifndef BOOST_HAS_NO_INT64_T
-inline int unchecked_count_leading_zeros(unsigned long long int value)
-{
- return __builtin_clzll(value);
-}
-
-
inline int count_leading_zeros(unsigned long long int value)
-{
- return (value != 0) ?
- __builtin_clzll(value) : -1;
+{
+ if (value == 0) {
+ return sizeof(unsigned long long int) << 3;
+ }
+
+ return __builtin_clzll(value);
}
-#endif
+#endif
#else
Modified: sandbox/SOC/2010/bits_and_ints/boost/integer/ilog2.hpp
==============================================================================
--- sandbox/SOC/2010/bits_and_ints/boost/integer/ilog2.hpp (original)
+++ sandbox/SOC/2010/bits_and_ints/boost/integer/ilog2.hpp 2010-07-20 09:53:50 EDT (Tue, 20 Jul 2010)
@@ -13,6 +13,8 @@
#include <boost/static_assert.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_unsigned.hpp>
+#include <boost/math/policies/policy.hpp>
+#include <boost/math/policies/error_handling.hpp>
#include <boost/integer/count_leading_zeros.hpp>
namespace boost {
@@ -21,15 +23,33 @@
* 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 undefined
+ * If `value` is equal to 0, the value returned is -1 and
+ * one error is raised according with the policy used.
+ *
+ * See the math::policies documentation for more details.
*
* `value` must be unsigned.
*/
+template <typename T, typename Policy>
+inline typename enable_if<is_unsigned<T>, int>::type
+ilog2(T value, const Policy& pol)
+{
+ if (value == 0) {
+ return math::policies::raise_indeterminate_result_error(
+ "boost::ilog2(%1%)",
+ "ilog2 is indeterminate for value 0, retunrning -1",
+ 0, -1, pol
+ );
+ }
+
+ return (sizeof(T) << 3) - count_leading_zeros(value) - 1;
+}
+
template <typename T>
-inline typename enable_if<is_unsigned<T>, T>::type
+inline typename enable_if<is_unsigned<T>, int>::type
ilog2(T value)
-{
- return (sizeof(T) * 8) - count_leading_zeros(value) - 1;
+{
+ return ilog2(value, math::policies::policy<>());
}
}
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