From 6c537290ad1df8469b54dee80079542c171fac1a Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Fri, 4 Jun 2021 14:45:37 +0000 Subject: Move roundUp2inN to vespalib. --- vespalib/src/vespa/vespalib/util/alloc.h | 12 ++++++++++-- vespalib/src/vespa/vespalib/util/optimized.h | 6 +++--- 2 files changed, 13 insertions(+), 5 deletions(-) (limited to 'vespalib/src') diff --git a/vespalib/src/vespa/vespalib/util/alloc.h b/vespalib/src/vespa/vespalib/util/alloc.h index f608a244035..a97e8c9f25e 100644 --- a/vespalib/src/vespa/vespalib/util/alloc.h +++ b/vespalib/src/vespa/vespalib/util/alloc.h @@ -102,13 +102,21 @@ private: namespace vespalib { /// Rounds up to the closest number that is a power of 2 -inline size_t roundUp2inN(size_t minimum) { +inline size_t +roundUp2inN(size_t minimum) { return 2ul << Optimized::msbIdx(minimum - 1); } /// Rounds minElems up to the closest number where minElems*elemSize is a power of 2 -inline size_t roundUp2inN(size_t minElems, size_t elemSize) { +inline size_t +roundUp2inN(size_t minElems, size_t elemSize) { return roundUp2inN(minElems * elemSize)/elemSize; } +template +size_t +roundUp2inN(size_t elems) { + return roundUp2inN(elems, sizeof(T)); +} + } diff --git a/vespalib/src/vespa/vespalib/util/optimized.h b/vespalib/src/vespa/vespalib/util/optimized.h index 92cf1f0ca24..6c6d1b12a71 100644 --- a/vespalib/src/vespa/vespalib/util/optimized.h +++ b/vespalib/src/vespa/vespalib/util/optimized.h @@ -22,9 +22,9 @@ public: static int lsbIdx(unsigned int v); static int lsbIdx(unsigned long v); static int lsbIdx(unsigned long long v); - static int popCount(unsigned int v) { return __builtin_popcount(v); } - static int popCount(unsigned long v) { return __builtin_popcountl(v); } - static int popCount(unsigned long long v) { return __builtin_popcountll(v); } + static constexpr int popCount(unsigned int v) { return __builtin_popcount(v); } + static constexpr int popCount(unsigned long v) { return __builtin_popcountl(v); } + static constexpr int popCount(unsigned long long v) { return __builtin_popcountll(v); } }; /** -- cgit v1.2.3