From e0a04cbc3d159a84a1aa845743a0b89981e16224 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Tue, 25 Feb 2025 15:25:08 -0700 Subject: libmisc: macro.h: Add LM_FLOORLOG2 --- libmisc/include/libmisc/macro.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'libmisc/include') diff --git a/libmisc/include/libmisc/macro.h b/libmisc/include/libmisc/macro.h index 149c4bb..b3e235c 100644 --- a/libmisc/include/libmisc/macro.h +++ b/libmisc/include/libmisc/macro.h @@ -17,10 +17,11 @@ /* numeric */ #define LM_ARRAY_LEN(ary) (sizeof(ary)/sizeof((ary)[0])) -#define LM_CEILDIV(n, d) ( ((n)+(d)-1) / (d) ) -#define LM_ROUND_UP(n, d) ( LM_CEILDIV(n, d) * (d) ) /** Return `n` rounded up to the nearest multiple of `d` */ -#define LM_ROUND_DOWN(n, d) ( ((n)/(d)) * (d) ) /** Return `n` rounded down to the nearest multiple of `d` */ -#define LM_NEXT_POWER_OF_2(x) ( (x) ? 1ULL<<((sizeof(unsigned long long)*8)-__builtin_clzll(x)) : 1) +#define LM_CEILDIV(n, d) ( ((n)+(d)-1) / (d) ) /** Return ceil(n/d) */ +#define LM_ROUND_UP(n, d) ( LM_CEILDIV(n, d) * (d) ) /** Return `n` rounded up to the nearest multiple of `d` */ +#define LM_ROUND_DOWN(n, d) ( ((n)/(d)) * (d) ) /** Return `n` rounded down to the nearest multiple of `d` */ +#define LM_NEXT_POWER_OF_2(x) ( (x) ? 1ULL<<((sizeof(unsigned long long)*8)-__builtin_clzll(x)) : 1) /** Return the lowest power of 2 that is > x */ +#define LM_FLOORLOG2(x) ((sizeof(unsigned long long)*8)-__builtin_clzll(x)-1) /** Return floor(log_2(x) */ /* strings */ -- cgit v1.2.3-2-g168b