diff options
author | Michal Schmidt <mschmidt@redhat.com> | 2014-10-15 01:28:54 +0200 |
---|---|---|
committer | Michal Schmidt <mschmidt@redhat.com> | 2014-10-30 19:50:50 +0100 |
commit | b5de6d984296c9446ba0d2d32fd3248f453208aa (patch) | |
tree | cc8c664f917009d8339cad6adab6e4e21aea3420 /src/shared/util.h | |
parent | a09abc4ae0bdc0200324eaa0416f23ff2170ec4e (diff) |
util: add log2u(), log2u_round_up()
Two's logarithms for unsigned.
Diffstat (limited to 'src/shared/util.h')
-rw-r--r-- | src/shared/util.h | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/shared/util.h b/src/shared/util.h index aad9eb7fcb..e405b02a81 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -830,6 +830,21 @@ static inline int log2i(int x) { return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1; } +static inline unsigned log2u(unsigned x) { + assert(x > 0); + + return sizeof(unsigned) * 8 - __builtin_clz(x) - 1; +} + +static inline unsigned log2u_round_up(unsigned x) { + assert(x > 0); + + if (x == 1) + return 0; + + return log2u(x - 1) + 1; +} + static inline bool logind_running(void) { return access("/run/systemd/seats/", F_OK) >= 0; } |