Update to use a more efficient power of 2 check. (#274)

This commit is contained in:
Matthew Parkinson
2021-01-27 11:58:41 +00:00
committed by GitHub
parent db3580a9d8
commit a3660c4069
10 changed files with 21 additions and 18 deletions

View File

@@ -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;