diff --git a/src/journal/journald-rate-limit.c b/src/journal/journald-rate-limit.c index 8bd68476a39..da3aed64eae 100644 --- a/src/journal/journald-rate-limit.c +++ b/src/journal/journald-rate-limit.c @@ -170,21 +170,6 @@ fail: return NULL; } -static uint64_t u64log2(uint64_t n) { - unsigned r; - - if (n <= 1) - return 0; - - r = 0; - for (;;) { - n = n >> 1; - if (!n) - return r; - r++; - } -} - static unsigned burst_modulate(unsigned burst, uint64_t available) { unsigned k; diff --git a/src/shared/util.h b/src/shared/util.h index 69a47653aa4..7c3da08dd88 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -635,3 +635,7 @@ static inline void _reset_umask_(struct umask_struct *s) { for (__attribute__((cleanup(_reset_umask_))) struct umask_struct _saved_umask_ = { umask(mask), false }; \ !_saved_umask_.quit ; \ _saved_umask_.quit = true) + +static inline unsigned u64log2(uint64_t n) { + return (n > 1) ? __builtin_clzll(n) ^ 63U : 0; +} diff --git a/src/test/test-util.c b/src/test/test-util.c index 08310c83ca4..eaf7e22d0fd 100644 --- a/src/test/test-util.c +++ b/src/test/test-util.c @@ -338,6 +338,16 @@ static void test_hostname_is_valid(void) { assert(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")); } +static void test_u64log2(void) { + assert(u64log2(0) == 0); + assert(u64log2(8) == 3); + assert(u64log2(9) == 3); + assert(u64log2(15) == 3); + assert(u64log2(16) == 4); + assert(u64log2(1024*1024) == 20); + assert(u64log2(1024*1024+5) == 20); +} + int main(int argc, char *argv[]) { test_streq_ptr(); test_first_word(); @@ -363,6 +373,7 @@ int main(int argc, char *argv[]) { test_memdup_multiply(); test_bus_path_escape(); test_hostname_is_valid(); + test_u64log2(); return 0; }