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@101
|
19 #ifndef BOOST_CONFIG_HPP
|
Chris@101
|
20 # include <boost/config.hpp>
|
Chris@101
|
21 #endif
|
Chris@101
|
22 #
|
Chris@101
|
23 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@101
|
24 # pragma once
|
Chris@101
|
25 #endif
|
Chris@101
|
26
|
Chris@16
|
27 #include <climits>
|
Chris@16
|
28 #include <boost/static_assert.hpp>
|
Chris@16
|
29
|
Chris@16
|
30 namespace boost {
|
Chris@16
|
31 namespace interprocess {
|
Chris@16
|
32 namespace ipcdetail {
|
Chris@16
|
33
|
Chris@16
|
34 // Greatest common divisor and least common multiple
|
Chris@16
|
35
|
Chris@16
|
36 //
|
Chris@16
|
37 // gcd is an algorithm that calculates the greatest common divisor of two
|
Chris@16
|
38 // integers, using Euclid's algorithm.
|
Chris@16
|
39 //
|
Chris@16
|
40 // Pre: A > 0 && B > 0
|
Chris@16
|
41 // Recommended: A > B
|
Chris@16
|
42 template <typename Integer>
|
Chris@16
|
43 inline Integer gcd(Integer A, Integer B)
|
Chris@16
|
44 {
|
Chris@16
|
45 do
|
Chris@16
|
46 {
|
Chris@16
|
47 const Integer tmp(B);
|
Chris@16
|
48 B = A % B;
|
Chris@16
|
49 A = tmp;
|
Chris@16
|
50 } while (B != 0);
|
Chris@16
|
51
|
Chris@16
|
52 return A;
|
Chris@16
|
53 }
|
Chris@16
|
54
|
Chris@16
|
55 //
|
Chris@16
|
56 // lcm is an algorithm that calculates the least common multiple of two
|
Chris@16
|
57 // integers.
|
Chris@16
|
58 //
|
Chris@16
|
59 // Pre: A > 0 && B > 0
|
Chris@16
|
60 // Recommended: A > B
|
Chris@16
|
61 template <typename Integer>
|
Chris@16
|
62 inline Integer lcm(const Integer & A, const Integer & B)
|
Chris@16
|
63 {
|
Chris@16
|
64 Integer ret = A;
|
Chris@16
|
65 ret /= gcd(A, B);
|
Chris@16
|
66 ret *= B;
|
Chris@16
|
67 return ret;
|
Chris@16
|
68 }
|
Chris@16
|
69
|
Chris@16
|
70 template <typename Integer>
|
Chris@16
|
71 inline Integer log2_ceil(const Integer & A)
|
Chris@16
|
72 {
|
Chris@16
|
73 Integer i = 0;
|
Chris@16
|
74 Integer power_of_2 = 1;
|
Chris@16
|
75
|
Chris@16
|
76 while(power_of_2 < A){
|
Chris@16
|
77 power_of_2 <<= 1;
|
Chris@16
|
78 ++i;
|
Chris@16
|
79 }
|
Chris@16
|
80 return i;
|
Chris@16
|
81 }
|
Chris@16
|
82
|
Chris@16
|
83 template <typename Integer>
|
Chris@16
|
84 inline Integer upper_power_of_2(const Integer & A)
|
Chris@16
|
85 {
|
Chris@16
|
86 Integer power_of_2 = 1;
|
Chris@16
|
87
|
Chris@16
|
88 while(power_of_2 < A){
|
Chris@16
|
89 power_of_2 <<= 1;
|
Chris@16
|
90 }
|
Chris@16
|
91 return power_of_2;
|
Chris@16
|
92 }
|
Chris@16
|
93
|
Chris@16
|
94 //This function uses binary search to discover the
|
Chris@16
|
95 //highest set bit of the integer
|
Chris@16
|
96 inline std::size_t floor_log2 (std::size_t x)
|
Chris@16
|
97 {
|
Chris@16
|
98 const std::size_t Bits = sizeof(std::size_t)*CHAR_BIT;
|
Chris@16
|
99 const bool Size_t_Bits_Power_2= !(Bits & (Bits-1));
|
Chris@16
|
100 BOOST_STATIC_ASSERT(((Size_t_Bits_Power_2)== true));
|
Chris@16
|
101
|
Chris@16
|
102 std::size_t n = x;
|
Chris@16
|
103 std::size_t log2 = 0;
|
Chris@16
|
104
|
Chris@16
|
105 for(std::size_t shift = Bits >> 1; shift; shift >>= 1){
|
Chris@16
|
106 std::size_t tmp = n >> shift;
|
Chris@16
|
107 if (tmp)
|
Chris@16
|
108 log2 += shift, n = tmp;
|
Chris@16
|
109 }
|
Chris@16
|
110
|
Chris@16
|
111 return log2;
|
Chris@16
|
112 }
|
Chris@16
|
113
|
Chris@16
|
114 } // namespace ipcdetail
|
Chris@16
|
115 } // namespace interprocess
|
Chris@16
|
116 } // namespace boost
|
Chris@16
|
117
|
Chris@16
|
118 #endif
|