Harald Welte | 2230c13 | 2011-01-19 10:10:16 +0100 | [diff] [blame] | 1 | #ifndef _OSMO_BITS_H |
| 2 | #define _OSMO_BITS_H |
| 3 | |
| 4 | #include <stdint.h> |
| 5 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 6 | /*! \file bits.h |
| 7 | * \brief Osmocom bit level support code |
| 8 | */ |
| 9 | |
| 10 | typedef int8_t sbit_t; /*!< \brief soft bit (-127...127) */ |
| 11 | typedef uint8_t ubit_t; /*!< \brief unpacked bit (0 or 1) */ |
| 12 | typedef uint8_t pbit_t; /*!< \brief packed bis (8 bits in a byte) */ |
Harald Welte | 2230c13 | 2011-01-19 10:10:16 +0100 | [diff] [blame] | 13 | |
Christian Vogel | c7f84e9 | 2011-01-22 22:48:37 +0100 | [diff] [blame] | 14 | /* |
| 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 Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 20 | /*! \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 Welte | 2230c13 | 2011-01-19 10:10:16 +0100 | [diff] [blame] | 23 | static 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 Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 33 | /*! \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 Welte | 2230c13 | 2011-01-19 10:10:16 +0100 | [diff] [blame] | 38 | int osmo_ubit2pbit(pbit_t *out, const ubit_t *in, unsigned int num_bits); |
| 39 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 40 | /*! \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 Welte | 2230c13 | 2011-01-19 10:10:16 +0100 | [diff] [blame] | 45 | int osmo_pbit2ubit(ubit_t *out, const pbit_t *in, unsigned int num_bits); |
| 46 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 47 | /*! \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 Munaut | aeb1077 | 2011-01-21 12:22:30 +0100 | [diff] [blame] | 56 | int 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 Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 60 | /*! \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 Munaut | aeb1077 | 2011-01-21 12:22:30 +0100 | [diff] [blame] | 69 | int 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 Welte | 2230c13 | 2011-01-19 10:10:16 +0100 | [diff] [blame] | 73 | #endif |