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 | |
Sylvain Munaut | d6643d5 | 2011-03-27 11:35:40 +0200 | [diff] [blame] | 6 | typedef int8_t sbit_t; /* soft bit (-127...127) */ |
Harald Welte | 2230c13 | 2011-01-19 10:10:16 +0100 | [diff] [blame] | 7 | typedef uint8_t ubit_t; /* unpacked bit (0 or 1) */ |
| 8 | typedef uint8_t pbit_t; /* packed bis (8 bits in a byte) */ |
| 9 | |
Christian Vogel | c7f84e9 | 2011-01-22 22:48:37 +0100 | [diff] [blame] | 10 | /* |
| 11 | NOTE on the endianess of pbit_t: |
| 12 | Bits in a pbit_t are ordered MSB first, i.e. 0x80 is the first bit. |
| 13 | Bit i in a pbit_t array is array[i/8] & (1<<(7-i%8)) |
| 14 | */ |
| 15 | |
Harald Welte | 2230c13 | 2011-01-19 10:10:16 +0100 | [diff] [blame] | 16 | /* determine how many bytes we would need for 'num_bits' packed bits */ |
| 17 | static inline unsigned int osmo_pbit_bytesize(unsigned int num_bits) |
| 18 | { |
| 19 | unsigned int pbit_bytesize = num_bits / 8; |
| 20 | |
| 21 | if (num_bits % 8) |
| 22 | pbit_bytesize++; |
| 23 | |
| 24 | return pbit_bytesize; |
| 25 | } |
| 26 | |
| 27 | /* convert unpacked bits to packed bits, return length in bytes */ |
| 28 | int osmo_ubit2pbit(pbit_t *out, const ubit_t *in, unsigned int num_bits); |
| 29 | |
| 30 | /* convert packed bits to unpacked bits, return length in bytes */ |
| 31 | int osmo_pbit2ubit(ubit_t *out, const pbit_t *in, unsigned int num_bits); |
| 32 | |
Sylvain Munaut | aeb1077 | 2011-01-21 12:22:30 +0100 | [diff] [blame] | 33 | /* convert unpacked bits to packed bits (extended options but slower), |
| 34 | * return length in bytes (max written ofs of output buffer + 1) */ |
| 35 | int osmo_ubit2pbit_ext(pbit_t *out, unsigned int out_ofs, |
| 36 | const ubit_t *in, unsigned int in_ofs, |
| 37 | unsigned int num_bits, int lsb_mode); |
| 38 | |
| 39 | /* convert packed bits to unpacked bits (extended options but slower), |
| 40 | * return length in bytes (max written ofs of output buffer + 1) */ |
| 41 | int osmo_pbit2ubit_ext(ubit_t *out, unsigned int out_ofs, |
| 42 | const pbit_t *in, unsigned int in_ofs, |
| 43 | unsigned int num_bits, int lsb_mode); |
| 44 | |
Harald Welte | 2230c13 | 2011-01-19 10:10:16 +0100 | [diff] [blame] | 45 | #endif |