math_functions.hpp 2.7 KB

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