blob: 9f8e1fb58650e2f37d573d001e7a891c37593b99 [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 Weltebd598e32011-08-16 23:26:52 +02006/*! \file bits.h
7 * \brief Osmocom bit level support code
8 */
9
10typedef int8_t sbit_t; /*!< \brief soft bit (-127...127) */
11typedef uint8_t ubit_t; /*!< \brief unpacked bit (0 or 1) */
12typedef uint8_t pbit_t; /*!< \brief packed bis (8 bits in a byte) */
Harald Welte2230c132011-01-19 10:10:16 +010013
Christian Vogelc7f84e92011-01-22 22:48:37 +010014/*
15 NOTE on the endianess of pbit_t:
16 Bits in a pbit_t are ordered MSB first, i.e. 0x80 is the first bit.
17 Bit i in a pbit_t array is array[i/8] & (1<<(7-i%8))
18*/
19
Harald Weltebd598e32011-08-16 23:26:52 +020020/*! \brief determine how many bytes we would need for \a num_bits packed bits
21 * \param[in] num_bits Number of packed bits
22 */
Harald Welte2230c132011-01-19 10:10:16 +010023static inline unsigned int osmo_pbit_bytesize(unsigned int num_bits)
24{
25 unsigned int pbit_bytesize = num_bits / 8;
26
27 if (num_bits % 8)
28 pbit_bytesize++;
29
30 return pbit_bytesize;
31}
32
Harald Weltebd598e32011-08-16 23:26:52 +020033/*! \brief convert unpacked bits to packed bits, return length in bytes
34 * \param[out] out output buffer of packed bits
35 * \param[in] in input buffer of unpacked bits
36 * \param[in] num_bits number of bits
37 */
Harald Welte2230c132011-01-19 10:10:16 +010038int osmo_ubit2pbit(pbit_t *out, const ubit_t *in, unsigned int num_bits);
39
Harald Weltebd598e32011-08-16 23:26:52 +020040/*! \brief convert packed bits to unpacked bits, return length in bytes
41 * \param[out] out output buffer of unpacked bits
42 * \param[in] in input buffer of packed bits
43 * \param[in] num_bits number of bits
44 */
Harald Welte2230c132011-01-19 10:10:16 +010045int osmo_pbit2ubit(ubit_t *out, const pbit_t *in, unsigned int num_bits);
46
Harald Weltebd598e32011-08-16 23:26:52 +020047/*! \brief convert unpacked bits to packed bits (extended options)
48 * \param[out] out output buffer of packed bits
49 * \param[in] out_ofs offset into output buffer
50 * \param[in] in input buffer of unpacked bits
51 * \param[in] in_ofs offset into input buffer
52 * \param[in] num_bits number of bits
53 * \param[in] lsb_mode Encode bits in LSB orde instead of MSB
54 * \returns length in bytes (max written offset of output buffer + 1)
55 */
Sylvain Munautaeb10772011-01-21 12:22:30 +010056int osmo_ubit2pbit_ext(pbit_t *out, unsigned int out_ofs,
57 const ubit_t *in, unsigned int in_ofs,
58 unsigned int num_bits, int lsb_mode);
59
Harald Weltebd598e32011-08-16 23:26:52 +020060/*! \brief convert packed bits to unpacked bits (extended options)
61 * \param[out] out output buffer of unpacked bits
62 * \param[in] out_ofs offset into output buffer
63 * \param[in] in input buffer of packed bits
64 * \param[in] in_ofs offset into input buffer
65 * \param[in] num_bits number of bits
66 * \param[in] lsb_mode Encode bits in LSB orde instead of MSB
67 * \returns length in bytes (max written offset of output buffer + 1)
68 */
Sylvain Munautaeb10772011-01-21 12:22:30 +010069int osmo_pbit2ubit_ext(ubit_t *out, unsigned int out_ofs,
70 const pbit_t *in, unsigned int in_ofs,
71 unsigned int num_bits, int lsb_mode);
72
Harald Welte2230c132011-01-19 10:10:16 +010073#endif