1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Stephen Cleary 2000.
4 // (C) Copyright Ion Gaztanaga 2007-2013.
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
10 // See http://www.boost.org/libs/container for documentation.
12 // This file is a slightly modified file from Boost.Pool
14 //////////////////////////////////////////////////////////////////////////////
16 #ifndef BOOST_CONTAINER_DETAIL_MATH_FUNCTIONS_HPP
17 #define BOOST_CONTAINER_DETAIL_MATH_FUNCTIONS_HPP
19 #ifndef BOOST_CONFIG_HPP
20 # include <boost/config.hpp>
23 #if defined(BOOST_HAS_PRAGMA_ONCE)
27 #include <boost/container/detail/config_begin.hpp>
28 #include <boost/container/detail/workaround.hpp>
31 #include <boost/static_assert.hpp>
37 // Greatest common divisor and least common multiple
40 // gcd is an algorithm that calculates the greatest common divisor of two
41 // integers, using Euclid's algorithm.
43 // Pre: A > 0 && B > 0
45 template <typename Integer>
46 inline Integer gcd(Integer A, Integer B)
59 // lcm is an algorithm that calculates the least common multiple of two
62 // Pre: A > 0 && B > 0
64 template <typename Integer>
65 inline Integer lcm(const Integer & A, const Integer & B)
73 template <typename Integer>
74 inline Integer log2_ceil(const Integer & A)
77 Integer power_of_2 = 1;
79 while(power_of_2 < A){
86 template <typename Integer>
87 inline Integer upper_power_of_2(const Integer & A)
89 Integer power_of_2 = 1;
91 while(power_of_2 < A){
97 template <typename Integer, bool Loop = true>
98 struct upper_power_of_2_loop_ct
101 template <Integer I, Integer P>
104 static const Integer value =
105 upper_power_of_2_loop_ct<Integer, (I > P*2)>::template apply<I, P*2>::value;
109 template <typename Integer>
110 struct upper_power_of_2_loop_ct<Integer, false>
112 template <Integer I, Integer P>
115 static const Integer value = P;
119 template <typename Integer, Integer I>
120 struct upper_power_of_2_ct
122 static const Integer value = upper_power_of_2_loop_ct<Integer, (I > 1)>::template apply<I, 2>::value;
125 //This function uses binary search to discover the
126 //highest set bit of the integer
127 inline std::size_t floor_log2 (std::size_t x)
129 const std::size_t Bits = sizeof(std::size_t)*CHAR_BIT;
130 const bool Size_t_Bits_Power_2= !(Bits & (Bits-1));
131 BOOST_STATIC_ASSERT(((Size_t_Bits_Power_2)== true));
134 std::size_t log2 = 0;
136 for(std::size_t shift = Bits >> 1; shift; shift >>= 1){
137 std::size_t tmp = n >> shift;
139 log2 += shift, n = tmp;
145 template<std::size_t I1, std::size_t I2>
148 static const std::size_t Max = I1 > I2 ? I1 : I2;
149 static const std::size_t Min = I1 < I2 ? I1 : I2;
150 static const std::size_t value = gcd_ct<Min, Max % Min>::value;
153 template<std::size_t I1>
156 static const std::size_t value = I1;
159 template<std::size_t I1>
162 static const std::size_t value = I1;
165 template<std::size_t I1, std::size_t I2>
168 static const std::size_t value = I1 * I2 / gcd_ct<I1, I2>::value;
172 } // namespace container
175 #include <boost/container/detail/config_end.hpp>