File indexing completed on 2025-01-18 09:38:26
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef BOOST_INTERPROCESS_DETAIL_MATH_FUNCTIONS_HPP
0017 #define BOOST_INTERPROCESS_DETAIL_MATH_FUNCTIONS_HPP
0018
0019 #ifndef BOOST_CONFIG_HPP
0020 # include <boost/config.hpp>
0021 #endif
0022 #
0023 #if defined(BOOST_HAS_PRAGMA_ONCE)
0024 # pragma once
0025 #endif
0026
0027 #include <climits>
0028 #include <boost/static_assert.hpp>
0029
0030 namespace boost {
0031 namespace interprocess {
0032 namespace ipcdetail {
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 template <typename Integer>
0043 inline Integer gcd(Integer A, Integer B)
0044 {
0045 do
0046 {
0047 const Integer tmp(B);
0048 B = A % B;
0049 A = tmp;
0050 } while (B != 0);
0051
0052 return A;
0053 }
0054
0055
0056
0057
0058
0059
0060
0061 template <typename Integer>
0062 inline Integer lcm(const Integer & A, const Integer & B)
0063 {
0064 Integer ret = A;
0065 ret /= gcd(A, B);
0066 ret *= B;
0067 return ret;
0068 }
0069
0070 template <typename Integer>
0071 inline Integer log2_ceil(const Integer & A)
0072 {
0073 Integer i = 0;
0074 Integer power_of_2 = 1;
0075
0076 while(power_of_2 < A){
0077 power_of_2 <<= 1;
0078 ++i;
0079 }
0080 return i;
0081 }
0082
0083 template <typename Integer>
0084 inline Integer upper_power_of_2(const Integer & A)
0085 {
0086 Integer power_of_2 = 1;
0087
0088 while(power_of_2 < A){
0089 power_of_2 <<= 1;
0090 }
0091 return power_of_2;
0092 }
0093
0094
0095
0096 inline std::size_t floor_log2 (std::size_t x)
0097 {
0098 const std::size_t Bits = sizeof(std::size_t)*CHAR_BIT;
0099 const bool Size_t_Bits_Power_2= !(Bits & (Bits-1));
0100 BOOST_STATIC_ASSERT(((Size_t_Bits_Power_2)== true));
0101
0102 std::size_t n = x;
0103 std::size_t log2 = 0;
0104
0105 for(std::size_t shift = Bits >> 1; shift; shift >>= 1){
0106 std::size_t tmp = n >> shift;
0107 if (tmp)
0108 log2 += shift, n = tmp;
0109 }
0110
0111 return log2;
0112 }
0113
0114 }
0115 }
0116 }
0117
0118 #endif