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>
35 namespace container_detail {
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 //This function uses binary search to discover the
98 //highest set bit of the integer
99 inline std::size_t floor_log2 (std::size_t x)
101 const std::size_t Bits = sizeof(std::size_t)*CHAR_BIT;
102 const bool Size_t_Bits_Power_2= !(Bits & (Bits-1));
103 BOOST_STATIC_ASSERT(((Size_t_Bits_Power_2)== true));
106 std::size_t log2 = 0;
108 for(std::size_t shift = Bits >> 1; shift; shift >>= 1){
109 std::size_t tmp = n >> shift;
111 log2 += shift, n = tmp;
117 } // namespace container_detail
118 } // namespace container
121 #include <boost/container/detail/config_end.hpp>