libosmocore 1.13.1.1-afb4 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
11#include <osmocom/core/bit16gen.h>
12#include <osmocom/core/bit32gen.h>
13#include <osmocom/core/bit64gen.h>
14
21 typedef int8_t sbit_t;
22
24 typedef uint8_t ubit_t;
25
30 typedef uint8_t pbit_t;
31
36 static 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
46 int osmo_ubit2pbit(pbit_t *out, const ubit_t *in, unsigned int num_bits);
47
48 int osmo_pbit2ubit(ubit_t *out, const pbit_t *in, unsigned int num_bits);
49
50 void osmo_nibble_shift_right(uint8_t *out, const uint8_t *in,
51 unsigned int num_nibbles);
52 void osmo_nibble_shift_left_unal(uint8_t *out, const uint8_t *in,
53 unsigned int num_nibbles);
54
55 void osmo_ubit2sbit(sbit_t *out, const ubit_t *in, unsigned int num_bits);
56 void osmo_sbit2ubit(ubit_t *out, const sbit_t *in, unsigned int num_bits);
57
58 int 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
62 int 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
93 enum osmo_br_mode {
95 OSMO_BR_BITS_IN_DWORD = 31,
97 OSMO_BR_BYTES_IN_DWORD = 24,
99 OSMO_BR_BITS_IN_BYTE = 7,
101 OSMO_BR_WORD_SWAP = 16,
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
110 void osmo_revbytebits_buf(uint8_t *buf, int len);
111
117 static inline uint16_t osmo_rol16(uint16_t in, unsigned shift)
118{
119 return (in << shift) | (in >> (16 - shift));
120}
121
bit16gen.h
bit32gen.h
bit64gen.h
k
uint8_t k[32]
osmo_ubit2pbit
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:42
pbit_t
uint8_t pbit_t
packed bits (8 bits in a byte).
Definition: bits.h:30
osmo_pbit_bytesize
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
osmo_br_mode
bit-reversal mode for osmo_bit_reversal()
Definition: bits.h:93
osmo_sbit2ubit
void osmo_sbit2ubit(ubit_t *out, const sbit_t *in, unsigned int num_bits)
convert soft bits to unpacked bits
Definition: bits.c:130
osmo_ubit2pbit_ext
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:188
osmo_pbit2ubit
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:143
ubit_t
uint8_t ubit_t
unpacked bit (0 or 1): 1 bit per byte
Definition: bits.h:24
osmo_nibble_shift_left_unal
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:97
osmo_pbit2ubit_ext
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:213
osmo_revbytebits_32
uint32_t osmo_revbytebits_32(uint32_t x)
reverse the bit-order in each byte of a dword
Definition: bits.c:282
osmo_ubit2sbit
void osmo_ubit2sbit(sbit_t *out, const ubit_t *in, unsigned int num_bits)
convert unpacked bits to soft bits
Definition: bits.c:118
osmo_revbytebits_buf
void osmo_revbytebits_buf(uint8_t *buf, int len)
reverse bit-order of each byte in a buffer
Definition: bits.c:306
sbit_t
int8_t sbit_t
soft bit with value (-127...127), as commonly used in communications receivers such as [viterbi] deco...
Definition: bits.h:21
osmo_rol16
static uint16_t osmo_rol16(uint16_t in, unsigned shift)
left circular shift
Definition: bits.h:117
osmo_nibble_shift_right
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:70
osmo_bit_reversal
uint32_t osmo_bit_reversal(uint32_t x, enum osmo_br_mode k)
generalized bit reversal function
Definition: bits.c:265
osmo_revbytebits_8
uint32_t osmo_revbytebits_8(uint8_t x)
reverse the bit order in a byte
Definition: bits.c:295
OSMO_BR_BITS_IN_DWORD
@ OSMO_BR_BITS_IN_DWORD
reverse all bits in a 32bit dword
Definition: bits.h:95
OSMO_BR_BYTES_IN_DWORD
@ OSMO_BR_BYTES_IN_DWORD
reverse byte order in a 32bit dword
Definition: bits.h:97
OSMO_BR_BITS_IN_BYTE
@ OSMO_BR_BITS_IN_BYTE
reverse bits of each byte in a 32bit dword
Definition: bits.h:99
OSMO_BR_WORD_SWAP
@ OSMO_BR_WORD_SWAP
swap the two 16bit words in a 32bit dword
Definition: bits.h:101
len
static size_t len(const char *str)
x
uint32_t x
Definition: jhash.h:0