blob: e082313a95f15ac1ef17bf381d555102b7cc2b89 [file] [log] [blame]
Sylvain Munaut12ba7782014-06-16 10:13:40 +02001#pragma once
Harald Welte2230c132011-01-19 10:10:16 +01002
3#include <stdint.h>
Max53777012014-06-04 19:07:41 +02004#include <stddef.h>
5
6#include <osmocom/core/bit16gen.h>
7#include <osmocom/core/bit32gen.h>
8#include <osmocom/core/bit64gen.h>
Harald Welte2230c132011-01-19 10:10:16 +01009
Harald Welteba6988b2011-08-17 12:46:48 +020010/*! \defgroup bits soft, unpacked and packed bits
11 * @{
12 */
13
Harald Weltebd598e32011-08-16 23:26:52 +020014/*! \file bits.h
15 * \brief Osmocom bit level support code
16 */
17
18typedef int8_t sbit_t; /*!< \brief soft bit (-127...127) */
19typedef uint8_t ubit_t; /*!< \brief unpacked bit (0 or 1) */
20typedef uint8_t pbit_t; /*!< \brief packed bis (8 bits in a byte) */
Harald Welte2230c132011-01-19 10:10:16 +010021
Christian Vogelc7f84e92011-01-22 22:48:37 +010022/*
23 NOTE on the endianess of pbit_t:
24 Bits in a pbit_t are ordered MSB first, i.e. 0x80 is the first bit.
25 Bit i in a pbit_t array is array[i/8] & (1<<(7-i%8))
26*/
27
Harald Weltebd598e32011-08-16 23:26:52 +020028/*! \brief determine how many bytes we would need for \a num_bits packed bits
29 * \param[in] num_bits Number of packed bits
30 */
Harald Welte2230c132011-01-19 10:10:16 +010031static inline unsigned int osmo_pbit_bytesize(unsigned int num_bits)
32{
33 unsigned int pbit_bytesize = num_bits / 8;
34
35 if (num_bits % 8)
36 pbit_bytesize++;
37
38 return pbit_bytesize;
39}
40
Harald Welte2230c132011-01-19 10:10:16 +010041int osmo_ubit2pbit(pbit_t *out, const ubit_t *in, unsigned int num_bits);
42
Harald Welte2230c132011-01-19 10:10:16 +010043int osmo_pbit2ubit(ubit_t *out, const pbit_t *in, unsigned int num_bits);
44
Maxd8fb1422016-04-06 16:13:00 +020045void osmo_ubit2sbit(sbit_t *out, const ubit_t *in, unsigned int num_bits);
46void osmo_sbit2ubit(ubit_t *out, const sbit_t *in, unsigned int num_bits);
47
Sylvain Munautaeb10772011-01-21 12:22:30 +010048int osmo_ubit2pbit_ext(pbit_t *out, unsigned int out_ofs,
49 const ubit_t *in, unsigned int in_ofs,
50 unsigned int num_bits, int lsb_mode);
51
Sylvain Munautaeb10772011-01-21 12:22:30 +010052int osmo_pbit2ubit_ext(ubit_t *out, unsigned int out_ofs,
53 const pbit_t *in, unsigned int in_ofs,
54 unsigned int num_bits, int lsb_mode);
55
Max6a5ef462016-02-24 16:05:48 +010056#define OSMO_BIN_SPEC "%d%d%d%d%d%d%d%d"
57#define OSMO_BIN_PRINT(byte) \
58 (byte & 0x80 ? 1 : 0), \
59 (byte & 0x40 ? 1 : 0), \
60 (byte & 0x20 ? 1 : 0), \
61 (byte & 0x10 ? 1 : 0), \
62 (byte & 0x08 ? 1 : 0), \
63 (byte & 0x04 ? 1 : 0), \
64 (byte & 0x02 ? 1 : 0), \
65 (byte & 0x01 ? 1 : 0)
66
67#define OSMO_BIT_SPEC "%c%c%c%c%c%c%c%c"
68#define OSMO_BIT_PRINT(byte) \
69 (byte & 0x80 ? '1' : '.'), \
70 (byte & 0x40 ? '1' : '.'), \
71 (byte & 0x20 ? '1' : '.'), \
72 (byte & 0x10 ? '1' : '.'), \
73 (byte & 0x08 ? '1' : '.'), \
74 (byte & 0x04 ? '1' : '.'), \
75 (byte & 0x02 ? '1' : '.'), \
76 (byte & 0x01 ? '1' : '.')
Harald Welte712691d2011-09-01 14:47:31 +020077
78/* BIT REVERSAL */
79
80/*! \brief bit-reversal mode for osmo_bit_reversal() */
81enum osmo_br_mode {
82 /*! \brief reverse all bits in a 32bit dword */
83 OSMO_BR_BITS_IN_DWORD = 31,
84 /*! \brief reverse byte order in a 32bit dword */
85 OSMO_BR_BYTES_IN_DWORD = 24,
86 /*! \brief reverse bits of each byte in a 32bit dword */
87 OSMO_BR_BITS_IN_BYTE = 7,
88 /*! \brief swap the two 16bit words in a 32bit dword */
89 OSMO_BR_WORD_SWAP = 16,
90};
91
92/*! \brief generic bit reversal function */
93uint32_t osmo_bit_reversal(uint32_t x, enum osmo_br_mode k);
94
95/* \brief reverse the bits within each byte of a 32bit word */
96uint32_t osmo_revbytebits_32(uint32_t x);
97
98/* \brief reverse the bits within a byte */
99uint32_t osmo_revbytebits_8(uint8_t x);
100
101/* \brief reverse the bits of each byte in a given buffer */
102void osmo_revbytebits_buf(uint8_t *buf, int len);
103
Maxe2c13902014-06-04 19:07:42 +0200104/*! \brief left circular shift
105 * \param[in] in The 16 bit unsigned integer to be rotated
106 * \param[in] shift Number of bits to shift \a in to, [0;16] bits
107 * \returns shifted value
108 */
109static inline uint16_t osmo_rol16(uint16_t in, unsigned shift)
110{
111 return (in << shift) | (in >> (16 - shift));
112}
113
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200114/*! @} */