bits.h revision 62684181bd6a426052af1e3e6c7ec73fb51eb7ff
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody#ifndef BITS_H
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody#define BITS_H
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody/* default lib includes */
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody#ifdef HAVE_CONFIG_H
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody# include "config.h"
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody#endif
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody#include "macros.h"
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody#include <limits.h>
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody#include <stddef.h>
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody#include <stdint.h>
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody
cbc01fc844fa307e565cc81fd78536d7afca05a9Timo Sirainen#define UINT64_SUM_OVERFLOWS(a, b) \
cbc01fc844fa307e565cc81fd78536d7afca05a9Timo Sirainen (a > (uint64_t)-1 - b)
cbc01fc844fa307e565cc81fd78536d7afca05a9Timo Sirainen
db10156822c596c5e953b0ce9b10fc3732030ba8Phil Carmody#define BIT(n) (1u << (n))
db10156822c596c5e953b0ce9b10fc3732030ba8Phil Carmody
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmodysize_t nearest_power(size_t num) ATTR_CONST;
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmodyunsigned int bits_required8(uint8_t num) ATTR_CONST;
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmodystatic inline
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmodyunsigned int bits_required16(uint16_t num)
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody{
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody return (num <= 0xff) ? bits_required8(num)
62684181bd6a426052af1e3e6c7ec73fb51eb7ffPhil Carmody : 8 + bits_required8(num >> 8);
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody}
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmodystatic inline
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmodyunsigned int bits_required32(uint32_t num)
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody{
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody return (num <= 0xffff) ? bits_required16(num)
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody : 16 + bits_required16(num >> 16);
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody}
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmodystatic inline
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmodyunsigned int bits_required64(uint64_t num)
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody{
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody return (num <= 0xffffffff) ? bits_required32(num)
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody : 32 + bits_required32(num >> 32);
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody}
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody
3637a2ad3b2ead17efd5591f73effd3b140d8d2dPhil Carmody#endif