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