mirror of
https://github.com/boostorg/intrusive.git
synced 2025-08-05 15:34:35 +02:00
Added is_pow2/static_is_pow2/ceil_pow2/previous_or_equal_pow2
This commit is contained in:
@@ -23,6 +23,7 @@
|
|||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <climits>
|
#include <climits>
|
||||||
|
#include <boost/intrusive/detail/mpl.hpp>
|
||||||
|
|
||||||
namespace boost {
|
namespace boost {
|
||||||
namespace intrusive {
|
namespace intrusive {
|
||||||
@@ -227,9 +228,28 @@ inline float fast_log2 (float val)
|
|||||||
return val + static_cast<float>(log_2);
|
return val + static_cast<float>(log_2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline bool is_pow2(std::size_t x)
|
||||||
|
{ return (x & (x-1)) == 0; }
|
||||||
|
|
||||||
|
template<std::size_t N>
|
||||||
|
struct static_is_pow2
|
||||||
|
{
|
||||||
|
static const bool value = (N & (N-1)) == 0;
|
||||||
|
};
|
||||||
|
|
||||||
inline std::size_t ceil_log2 (std::size_t x)
|
inline std::size_t ceil_log2 (std::size_t x)
|
||||||
{
|
{
|
||||||
return static_cast<std::size_t>((x & (x-1)) != 0) + floor_log2(x);
|
return static_cast<std::size_t>(!(is_pow2)(x)) + floor_log2(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::size_t ceil_pow2 (std::size_t x)
|
||||||
|
{
|
||||||
|
return std::size_t(1u) << (ceil_log2)(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::size_t previous_or_equal_pow2(std::size_t x)
|
||||||
|
{
|
||||||
|
return std::size_t(1u) << floor_log2(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class SizeType, std::size_t N>
|
template<class SizeType, std::size_t N>
|
||||||
|
Reference in New Issue
Block a user