diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2023-04-03 14:32:39 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2023-04-24 10:02:30 +0200 |
commit | a12bc99ef0fc04fa48767c891f7a6db6404e51d5 (patch) | |
tree | f5840c3081c8aea6cddd557635313a2ab2d426a4 /src/basic | |
parent | 6eccc3cfa9dcfea3c8b508a66d2d592e6b9fcb93 (diff) | |
download | systemd-a12bc99ef0fc04fa48767c891f7a6db6404e51d5.tar.gz |
basic/logarithm: add popcount() wrapper
__builtin_popcount() is a bit of a mouthful, so let's provide a helper.
Using _Generic has the advantage that if a type other then the ones on
the list is given, compilation will fail. This is nice, because if by any
change we pass a wider type, it is rejected immediately instead of being
truncated.
log.h is also needed. It is included transitively, but let's include it
directly.
macro.h is *not* needed.
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/logarithm.h | 53 |
1 files changed, 0 insertions, 53 deletions
diff --git a/src/basic/logarithm.h b/src/basic/logarithm.h deleted file mode 100644 index 646f2d3613..0000000000 --- a/src/basic/logarithm.h +++ /dev/null @@ -1,53 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later */ -#pragma once - -#include <stdint.h> - -#include "macro.h" - -/* Note: log2(0) == log2(1) == 0 here and below. */ - -#define CONST_LOG2ULL(x) ((x) > 1 ? (unsigned) __builtin_clzll(x) ^ 63U : 0) -#define NONCONST_LOG2ULL(x) ({ \ - unsigned long long _x = (x); \ - _x > 1 ? (unsigned) __builtin_clzll(_x) ^ 63U : 0; \ - }) -#define LOG2ULL(x) __builtin_choose_expr(__builtin_constant_p(x), CONST_LOG2ULL(x), NONCONST_LOG2ULL(x)) - -static inline unsigned log2u64(uint64_t x) { -#if __SIZEOF_LONG_LONG__ == 8 - return LOG2ULL(x); -#else -# error "Wut?" -#endif -} - -static inline unsigned u32ctz(uint32_t n) { -#if __SIZEOF_INT__ == 4 - return n != 0 ? __builtin_ctz(n) : 32; -#else -# error "Wut?" -#endif -} - -#define CONST_LOG2U(x) ((x) > 1 ? __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1 : 0) -#define NONCONST_LOG2U(x) ({ \ - unsigned _x = (x); \ - _x > 1 ? __SIZEOF_INT__ * 8 - __builtin_clz(_x) - 1 : 0; \ - }) -#define LOG2U(x) __builtin_choose_expr(__builtin_constant_p(x), CONST_LOG2U(x), NONCONST_LOG2U(x)) - -static inline unsigned log2i(int x) { - return LOG2U(x); -} - -static inline unsigned log2u(unsigned x) { - return LOG2U(x); -} - -static inline unsigned log2u_round_up(unsigned x) { - if (x <= 1) - return 0; - - return log2u(x - 1) + 1; -} |