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;
/*
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))
*/
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);
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);
/* 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);
/* \brief reverse the bits within each byte of a 32bit word */
uint32_t osmo_revbytebits_32(uint32_t x);
/* \brief reverse the bits within a byte */
uint32_t osmo_revbytebits_8(uint8_t x);
/* \brief reverse the bits of each byte in a given buffer */
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));
}
sbit_t
int8_t sbit_t
soft bit (-127...127)
Definition: bits.h:18
OSMO_BR_BITS_IN_DWORD
reverse all bits in a 32bit dword
Definition: bits.h:59
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:69
osmo_rol16
static uint16_t osmo_rol16(uint16_t in, unsigned shift)
left circular shift
Definition: bits.h:85
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:31
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:41
osmo_br_mode
osmo_br_mode
bit-reversal mode for osmo_bit_reversal()
Definition: bits.h:57
OSMO_BR_BITS_IN_BYTE
reverse bits of each byte in a 32bit dword
Definition: bits.h:63
osmo_bit_reversal
uint32_t osmo_bit_reversal(uint32_t x, enum osmo_br_mode k)
generic bit reversal function
Definition: bits.c:162
osmo_revbytebits_buf
void osmo_revbytebits_buf(uint8_t *buf, int len)
reverse bit-order of each byte in a buffer
Definition: bits.c:209
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:114
pbit_t
uint8_t pbit_t
packed bis (8 bits in a byte)
Definition: bits.h:20
osmo_revbytebits_32
uint32_t osmo_revbytebits_32(uint32_t x)
reverse the bit-order in each byte of a dword
Definition: bits.c:179
OSMO_BR_BYTES_IN_DWORD
reverse byte order in a 32bit dword
Definition: bits.h:61
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:139
ubit_t
uint8_t ubit_t
unpacked bit (0 or 1)
Definition: bits.h:19
OSMO_BR_WORD_SWAP
swap the two 16bit words in a 32bit dword
Definition: bits.h:65
osmo_revbytebits_8
uint32_t osmo_revbytebits_8(uint8_t x)
reverse the bit order in a byte
Definition: bits.c:194