summaryrefslogtreecommitdiffstats
path: root/include/osmocom/core/bits.h
blob: 61af716cf9896581473302f0309f45eb78a3f182 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#pragma once

#include <stdint.h>
#include <stddef.h>

#include <osmocom/core/bit16gen.h>
#include <osmocom/core/bit32gen.h>
#include <osmocom/core/bit64gen.h>

/*! \defgroup bits soft, unpacked and packed bits
 *  @{
 */

/*! \file bits.h
 *  Osmocom bit level support code
 *
 *  NOTE on the endianess of pbit_t:
 *  Bits in a pbit_t are ordered MSB first, i.e. 0x80 is the first bit.
 *  Bit i in a pbit_t array is array[i/8] & (1<<(7-i%8))
 */

typedef int8_t  sbit_t;		/*!< soft bit (-127...127) */
typedef uint8_t ubit_t;		/*!< unpacked bit (0 or 1) */
typedef uint8_t pbit_t;		/*!< packed bis (8 bits in a byte) */

/*! determine how many bytes we would need for \a num_bits packed bits
 *  \param[in] num_bits Number of packed bits
 *  \returns number of bytes needed for \a num_bits packed bits
 */
static inline unsigned int osmo_pbit_bytesize(unsigned int num_bits)
{
	unsigned int pbit_bytesize = num_bits / 8;

	if (num_bits % 8)
		pbit_bytesize++;

	return pbit_bytesize;
}

int osmo_ubit2pbit(pbit_t *out, const ubit_t *in, unsigned int num_bits);

int osmo_pbit2ubit(ubit_t *out, const pbit_t *in, unsigned int num_bits);

void osmo_nibble_shift_right(uint8_t *out, const uint8_t *in,
			     unsigned int num_nibbles);
void osmo_nibble_shift_left_unal(uint8_t *out, const uint8_t *in,
				 unsigned int num_nibbles);

void osmo_ubit2sbit(sbit_t *out, const ubit_t *in, unsigned int num_bits);
void osmo_sbit2ubit(ubit_t *out, const sbit_t *in, unsigned int num_bits);

int osmo_ubit2pbit_ext(pbit_t *out, unsigned int out_ofs,
                       const ubit_t *in, unsigned int in_ofs,
                       unsigned int num_bits, int lsb_mode);

int osmo_pbit2ubit_ext(ubit_t *out, unsigned int out_ofs,
                       const pbit_t *in, unsigned int in_ofs,
                       unsigned int num_bits, int lsb_mode);

#define OSMO_BIN_SPEC "%d%d%d%d%d%d%d%d"
#define OSMO_BIN_PRINT(byte)  \
  (byte & 0x80 ? 1 : 0), \
  (byte & 0x40 ? 1 : 0), \
  (byte & 0x20 ? 1 : 0), \
  (byte & 0x10 ? 1 : 0), \
  (byte & 0x08 ? 1 : 0), \
  (byte & 0x04 ? 1 : 0), \
  (byte & 0x02 ? 1 : 0), \
  (byte & 0x01 ? 1 : 0)

#define OSMO_BIT_SPEC "%c%c%c%c%c%c%c%c"
#define OSMO_BIT_PRINT(byte)  \
  (byte & 0x80 ? '1' : '.'), \
  (byte & 0x40 ? '1' : '.'), \
  (byte & 0x20 ? '1' : '.'), \
  (byte & 0x10 ? '1' : '.'), \
  (byte & 0x08 ? '1' : '.'), \
  (byte & 0x04 ? '1' : '.'), \
  (byte & 0x02 ? '1' : '.'), \
  (byte & 0x01 ? '1' : '.')

/* BIT REVERSAL */

/*! bit-reversal mode for osmo_bit_reversal() */
enum osmo_br_mode {
	/*! reverse all bits in a 32bit dword */
	OSMO_BR_BITS_IN_DWORD	= 31,
	/*! reverse byte order in a 32bit dword */
	OSMO_BR_BYTES_IN_DWORD	= 24,
	/*! reverse bits of each byte in a 32bit dword */
	OSMO_BR_BITS_IN_BYTE	= 7,
	/*! swap the two 16bit words in a 32bit dword */
	OSMO_BR_WORD_SWAP	= 16,
};

/*! generic bit reversal function */
uint32_t osmo_bit_reversal(uint32_t x, enum osmo_br_mode k);

/* reverse the bits within each byte of a 32bit word */
uint32_t osmo_revbytebits_32(uint32_t x);

/* reverse the bits within a byte */
uint32_t osmo_revbytebits_8(uint8_t x);

/* reverse the bits of each byte in a given buffer */
void osmo_revbytebits_buf(uint8_t *buf, int len);

/*! left circular shift
 *  \param[in] in The 16 bit unsigned integer to be rotated
 *  \param[in] shift Number of bits to shift \a in to, [0;16] bits
 *  \returns shifted value
 */
static inline uint16_t osmo_rol16(uint16_t in, unsigned shift)
{
	return (in << shift) | (in >> (16 - shift));
}

/*! @} */