From d5c844f18d1dd811877d4837ad306f4839483158 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Sat, 22 Oct 2016 20:44:18 -0400 Subject: [PATCH] added LOG2_CEIL --- include/villas/utils.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/include/villas/utils.h b/include/villas/utils.h index 87f2a6c69..4207d3626 100644 --- a/include/villas/utils.h +++ b/include/villas/utils.h @@ -55,7 +55,11 @@ #define ALIGN_MASK(x, m) (((uintptr_t) (x) + (m)) & ~(m)) #define IS_ALIGNED(x, a) (ALIGN(x, a) == (uintptr_t) x) -#define CEIL(x, y) ((x + y - 1) / y) +/** Round-up integer division */ +#define CEIL(x, y) (((x) + (y) - 1) / (y)) + +/** Get nearest up-rounded power of 2 */ +#define LOG2_CEIL(x) (1 << log2i((x) - 1) + 1) /** Calculate the number of elements in an array. */ #define ARRAY_LEN(a) ( sizeof (a) / sizeof (a)[0] ) @@ -212,6 +216,13 @@ __attribute__((always_inline)) static inline uint64_t rdtsc() return tsc; } +/** Get log2 of long long integers */ +static inline int log2i(long long x) { + assert(x > 0); + + return sizeof(x) * 8 - __builtin_clzll(x) - 1; +} + /** Sleep with rdtsc */ void rdtsc_sleep(uint64_t nanosecs, uint64_t start);