Chris@16
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // (C) Copyright Stephen Cleary 2000.
|
Chris@16
|
4 // (C) Copyright Ion Gaztanaga 2007-2012.
|
Chris@16
|
5 //
|
Chris@16
|
6 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
7 // (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
8 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10 // See http://www.boost.org/libs/interprocess for documentation.
|
Chris@16
|
11 //
|
Chris@16
|
12 // This file is a slightly modified file from Boost.Pool
|
Chris@16
|
13 //
|
Chris@16
|
14 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
15
|
Chris@16
|
16 #ifndef BOOST_INTERPROCESS_DETAIL_MATH_FUNCTIONS_HPP
|
Chris@16
|
17 #define BOOST_INTERPROCESS_DETAIL_MATH_FUNCTIONS_HPP
|
Chris@16
|
18
|
Chris@16
|
19 #include <climits>
|
Chris@16
|
20 #include <boost/static_assert.hpp>
|
Chris@16
|
21
|
Chris@16
|
22 namespace boost {
|
Chris@16
|
23 namespace interprocess {
|
Chris@16
|
24 namespace ipcdetail {
|
Chris@16
|
25
|
Chris@16
|
26 // Greatest common divisor and least common multiple
|
Chris@16
|
27
|
Chris@16
|
28 //
|
Chris@16
|
29 // gcd is an algorithm that calculates the greatest common divisor of two
|
Chris@16
|
30 // integers, using Euclid's algorithm.
|
Chris@16
|
31 //
|
Chris@16
|
32 // Pre: A > 0 && B > 0
|
Chris@16
|
33 // Recommended: A > B
|
Chris@16
|
34 template <typename Integer>
|
Chris@16
|
35 inline Integer gcd(Integer A, Integer B)
|
Chris@16
|
36 {
|
Chris@16
|
37 do
|
Chris@16
|
38 {
|
Chris@16
|
39 const Integer tmp(B);
|
Chris@16
|
40 B = A % B;
|
Chris@16
|
41 A = tmp;
|
Chris@16
|
42 } while (B != 0);
|
Chris@16
|
43
|
Chris@16
|
44 return A;
|
Chris@16
|
45 }
|
Chris@16
|
46
|
Chris@16
|
47 //
|
Chris@16
|
48 // lcm is an algorithm that calculates the least common multiple of two
|
Chris@16
|
49 // integers.
|
Chris@16
|
50 //
|
Chris@16
|
51 // Pre: A > 0 && B > 0
|
Chris@16
|
52 // Recommended: A > B
|
Chris@16
|
53 template <typename Integer>
|
Chris@16
|
54 inline Integer lcm(const Integer & A, const Integer & B)
|
Chris@16
|
55 {
|
Chris@16
|
56 Integer ret = A;
|
Chris@16
|
57 ret /= gcd(A, B);
|
Chris@16
|
58 ret *= B;
|
Chris@16
|
59 return ret;
|
Chris@16
|
60 }
|
Chris@16
|
61
|
Chris@16
|
62 template <typename Integer>
|
Chris@16
|
63 inline Integer log2_ceil(const Integer & A)
|
Chris@16
|
64 {
|
Chris@16
|
65 Integer i = 0;
|
Chris@16
|
66 Integer power_of_2 = 1;
|
Chris@16
|
67
|
Chris@16
|
68 while(power_of_2 < A){
|
Chris@16
|
69 power_of_2 <<= 1;
|
Chris@16
|
70 ++i;
|
Chris@16
|
71 }
|
Chris@16
|
72 return i;
|
Chris@16
|
73 }
|
Chris@16
|
74
|
Chris@16
|
75 template <typename Integer>
|
Chris@16
|
76 inline Integer upper_power_of_2(const Integer & A)
|
Chris@16
|
77 {
|
Chris@16
|
78 Integer power_of_2 = 1;
|
Chris@16
|
79
|
Chris@16
|
80 while(power_of_2 < A){
|
Chris@16
|
81 power_of_2 <<= 1;
|
Chris@16
|
82 }
|
Chris@16
|
83 return power_of_2;
|
Chris@16
|
84 }
|
Chris@16
|
85
|
Chris@16
|
86 //This function uses binary search to discover the
|
Chris@16
|
87 //highest set bit of the integer
|
Chris@16
|
88 inline std::size_t floor_log2 (std::size_t x)
|
Chris@16
|
89 {
|
Chris@16
|
90 const std::size_t Bits = sizeof(std::size_t)*CHAR_BIT;
|
Chris@16
|
91 const bool Size_t_Bits_Power_2= !(Bits & (Bits-1));
|
Chris@16
|
92 BOOST_STATIC_ASSERT(((Size_t_Bits_Power_2)== true));
|
Chris@16
|
93
|
Chris@16
|
94 std::size_t n = x;
|
Chris@16
|
95 std::size_t log2 = 0;
|
Chris@16
|
96
|
Chris@16
|
97 for(std::size_t shift = Bits >> 1; shift; shift >>= 1){
|
Chris@16
|
98 std::size_t tmp = n >> shift;
|
Chris@16
|
99 if (tmp)
|
Chris@16
|
100 log2 += shift, n = tmp;
|
Chris@16
|
101 }
|
Chris@16
|
102
|
Chris@16
|
103 return log2;
|
Chris@16
|
104 }
|
Chris@16
|
105
|
Chris@16
|
106 } // namespace ipcdetail
|
Chris@16
|
107 } // namespace interprocess
|
Chris@16
|
108 } // namespace boost
|
Chris@16
|
109
|
Chris@16
|
110 #endif
|