1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Stephen Cleary 2000.
4 // (C) Copyright Ion Gaztanaga 2007-2008.
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
20 #include <boost/static_assert.hpp>
23 namespace interprocess
{
26 // Greatest common divisor and least common multiple
29 // gcd is an algorithm that calculates the greatest common divisor of two
30 // integers, using Euclid's algorithm.
32 // Pre: A > 0 && B > 0
34 template <typename Integer
>
35 inline Integer
gcd(Integer A
, Integer B
)
48 // lcm is an algorithm that calculates the least common multiple of two
51 // Pre: A > 0 && B > 0
53 template <typename Integer
>
54 inline Integer
lcm(const Integer
& A
, const Integer
& B
)
62 template <typename Integer
>
63 inline Integer
log2_ceil(const Integer
& A
)
66 Integer power_of_2
= 1;
68 while(power_of_2
< A
){
75 template <typename Integer
>
76 inline Integer
upper_power_of_2(const Integer
& A
)
78 Integer power_of_2
= 1;
80 while(power_of_2
< A
){
86 //This function uses binary search to discover the
87 //highest set bit of the integer
88 inline std::size_t floor_log2 (std::size_t x
)
90 const std::size_t Bits
= sizeof(std::size_t)*CHAR_BIT
;
91 const bool Size_t_Bits_Power_2
= !(Bits
& (Bits
-1));
92 BOOST_STATIC_ASSERT(((Size_t_Bits_Power_2
)== true));
97 for(std::size_t shift
= Bits
>> 1; shift
; shift
>>= 1){
98 std::size_t tmp
= n
>> shift
;
100 log2
+= shift
, n
= tmp
;
106 } // namespace detail
107 } // namespace interprocess