math_functions.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Stephen Cleary 2000.
  4. // (C) Copyright Ion Gaztanaga 2007-2013.
  5. //
  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)
  9. //
  10. // See http://www.boost.org/libs/container for documentation.
  11. //
  12. // This file is a slightly modified file from Boost.Pool
  13. //
  14. //////////////////////////////////////////////////////////////////////////////
  15. #ifndef BOOST_CONTAINER_DETAIL_MATH_FUNCTIONS_HPP
  16. #define BOOST_CONTAINER_DETAIL_MATH_FUNCTIONS_HPP
  17. #ifndef BOOST_CONFIG_HPP
  18. # include <boost/config.hpp>
  19. #endif
  20. #if defined(BOOST_HAS_PRAGMA_ONCE)
  21. # pragma once
  22. #endif
  23. #include <boost/container/detail/config_begin.hpp>
  24. #include <boost/container/detail/workaround.hpp>
  25. #include <climits>
  26. namespace boost {
  27. namespace container {
  28. namespace dtl {
  29. // Greatest common divisor and least common multiple
  30. //
  31. // gcd is an algorithm that calculates the greatest common divisor of two
  32. // integers, using Euclid's algorithm.
  33. //
  34. // Pre: A > 0 && B > 0
  35. // Recommended: A > B
  36. template <typename Integer>
  37. inline Integer gcd(Integer A, Integer B)
  38. {
  39. do
  40. {
  41. const Integer tmp(B);
  42. B = A % B;
  43. A = tmp;
  44. } while (B != 0);
  45. return A;
  46. }
  47. //
  48. // lcm is an algorithm that calculates the least common multiple of two
  49. // integers.
  50. //
  51. // Pre: A > 0 && B > 0
  52. // Recommended: A > B
  53. template <typename Integer>
  54. inline Integer lcm(const Integer & A, const Integer & B)
  55. {
  56. Integer ret = A;
  57. ret /= gcd(A, B);
  58. ret *= B;
  59. return ret;
  60. }
  61. template <typename Integer>
  62. inline Integer log2_ceil(const Integer & A)
  63. {
  64. Integer i = 0;
  65. Integer power_of_2 = 1;
  66. while(power_of_2 < A){
  67. power_of_2 <<= 1;
  68. ++i;
  69. }
  70. return i;
  71. }
  72. template <typename Integer>
  73. inline Integer upper_power_of_2(const Integer & A)
  74. {
  75. Integer power_of_2 = 1;
  76. while(power_of_2 < A){
  77. power_of_2 <<= 1;
  78. }
  79. return power_of_2;
  80. }
  81. template <typename Integer, bool Loop = true>
  82. struct upper_power_of_2_loop_ct
  83. {
  84. template <Integer I, Integer P>
  85. struct apply
  86. {
  87. static const Integer value =
  88. upper_power_of_2_loop_ct<Integer, (I > P*2)>::template apply<I, P*2>::value;
  89. };
  90. };
  91. template <typename Integer>
  92. struct upper_power_of_2_loop_ct<Integer, false>
  93. {
  94. template <Integer I, Integer P>
  95. struct apply
  96. {
  97. static const Integer value = P;
  98. };
  99. };
  100. template <typename Integer, Integer I>
  101. struct upper_power_of_2_ct
  102. {
  103. static const Integer value = upper_power_of_2_loop_ct<Integer, (I > 1)>::template apply<I, 2>::value;
  104. };
  105. //This function uses binary search to discover the
  106. //highest set bit of the integer
  107. inline std::size_t floor_log2 (std::size_t x)
  108. {
  109. const std::size_t Bits = sizeof(std::size_t)*CHAR_BIT;
  110. const bool Size_t_Bits_Power_2= !(Bits & (Bits-1));
  111. BOOST_CONTAINER_STATIC_ASSERT(((Size_t_Bits_Power_2)== true));
  112. std::size_t n = x;
  113. std::size_t log2 = 0;
  114. for(std::size_t shift = Bits >> 1; shift; shift >>= 1){
  115. std::size_t tmp = n >> shift;
  116. if (tmp)
  117. log2 += shift, n = tmp;
  118. }
  119. return log2;
  120. }
  121. template<std::size_t I1, std::size_t I2>
  122. struct gcd_ct
  123. {
  124. static const std::size_t Max = I1 > I2 ? I1 : I2;
  125. static const std::size_t Min = I1 < I2 ? I1 : I2;
  126. static const std::size_t value = gcd_ct<Min, Max % Min>::value;
  127. };
  128. template<std::size_t I1>
  129. struct gcd_ct<I1, 0>
  130. {
  131. static const std::size_t value = I1;
  132. };
  133. template<std::size_t I1>
  134. struct gcd_ct<0, I1>
  135. {
  136. static const std::size_t value = I1;
  137. };
  138. template<std::size_t I1, std::size_t I2>
  139. struct lcm_ct
  140. {
  141. static const std::size_t value = I1 * I2 / gcd_ct<I1, I2>::value;
  142. };
  143. } // namespace dtl
  144. } // namespace container
  145. } // namespace boost
  146. #include <boost/container/detail/config_end.hpp>
  147. #endif