blob: ab4cf77336299646b6a7ee38fcce913d517eda39 [file] [log] [blame]
Harald Welte2230c132011-01-19 10:10:16 +01001#ifndef _OSMO_BITS_H
2#define _OSMO_BITS_H
3
4#include <stdint.h>
5
Harald Welteba6988b2011-08-17 12:46:48 +02006/*! \defgroup bits soft, unpacked and packed bits
7 * @{
8 */
9
Harald Weltebd598e32011-08-16 23:26:52 +020010/*! \file bits.h
11 * \brief Osmocom bit level support code
12 */
13
14typedef int8_t sbit_t; /*!< \brief soft bit (-127...127) */
15typedef uint8_t ubit_t; /*!< \brief unpacked bit (0 or 1) */
16typedef uint8_t pbit_t; /*!< \brief packed bis (8 bits in a byte) */
Harald Welte2230c132011-01-19 10:10:16 +010017
Christian Vogelc7f84e92011-01-22 22:48:37 +010018/*
19 NOTE on the endianess of pbit_t:
20 Bits in a pbit_t are ordered MSB first, i.e. 0x80 is the first bit.
21 Bit i in a pbit_t array is array[i/8] & (1<<(7-i%8))
22*/
23
Harald Weltebd598e32011-08-16 23:26:52 +020024/*! \brief determine how many bytes we would need for \a num_bits packed bits
25 * \param[in] num_bits Number of packed bits
26 */
Harald Welte2230c132011-01-19 10:10:16 +010027static inline unsigned int osmo_pbit_bytesize(unsigned int num_bits)
28{
29 unsigned int pbit_bytesize = num_bits / 8;
30
31 if (num_bits % 8)
32 pbit_bytesize++;
33
34 return pbit_bytesize;
35}
36
Harald Welte2230c132011-01-19 10:10:16 +010037int osmo_ubit2pbit(pbit_t *out, const ubit_t *in, unsigned int num_bits);
38
Harald Welte2230c132011-01-19 10:10:16 +010039int osmo_pbit2ubit(ubit_t *out, const pbit_t *in, unsigned int num_bits);
40
Sylvain Munautaeb10772011-01-21 12:22:30 +010041int osmo_ubit2pbit_ext(pbit_t *out, unsigned int out_ofs,
42 const ubit_t *in, unsigned int in_ofs,
43 unsigned int num_bits, int lsb_mode);
44
Sylvain Munautaeb10772011-01-21 12:22:30 +010045int osmo_pbit2ubit_ext(ubit_t *out, unsigned int out_ofs,
46 const pbit_t *in, unsigned int in_ofs,
47 unsigned int num_bits, int lsb_mode);
48
Harald Welteba6988b2011-08-17 12:46:48 +020049/*! }@ */
50
51#endif /* _OSMO_BITS_H */