libosmocore 1.10.0.27-aec32
Osmocom core library
bits.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <stdint.h>
9#include <stddef.h>
10
14
21typedef int8_t sbit_t;
22
24typedef uint8_t ubit_t;
25
30typedef uint8_t pbit_t;
31
36static inline unsigned int osmo_pbit_bytesize(unsigned int num_bits)
37{
38 unsigned int pbit_bytesize = num_bits / 8;
39
40 if (num_bits % 8)
41 pbit_bytesize++;
42
43 return pbit_bytesize;
44}
45
46int osmo_ubit2pbit(pbit_t *out, const ubit_t *in, unsigned int num_bits);
47
48int osmo_pbit2ubit(ubit_t *out, const pbit_t *in, unsigned int num_bits);
49
50void osmo_nibble_shift_right(uint8_t *out, const uint8_t *in,
51 unsigned int num_nibbles);
52void osmo_nibble_shift_left_unal(uint8_t *out, const uint8_t *in,
53 unsigned int num_nibbles);
54
55void osmo_ubit2sbit(sbit_t *out, const ubit_t *in, unsigned int num_bits);
56void osmo_sbit2ubit(ubit_t *out, const sbit_t *in, unsigned int num_bits);
57
58int osmo_ubit2pbit_ext(pbit_t *out, unsigned int out_ofs,
59 const ubit_t *in, unsigned int in_ofs,
60 unsigned int num_bits, int lsb_mode);
61
62int osmo_pbit2ubit_ext(ubit_t *out, unsigned int out_ofs,
63 const pbit_t *in, unsigned int in_ofs,
64 unsigned int num_bits, int lsb_mode);
65
66#define OSMO_BIN_SPEC "%d%d%d%d%d%d%d%d"
67#define OSMO_BIN_PRINT(byte) \
68 (byte & 0x80 ? 1 : 0), \
69 (byte & 0x40 ? 1 : 0), \
70 (byte & 0x20 ? 1 : 0), \
71 (byte & 0x10 ? 1 : 0), \
72 (byte & 0x08 ? 1 : 0), \
73 (byte & 0x04 ? 1 : 0), \
74 (byte & 0x02 ? 1 : 0), \
75 (byte & 0x01 ? 1 : 0)
76
77#define OSMO_BIT_SPEC "%c%c%c%c%c%c%c%c"
78#define OSMO_BIT_PRINT_EX(byte, ch) \
79 (byte & 0x80 ? ch : '.'), \
80 (byte & 0x40 ? ch : '.'), \
81 (byte & 0x20 ? ch : '.'), \
82 (byte & 0x10 ? ch : '.'), \
83 (byte & 0x08 ? ch : '.'), \
84 (byte & 0x04 ? ch : '.'), \
85 (byte & 0x02 ? ch : '.'), \
86 (byte & 0x01 ? ch : '.')
87
88#define OSMO_BIT_PRINT(byte) OSMO_BIT_PRINT_EX(byte, '1')
89
90/* BIT REVERSAL */
91
102};
103
104uint32_t osmo_bit_reversal(uint32_t x, enum osmo_br_mode k);
105
106uint32_t osmo_revbytebits_32(uint32_t x);
107
108uint32_t osmo_revbytebits_8(uint8_t x);
109
110void osmo_revbytebits_buf(uint8_t *buf, int len);
111
117static inline uint16_t osmo_rol16(uint16_t in, unsigned shift)
118{
119 return (in << shift) | (in >> (16 - shift));
120}
121
uint8_t k[32]
int osmo_ubit2pbit(pbit_t *out, const ubit_t *in, unsigned int num_bits)
convert unpacked bits to packed bits, return length in bytes
Definition: bits.c:41
uint8_t pbit_t
packed bits (8 bits in a byte).
Definition: bits.h:30
static unsigned int osmo_pbit_bytesize(unsigned int num_bits)
determine how many bytes we would need for num_bits packed bits
Definition: bits.h:36
osmo_br_mode
bit-reversal mode for osmo_bit_reversal()
Definition: bits.h:93
void osmo_sbit2ubit(ubit_t *out, const sbit_t *in, unsigned int num_bits)
convert soft bits to unpacked bits
Definition: bits.c:129
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)
convert unpacked bits to packed bits (extended options)
Definition: bits.c:187
int osmo_pbit2ubit(ubit_t *out, const pbit_t *in, unsigned int num_bits)
convert packed bits to unpacked bits, return length in bytes
Definition: bits.c:142
uint8_t ubit_t
unpacked bit (0 or 1): 1 bit per byte
Definition: bits.h:24
void osmo_nibble_shift_left_unal(uint8_t *out, const uint8_t *in, unsigned int num_nibbles)
Shift unaligned input to octet-aligned output.
Definition: bits.c:96
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)
convert packed bits to unpacked bits (extended options)
Definition: bits.c:212
uint32_t osmo_revbytebits_32(uint32_t x)
reverse the bit-order in each byte of a dword
Definition: bits.c:281
void osmo_ubit2sbit(sbit_t *out, const ubit_t *in, unsigned int num_bits)
convert unpacked bits to soft bits
Definition: bits.c:117
void osmo_revbytebits_buf(uint8_t *buf, int len)
reverse bit-order of each byte in a buffer
Definition: bits.c:305
int8_t sbit_t
soft bit with value (-127...127), as commonly used in communications receivers such as [viterbi] deco...
Definition: bits.h:21
static uint16_t osmo_rol16(uint16_t in, unsigned shift)
left circular shift
Definition: bits.h:117
void osmo_nibble_shift_right(uint8_t *out, const uint8_t *in, unsigned int num_nibbles)
Shift unaligned input to octet-aligned output.
Definition: bits.c:69
uint32_t osmo_bit_reversal(uint32_t x, enum osmo_br_mode k)
generalized bit reversal function
Definition: bits.c:264
uint32_t osmo_revbytebits_8(uint8_t x)
reverse the bit order in a byte
Definition: bits.c:294
@ OSMO_BR_BITS_IN_DWORD
reverse all bits in a 32bit dword
Definition: bits.h:95
@ OSMO_BR_BYTES_IN_DWORD
reverse byte order in a 32bit dword
Definition: bits.h:97
@ OSMO_BR_BITS_IN_BYTE
reverse bits of each byte in a 32bit dword
Definition: bits.h:99
@ OSMO_BR_WORD_SWAP
swap the two 16bit words in a 32bit dword
Definition: bits.h:101
static size_t len(const char *str)
uint32_t x
Definition: jhash.h:0