From: Michal Schmidt Date: Tue, 14 Oct 2014 23:28:54 +0000 (+0200) Subject: util: add log2u(), log2u_round_up() X-Git-Tag: v218~652 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=b5de6d984296c9446ba0d2d32fd3248f453208aa util: add log2u(), log2u_round_up() Two's logarithms for unsigned. --- diff --git a/src/shared/util.h b/src/shared/util.h index aad9eb7fc..e405b02a8 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; }