bits.h
Go to the documentation of this file.
#pragma once
#include <stdint.h>
#include <stddef.h>
#include <osmocom/core/bit16gen.h>
#include <osmocom/core/bit32gen.h>
#include <osmocom/core/bit64gen.h>
typedef int8_t sbit_t;
typedef uint8_t ubit_t;
typedef uint8_t pbit_t;
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_EX(byte, ch) \
(byte & 0x80 ? ch : '.'), \
(byte & 0x40 ? ch : '.'), \
(byte & 0x20 ? ch : '.'), \
(byte & 0x10 ? ch : '.'), \
(byte & 0x08 ? ch : '.'), \
(byte & 0x04 ? ch : '.'), \
(byte & 0x02 ? ch : '.'), \
(byte & 0x01 ? ch : '.')
#define OSMO_BIT_PRINT(byte) OSMO_BIT_PRINT_EX(byte, '1')
/* BIT REVERSAL */
enum osmo_br_mode {
OSMO_BR_BITS_IN_DWORD = 31,
OSMO_BR_BYTES_IN_DWORD = 24,
OSMO_BR_BITS_IN_BYTE = 7,
OSMO_BR_WORD_SWAP = 16,
};
uint32_t osmo_bit_reversal(uint32_t x, enum osmo_br_mode k);
uint32_t osmo_revbytebits_32(uint32_t x);
uint32_t osmo_revbytebits_8(uint8_t x);
void osmo_revbytebits_buf(uint8_t *buf, int len);
static inline uint16_t osmo_rol16(uint16_t in, unsigned shift)
{
return (in << shift) | (in >> (16 - shift));
}
bit32gen.h
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:45
osmo_rol16
static uint16_t osmo_rol16(uint16_t in, unsigned shift)
left circular shift
Definition: bits.h:117
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:146
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:100
OSMO_BR_BITS_IN_DWORD
@ OSMO_BR_BITS_IN_DWORD
reverse all bits in a 32bit dword
Definition: bits.h:95
osmo_revbytebits_buf
void osmo_revbytebits_buf(uint8_t *buf, int len)
reverse bit-order of each byte in a buffer
Definition: bits.c:286
bit16gen.h
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_BR_BITS_IN_BYTE
@ OSMO_BR_BITS_IN_BYTE
reverse bits of each byte in a 32bit dword
Definition: bits.h:99
log_target::out
FILE * out
Definition: logging.h:261
osmo_bit_reversal
uint32_t osmo_bit_reversal(uint32_t x, enum osmo_br_mode k)
generalized bit reversal function
Definition: bits.c:239
osmo_revbytebits_32
uint32_t osmo_revbytebits_32(uint32_t x)
reverse the bit-order in each byte of a dword
Definition: bits.c:256
pbit_t
uint8_t pbit_t
packed bits (8 bits in a byte).
Definition: bits.h:30
osmo_revbytebits_8
uint32_t osmo_revbytebits_8(uint8_t x)
reverse the bit order in a byte
Definition: bits.c:271
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:133
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
ubit_t
uint8_t ubit_t
unpacked bit (0 or 1): 1 bit per byte
Definition: bits.h:24
osmo_br_mode
osmo_br_mode
bit-reversal mode for osmo_bit_reversal()
Definition: bits.h:93
OSMO_BR_BYTES_IN_DWORD
@ OSMO_BR_BYTES_IN_DWORD
reverse byte order in a 32bit dword
Definition: bits.h:97
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:121
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:216
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:191
OSMO_BR_WORD_SWAP
@ OSMO_BR_WORD_SWAP
swap the two 16bit words in a 32bit dword
Definition: bits.h:101
bit64gen.h
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:73