1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Stephen Cleary 2000.
4 // (C) Copyright Ion Gaztanaga 2007-2012.
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/interprocess for documentation.
12 // This file is a slightly modified file from Boost.Pool
14 //////////////////////////////////////////////////////////////////////////////
16 #ifndef BOOST_INTERPROCESS_DETAIL_MATH_FUNCTIONS_HPP
17 #define BOOST_INTERPROCESS_DETAIL_MATH_FUNCTIONS_HPP
19 #ifndef BOOST_CONFIG_HPP
20 # include <boost/config.hpp>
23 #if defined(BOOST_HAS_PRAGMA_ONCE)
28 #include <boost/static_assert.hpp>
31 namespace interprocess {
34 // Greatest common divisor and least common multiple
37 // gcd is an algorithm that calculates the greatest common divisor of two
38 // integers, using Euclid's algorithm.
40 // Pre: A > 0 && B > 0
42 template <typename Integer>
43 inline Integer gcd(Integer A, Integer B)
56 // lcm is an algorithm that calculates the least common multiple of two
59 // Pre: A > 0 && B > 0
61 template <typename Integer>
62 inline Integer lcm(const Integer & A, const Integer & B)
70 template <typename Integer>
71 inline Integer log2_ceil(const Integer & A)
74 Integer power_of_2 = 1;
76 while(power_of_2 < A){
83 template <typename Integer>
84 inline Integer upper_power_of_2(const Integer & A)
86 Integer power_of_2 = 1;
88 while(power_of_2 < A){
94 //This function uses binary search to discover the
95 //highest set bit of the integer
96 inline std::size_t floor_log2 (std::size_t x)
98 const std::size_t Bits = sizeof(std::size_t)*CHAR_BIT;
99 const bool Size_t_Bits_Power_2= !(Bits & (Bits-1));
100 BOOST_STATIC_ASSERT(((Size_t_Bits_Power_2)== true));
103 std::size_t log2 = 0;
105 for(std::size_t shift = Bits >> 1; shift; shift >>= 1){
106 std::size_t tmp = n >> shift;
108 log2 += shift, n = tmp;
114 } // namespace ipcdetail
115 } // namespace interprocess