Update to use a more efficient power of 2 check. (#274)
This commit is contained in:
committed by
GitHub
parent
db3580a9d8
commit
a3660c4069
@@ -49,7 +49,7 @@ namespace snmalloc
|
||||
template<size_t alignment>
|
||||
static inline bool is_aligned_block(void* p, size_t size)
|
||||
{
|
||||
static_assert(bits::next_pow2_const(alignment) == alignment);
|
||||
static_assert(bits::is_pow2(alignment));
|
||||
|
||||
return ((address_cast(p) | size) & (alignment - 1)) == 0;
|
||||
}
|
||||
@@ -62,7 +62,7 @@ namespace snmalloc
|
||||
SNMALLOC_FAST_PATH T* pointer_align_down(void* p)
|
||||
{
|
||||
static_assert(alignment > 0);
|
||||
static_assert(bits::next_pow2_const(alignment) == alignment);
|
||||
static_assert(bits::is_pow2(alignment));
|
||||
if constexpr (alignment == 1)
|
||||
return static_cast<T*>(p);
|
||||
else
|
||||
@@ -84,7 +84,7 @@ namespace snmalloc
|
||||
inline T* pointer_align_up(void* p)
|
||||
{
|
||||
static_assert(alignment > 0);
|
||||
static_assert(bits::next_pow2_const(alignment) == alignment);
|
||||
static_assert(bits::is_pow2(alignment));
|
||||
if constexpr (alignment == 1)
|
||||
return static_cast<T*>(p);
|
||||
else
|
||||
@@ -106,7 +106,7 @@ namespace snmalloc
|
||||
SNMALLOC_FAST_PATH T* pointer_align_down(void* p, size_t alignment)
|
||||
{
|
||||
SNMALLOC_ASSERT(alignment > 0);
|
||||
SNMALLOC_ASSERT(bits::next_pow2(alignment) == alignment);
|
||||
SNMALLOC_ASSERT(bits::is_pow2(alignment));
|
||||
#if __has_builtin(__builtin_align_down)
|
||||
return static_cast<T*>(__builtin_align_down(p, alignment));
|
||||
#else
|
||||
@@ -123,7 +123,7 @@ namespace snmalloc
|
||||
inline T* pointer_align_up(void* p, size_t alignment)
|
||||
{
|
||||
SNMALLOC_ASSERT(alignment > 0);
|
||||
SNMALLOC_ASSERT(bits::next_pow2(alignment) == alignment);
|
||||
SNMALLOC_ASSERT(bits::is_pow2(alignment));
|
||||
#if __has_builtin(__builtin_align_up)
|
||||
return static_cast<T*>(__builtin_align_up(p, alignment));
|
||||
#else
|
||||
|
||||
@@ -211,6 +211,11 @@ namespace snmalloc
|
||||
#endif
|
||||
}
|
||||
|
||||
constexpr SNMALLOC_FAST_PATH bool is_pow2(size_t x)
|
||||
{
|
||||
return (x & (x - 1)) == 0;
|
||||
}
|
||||
|
||||
SNMALLOC_FAST_PATH size_t next_pow2(size_t x)
|
||||
{
|
||||
// Correct for numbers [0..MAX_SIZE >> 1).
|
||||
@@ -244,7 +249,7 @@ namespace snmalloc
|
||||
constexpr SNMALLOC_FAST_PATH size_t
|
||||
align_down(size_t value, size_t alignment)
|
||||
{
|
||||
SNMALLOC_ASSERT(next_pow2_const(alignment) == alignment);
|
||||
SNMALLOC_ASSERT(is_pow2(alignment));
|
||||
|
||||
size_t align_1 = alignment - 1;
|
||||
value &= ~align_1;
|
||||
@@ -253,7 +258,7 @@ namespace snmalloc
|
||||
|
||||
constexpr SNMALLOC_FAST_PATH size_t align_up(size_t value, size_t alignment)
|
||||
{
|
||||
SNMALLOC_ASSERT(next_pow2_const(alignment) == alignment);
|
||||
SNMALLOC_ASSERT(is_pow2(alignment));
|
||||
|
||||
size_t align_1 = alignment - 1;
|
||||
value += align_1;
|
||||
|
||||
@@ -52,8 +52,7 @@ namespace snmalloc
|
||||
template<size_t length, typename T>
|
||||
class Mod
|
||||
{
|
||||
static_assert(
|
||||
length == bits::next_pow2_const(length), "Must be a power of two.");
|
||||
static_assert(bits::is_pow2(length), "Must be a power of two.");
|
||||
|
||||
private:
|
||||
T value = 0;
|
||||
|
||||
Reference in New Issue
Block a user