annotate DEPENDENCIES/generic/include/boost/pending/integer_log2.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
rev   line source
Chris@16 1 // -----------------------------------------------------------
Chris@16 2 // integer_log2.hpp
Chris@16 3 //
Chris@16 4 // Gives the integer part of the logarithm, in base 2, of a
Chris@16 5 // given number. Behavior is undefined if the argument is <= 0.
Chris@16 6 //
Chris@16 7 // Copyright (c) 2003-2004, 2008 Gennaro Prota
Chris@16 8 //
Chris@16 9 // Distributed under the Boost Software License, Version 1.0.
Chris@16 10 // (See accompanying file LICENSE_1_0.txt or copy at
Chris@16 11 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 12 //
Chris@16 13 // -----------------------------------------------------------
Chris@16 14
Chris@16 15 #ifndef BOOST_INTEGER_LOG2_HPP_GP_20030301
Chris@16 16 #define BOOST_INTEGER_LOG2_HPP_GP_20030301
Chris@16 17
Chris@16 18 #include <assert.h>
Chris@16 19 #ifdef __BORLANDC__
Chris@16 20 #include <climits>
Chris@16 21 #endif
Chris@16 22 #include "boost/limits.hpp"
Chris@16 23 #include "boost/config.hpp"
Chris@16 24
Chris@16 25
Chris@16 26 namespace boost {
Chris@16 27 namespace detail {
Chris@16 28
Chris@16 29 template <typename T>
Chris@16 30 int integer_log2_impl(T x, int n) {
Chris@16 31
Chris@16 32 int result = 0;
Chris@16 33
Chris@16 34 while (x != 1) {
Chris@16 35
Chris@16 36 const T t = static_cast<T>(x >> n);
Chris@16 37 if (t) {
Chris@16 38 result += n;
Chris@16 39 x = t;
Chris@16 40 }
Chris@16 41 n /= 2;
Chris@16 42
Chris@16 43 }
Chris@16 44
Chris@16 45 return result;
Chris@16 46 }
Chris@16 47
Chris@16 48
Chris@16 49
Chris@16 50 // helper to find the maximum power of two
Chris@16 51 // less than p (more involved than necessary,
Chris@16 52 // to avoid PTS)
Chris@16 53 //
Chris@16 54 template <int p, int n>
Chris@16 55 struct max_pow2_less {
Chris@16 56
Chris@16 57 enum { c = 2*n < p };
Chris@16 58
Chris@16 59 BOOST_STATIC_CONSTANT(int, value =
Chris@16 60 c ? (max_pow2_less< c*p, 2*c*n>::value) : n);
Chris@16 61
Chris@16 62 };
Chris@16 63
Chris@16 64 template <>
Chris@16 65 struct max_pow2_less<0, 0> {
Chris@16 66
Chris@16 67 BOOST_STATIC_CONSTANT(int, value = 0);
Chris@16 68 };
Chris@16 69
Chris@16 70 // this template is here just for Borland :(
Chris@16 71 // we could simply rely on numeric_limits but sometimes
Chris@16 72 // Borland tries to use numeric_limits<const T>, because
Chris@16 73 // of its usual const-related problems in argument deduction
Chris@16 74 // - gps
Chris@16 75 template <typename T>
Chris@16 76 struct width {
Chris@16 77
Chris@16 78 #ifdef __BORLANDC__
Chris@16 79 BOOST_STATIC_CONSTANT(int, value = sizeof(T) * CHAR_BIT);
Chris@16 80 #else
Chris@16 81 BOOST_STATIC_CONSTANT(int, value = (std::numeric_limits<T>::digits));
Chris@16 82 #endif
Chris@16 83
Chris@16 84 };
Chris@16 85
Chris@16 86 } // detail
Chris@16 87
Chris@16 88
Chris@16 89 // ---------
Chris@16 90 // integer_log2
Chris@16 91 // ---------------
Chris@16 92 //
Chris@16 93 template <typename T>
Chris@16 94 int integer_log2(T x) {
Chris@16 95
Chris@16 96 assert(x > 0);
Chris@16 97
Chris@16 98 const int n = detail::max_pow2_less<
Chris@16 99 detail::width<T> :: value, 4
Chris@16 100 > :: value;
Chris@16 101
Chris@16 102 return detail::integer_log2_impl(x, n);
Chris@16 103
Chris@16 104 }
Chris@16 105
Chris@16 106
Chris@16 107
Chris@16 108 }
Chris@16 109
Chris@16 110
Chris@16 111
Chris@16 112 #endif // include guard