/src/dovecot/src/lib/bits.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (c) 2001-2018 Dovecot authors, see the included COPYING file */ |
2 | | |
3 | | #include "lib.h" |
4 | | |
5 | | /* |
6 | | * We could use bits_required64() unconditionally, but that's unnecessary |
7 | | * and way more heavy weight on 32-bit systems. |
8 | | */ |
9 | | #if SIZEOF_SIZE_T > 4 |
10 | 3.67k | #define BITS_REQUIRED(x) bits_required64(x) |
11 | | #else |
12 | | #define BITS_REQUIRED(x) bits_required32(x) |
13 | | #endif |
14 | | |
15 | | size_t nearest_power(size_t num) |
16 | 3.67k | { |
17 | 3.67k | i_assert(num <= ((size_t)1 << (CHAR_BIT*sizeof(size_t) - 1))); |
18 | | |
19 | 3.67k | if (num == 0) |
20 | 0 | return 1; |
21 | | |
22 | 3.67k | return 1UL << BITS_REQUIRED(num - 1); |
23 | 3.67k | } |
24 | | |
25 | | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) |
26 | | /* Lucky you, it's all inline intrinsics */ |
27 | | #else |
28 | | unsigned int bits_required8(uint8_t num) |
29 | | { |
30 | | int ret = 0; |
31 | | if (num > 0xf) { ret += 4; num >>= 4; } |
32 | | if (num > 0x3) { ret += 2; num >>= 2; } |
33 | | num &= ~(num>>1); /* 3->2, else unchanged */ |
34 | | return ret + num; |
35 | | } |
36 | | #endif |