math_functions.hpp 2.5 KB

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