Coverage Report

Created: 2026-06-09 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dovecot/src/lib/bits.c
Line
Count
Source
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
583k
#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
583k
{
17
583k
  if (unlikely(num > ((size_t)1 << (CHAR_BIT*sizeof(size_t) - 1))))
18
0
    i_panic("nearest_power(%zu): calculation would overflow", num);
19
20
583k
  if (num == 0)
21
0
    return 1;
22
23
583k
  return 1UL << BITS_REQUIRED(num - 1);
24
583k
}
25
26
#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
27
/* Lucky you, it's all inline intrinsics */
28
#else
29
unsigned int bits_required8(uint8_t num)
30
{
31
  int ret = 0;
32
  if (num > 0xf) { ret += 4; num >>= 4; }
33
  if (num > 0x3) { ret += 2; num >>= 2; }
34
  num &= ~(num>>1); /* 3->2, else unchanged */
35
  return ret + num;
36
}
37
#endif