askpass.c \
backtrace-string.c \
base64.c \
+ bits.c \
bsearch-insert-pos.c \
buffer.c \
child-wait.c \
askpass.h \
backtrace-string.h \
base64.h \
+ bits.h \
bsearch-insert-pos.h \
buffer.h \
child-wait.h \
--- /dev/null
+/* Copyright (c) 2001-2014 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+
+size_t nearest_power(size_t num)
+{
+ size_t n = 1;
+
+ i_assert(num <= ((size_t)1 << (CHAR_BIT*sizeof(size_t) - 1)));
+
+ while (n < num) n <<= 1;
+ return n;
+}
+
+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;
+}
--- /dev/null
+#ifndef BITS_H
+#define BITS_H
+
+/* default lib includes */
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "macros.h"
+
+#include <limits.h>
+#include <stddef.h>
+#include <stdint.h>
+
+size_t nearest_power(size_t num) ATTR_CONST;
+
+unsigned int bits_required8(uint8_t num) ATTR_CONST;
+
+static inline
+unsigned int bits_required16(uint16_t num)
+{
+ return (num <= 0xff) ? bits_required8(num)
+ : 8 + bits_required16(num >> 8);
+}
+static inline
+unsigned int bits_required32(uint32_t num)
+{
+ return (num <= 0xffff) ? bits_required16(num)
+ : 16 + bits_required16(num >> 16);
+}
+static inline
+unsigned int bits_required64(uint64_t num)
+{
+ return (num <= 0xffffffff) ? bits_required32(num)
+ : 32 + bits_required32(num >> 32);
+}
+
+#endif
static ARRAY(lib_atexit_callback_t *) atexit_callbacks = ARRAY_INIT;
-size_t nearest_power(size_t num)
-{
- size_t n = 1;
-
- i_assert(num <= ((size_t)1 << (CHAR_BIT*sizeof(size_t) - 1)));
-
- while (n < num) n <<= 1;
- return n;
-}
-
int close_keep_errno(int *fd)
{
int ret, old_errno = errno;
typedef void lib_atexit_callback_t(void);
#include "array-decl.h" /* ARRAY*()s may exist in any header */
+#include "bits.h"
#include "hash-decl.h" /* HASH_TABLE*()s may exist in any header */
#include "strfuncs.h"
#include "strnum.h"
-size_t nearest_power(size_t num) ATTR_CONST;
int close_keep_errno(int *fd);
/* Call the given callback at the beginning of lib_deinit(). The main