diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 09:51:24 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 09:51:24 +0000 |
commit | f7548d6d28c313cf80e6f3ef89aed16a19815df1 (patch) | |
tree | a3f6f2a3f247293bee59ecd28e8cd8ceb6ca064a /src/lib/bits.c | |
parent | Initial commit. (diff) | |
download | dovecot-f7548d6d28c313cf80e6f3ef89aed16a19815df1.tar.xz dovecot-f7548d6d28c313cf80e6f3ef89aed16a19815df1.zip |
Adding upstream version 1:2.3.19.1+dfsg1.upstream/1%2.3.19.1+dfsg1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/lib/bits.c | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/lib/bits.c b/src/lib/bits.c new file mode 100644 index 0000000..6366cf2 --- /dev/null +++ b/src/lib/bits.c @@ -0,0 +1,36 @@ +/* Copyright (c) 2001-2018 Dovecot authors, see the included COPYING file */ + +#include "lib.h" + +/* + * We could use bits_required64() unconditionally, but that's unnecessary + * and way more heavy weight on 32-bit systems. + */ +#ifdef _LP64 +#define BITS_REQUIRED(x) bits_required64(x) +#else +#define BITS_REQUIRED(x) bits_required32(x) +#endif + +size_t nearest_power(size_t num) +{ + i_assert(num <= ((size_t)1 << (CHAR_BIT*sizeof(size_t) - 1))); + + if (num == 0) + return 1; + + return 1UL << BITS_REQUIRED(num - 1); +} + +#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) +/* Lucky you, it's all inline intrinsics */ +#else +unsigned int bits_required8(uint8_t num) +{ + int ret = 0; + if (num > 0xf) { ret += 4; num >>= 4; } + if (num > 0x3) { ret += 2; num >>= 2; } + num &= ~(num>>1); /* 3->2, else unchanged */ + return ret + num; +} +#endif |